Solved

When I tried to update the existing record, but it is still inserting the new record.

  • 29 April 2022
  • 8 replies
  • 576 views

Userlevel 3
Badge

Hi Everyone,

In the Stock Items, I’m trying to update the existing record with the below code but it is still trying to insert a new record in the Cross-Reference tab.

Can anyone help on this?

 

  public delegate void PersistDelegate();
[PXOverride]
public void Persist(PersistDelegate del)
{
try
{
InventoryItem row = Base.Item.Current;
if (row != null)
{
CSAnswers objCSAnswers = PXSelect<CSAnswers, Where<CSAnswers.refNoteID, Equal<Required<CSAnswers.refNoteID>>,
And<CSAnswers.attributeID, Equal<Required<CSAnswers.attributeID>>>>>.Select(Base, row.NoteID, "ALTID");

if (objCSAnswers != null && !string.IsNullOrEmpty(objCSAnswers.Value))
{
row.GetExtension<InventoryItemCExt>().UsrCAlternateID = objCSAnswers.Value;

INItemXRef objINItemXRef = PXSelect<INItemXRef, Where<INItemXRef.inventoryID, Equal<Required<INItemXRef.inventoryID>>,
And<INItemXRefCExt.usrCAltIDExists, Equal<boolTrue>,
And<INItemXRef.alternateType, Equal<INAlternateType.global>>>>>.Select(Base, row.InventoryID);


if (objINItemXRef != null)
{
INItemXRefCExt refExt = objINItemXRef.GetExtension<INItemXRefCExt>();


if (objINItemXRef.InventoryID == row.InventoryID && refExt.UsrCAltIDExists == true && objINItemXRef.AlternateType == INAlternateType.Global)
{
objINItemXRef.AlternateID = objCSAnswers.Value.Trim();
Base.itemxrefrecords.Cache.Update(objINItemXRef);
}
}

}
}
del();
}
catch (Exception ex)
{
throw ex;
}
}

 

icon

Best answer by nsmith51 2 May 2022, 21:01

View original

8 replies

Userlevel 3
Badge

When you instantiate the objINItemXRef, instead of doing a PXSelect, try to search the itemxrefrecords view, there by when you later update the view with that object, the key values match exactly.

Like-

INItemXRef objINItemXRef = Base.itemxrefrecords.Search<INItemXRef.inventoryID, INItemXRefCExt.usrCAltIDExists, INItemXRef.alternateType>(row.InventoryID, boolTrue, INAlternateType.global);

 

//…

//make update to the necessary fields

//...

Base.itemxrefrecords.Cache.Update(objINItemXRef);

Userlevel 3
Badge

Hi @Raj Gopinathan  Is it possible to share the sample code?

Userlevel 3
Badge

Yup, I accidentally hit send before updating example. Updated now.

Userlevel 3
Badge

Hi @Raj Gopinathan  As suggested, I have tried with the below code and it is also behaving the same.

 

It is creating a new record instead of updating.

 

  INItemXRef objINItemXRef = Base.itemxrefrecords.Search<INItemXRef.inventoryID,
INItemXRefCExt.usrCAltIDExists, INItemXRef.alternateType>(row.InventoryID, true, INAlternateType.Global);

if (objINItemXRef != null)
{
INItemXRefCExt refExt = objINItemXRef.GetExtension<INItemXRefCExt>();


if (objINItemXRef.InventoryID == row.InventoryID && refExt.UsrCAltIDExists == true && objINItemXRef.AlternateType == INAlternateType.Global)
{
objINItemXRef.AlternateID = objCSAnswers.Value.Trim();
Base.itemxrefrecords.Cache.Update(objINItemXRef);
}
}

 

Userlevel 3
Badge

Oh! Also do Base.itemxrefrecords.Update(objINItemXRef) instead of Base.itemxrefrecords.Cache.Update(objINItemXRef).

Also add a line after that as follows:

Base.itemxrefrecords.Cache.Persist(PXDBOperation.Insert);

Let me know if that helps.

 

Sorry about multiple edits, I’ve not tested these code suggestions, I’m just thinking and making suggestions.

Userlevel 7
Badge +11

Hi @nsmith51 ,

Cross reference allows for adding multiple records. So we need to use the for each loop to load all details and add the logic to update the data. Try like below

                       foreach (INItemXRef CrossRef in Base.itemxrefrecords.Select())
                        {

                     // Write the logic to get the values and assign them to the field like below and update the view.

                           CrossRef.AlternateID = objCSAnswers.Value.Trim();

                                Base.itemxrefrecords.Update(CrossRef);
                            }
                        }
 

 

Userlevel 3
Badge

Hi @Raj Gopinathan  Thanks for the replies.

I have received an response from Acumatica team, that AlternateID is a Key so that we need to delete and insert the record instead of updating.

Here is the example.

 

       Base.itemxrefrecords.Current = Base.itemxrefrecords.Search<INItemXRef.inventoryID, INItemXRef.subItemID, INItemXRef.alternateType, INItemXRef.bAccountID, INItemXRef.alternateID>
(objINItemXRef.InventoryID, objINItemXRef.SubItemID, objINItemXRef.AlternateType, objINItemXRef.BAccountID, objINItemXRef.AlternateID);


INItemXRef newCrossRef = new INItemXRef();
newCrossRef.InventoryID = Base.itemxrefrecords.Current.InventoryID;
newCrossRef.AlternateType = Base.itemxrefrecords.Current.AlternateType;
newCrossRef.SubItemID = Base.itemxrefrecords.Current.SubItemID;
newCrossRef.AlternateID = "1217";
newCrossRef.BAccountID = Base.itemxrefrecords.Current.BAccountID;


Base.itemxrefrecords.Delete(Base.itemxrefrecords.Current);
Base.itemxrefrecords.Cache.Insert(newCrossRef);

 

Userlevel 7
Badge

Thank you for sharing the solution @nsmith51 !

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 — 2024  Acumatica, Inc. All rights reserved