Question

How to prevent duplicate data in PXGRID

  • 10 June 2021
  • 6 replies
  • 58 views

Hello Goodnight. I new in acumatica. I have the following requirement: the phase codes field cannot be repeated.My validation already works but the data must be saved in the DB previously.
The problem is when the user enters N number of rows (without saving before) that is when my validation does not work.
This is how I currently do the validation (Row_Updated event). It works, but the data has to be saved in the DB before.
Can you help me please. I think it should take data from the grid (cache) but I don't know how to do it.

6 replies

Userlevel 1
Badge

There actually already is implementation of the needed logic in Acumatica. 

You can put PXCheckUnique attribute on the field in the DAC and it will verify the uniqueness of the field. 

Here is example code:

     

public abstract class description: PX.Data.BQL.BqlString.Field<description> { }

[PXDBString(200,IsUnicode = true)]
[PXUIField(DisplayName = "Description")]
[PXCheckUnique(typeof(CustomDAC.description))]
[PXDefault]
public virtual string Description
{
get; set;
}

 

Userlevel 6
Badge +4

Hi @eddiedaco, Good Morning !!

This validation you need to handle in FeildVerifying and Perisist delete(). Please find the code sample below.

public delegate void PersistDelegate();
[PXOverride]
public void Persist(PersistDelegate del)
{
if (Base.BAccount.Current != null)
{
int count = 0;
List<string> liColors = new List<string>();
foreach (DACName rec in ViewName.cache.Cached)
{
if (liColors.Contains(rec.LevisColorID))
{
count = 1;
break;
}
else liColors.Add(rec.FiledName);
}

if (count >= 1)
{
throw new Exception("Duplication record not allowed.Please review.");
}
}
del();
}



protected virtual void DACName_FieldName_FieldVerifying(PXCache cache, PXFieldVerifyingEventArgs e)
{
DACName row = (DACName)e.Row;
if (row == null) return;
if (e.NewValue != null)
{
int count = ViewName.Select().FirstTableItems.ToList().Where(x => x.FiledName == e.NewValue.ToString()).Count();
if (count > 1)
{
cache.RaiseExceptionHandling<DACName.filedName>(row, "", new PXSetPropertyException("Duplicate Record Found. Please review."));
}
}
}

 

Hope this help!!

There actually already is implementation of the needed logic in Acumatica. 

You can put PXCheckUnique attribute on the field in the DAC and it will verify the uniqueness of the field. 

Here is example code:

     

public abstract class description: PX.Data.BQL.BqlString.Field<description> { }

[PXDBString(200,IsUnicode = true)]
[PXUIField(DisplayName = "Description")]
[PXCheckUnique(typeof(CustomDAC.description))]
[PXDefault]
public virtual string Description
{
get; set;
}

 

@dnaumov  Thank you so much. I add the property to my field but it still allows inserting repeating fields.

Hi @eddiedaco, Good Morning !!

This validation you need to handle in FeildVerifying and Perisist delete(). Please find the code sample below.

public delegate void PersistDelegate();
[PXOverride]
public void Persist(PersistDelegate del)
{
if (Base.BAccount.Current != null)
{
int count = 0;
List<string> liColors = new List<string>();
foreach (DACName rec in ViewName.cache.Cached)
{
if (liColors.Contains(rec.LevisColorID))
{
count = 1;
break;
}
else liColors.Add(rec.FiledName);
}

if (count >= 1)
{
throw new Exception("Duplication record not allowed.Please review.");
}
}
del();
}



protected virtual void DACName_FieldName_FieldVerifying(PXCache cache, PXFieldVerifyingEventArgs e)
{
DACName row = (DACName)e.Row;
if (row == null) return;
if (e.NewValue != null)
{
int count = ViewName.Select().FirstTableItems.ToList().Where(x => x.FiledName == e.NewValue.ToString()).Count();
if (count > 1)
{
cache.RaiseExceptionHandling<DACName.filedName>(row, "", new PXSetPropertyException("Duplicate Record Found. Please review."));
}
}
}

 

Hope this help!!

 

Hi! @Naveen B  

Thank you! I tried to use the PersistDelegate function.
 but I got this error 

 

'FbqlSelect<SelectFromBase<CEPhaseCodes, TypeArrayOf<IFbqlJoin>.Empty>.Order<By<BqlField<CEPhaseCodes.phaseCode, IBqlString>.Asc>>, CEPhaseCodes>.View' does not contain a definition for 'cache' and no accessible extension method 'cache' accepting a first argument of type 'FbqlSelect<SelectFromBase<CEPhaseCodes, TypeArrayOf<IFbqlJoin>.Empty>.Order<By<BqlField<CEPhaseCodes.phaseCode, IBqlString>.Asc>>, CEPhaseCodes>.View' could be found (are you missing a using directive or an assembly reference?)

 

Also implement FieldVerify 
but I got this error
 'PXResultset<CEPhaseCodes>' does not contain a definition for 'PhaseCode' and no accessible extension method 'PhaseCode' accepting a first argument of type 'PXResultset<CEPhaseCodes>' could be found (are you missing a using directive or an assembly reference?)

 

sorry I'm new, please help.

Userlevel 6
Badge +4

Hi @dnaumov Thanks for you response. Long back I also tried with “PXCheckUnique” for some reason it didn’t work for me as well. Hence we have handled in event…

@eddiedaco Since this is NOT a extended graph, you need to have a logic in the override persist() code block, instead of Persist() delegate. Please find the sample code here.

 

    public override void Persist()
        {
             base.Persist();

             // logic here       

        }

@Naveen B 

Thank you very much, I check it these days

Reply


About Acumatica ERP system
Acumatica Cloud ERP provides the best business management solution for transforming your company to thrive in the new digital economy. Built on a future-proof platform with open architecture for rapid integrations, scalability, and ease of use, Acumatica delivers unparalleled value to small and midmarket organizations. Connected Business. Delivered.
© 2008 — 2020  Acumatica, Inc. All rights reserved