Skip to main content
Answer

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

  • April 29, 2022
  • 8 replies
  • 785 views

Forum|alt.badge.img

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;
}
}

 

Best answer by nsmith51

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);

 

8 replies

Forum|alt.badge.img

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);


Forum|alt.badge.img
  • Author
  • Jr Varsity III
  • April 29, 2022

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


Forum|alt.badge.img

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


Forum|alt.badge.img
  • Author
  • Jr Varsity III
  • April 29, 2022

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);
}
}

 


Forum|alt.badge.img

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.


jinin
Pro I
Forum|alt.badge.img+11
  • Pro I
  • May 2, 2022

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);
                            }
                        }
 

 


Forum|alt.badge.img
  • Author
  • Jr Varsity III
  • Answer
  • May 2, 2022

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);

 


Chris Hackett
Community Manager
Forum|alt.badge.img
  • Acumatica Community Manager
  • May 2, 2022

Thank you for sharing the solution @nsmith51 !