Skip to main content
Solved

Explicit save with a RowPersisited Event

  • 1 July 2024
  • 2 replies
  • 54 views

For an integration, we are triggering a API by the InventoryItem_RowPersisted event handler when creating a NonStock item in Acumatica. By the response JSON some essential statuses are saved and maintained in custom fields as shown in the below diagram.

After creating a NonStock item and when clicking the save button these two custom fields get updated. But right after the screen refreshes or closes the screen the values get lost without saving the updated values of this custom fields. I have already inserted a explicit save, but it seems to be not functioning as expected. Here with I'm attaching essential segments of the NonStockItemMaint graph. If you guys can provide any guidance in this regard, I would greatly appreciate it. Thank you. For explicit saving i tried this “

                        // Mark the extension fields as updated
                        PXCache caches = Base.CachesXtypeof(InventoryItem)];
                        caches.Update(row);
                        caches.Persist(row,PXDBOperation.Update);”  and i tried as this also.

”// Mark the extension fields as updated
                        cache.SetValueExt<InventoryItemExt.usrSFRemark>(row, ext.UsrSFRemark);
                        cache.SetValueExt<InventoryItemExt.usrSFMessage>(row, ext.UsrSFMessage);
                        cache.Update(row);
                        cache.Persist(PXDBOperation.Update);”

public class NonStockItemMaint_Extension: PXGraphExtension < PX.Objects.IN.NonStockItemMaint > {

public PXSelect<InventoryItemCurySettings,
Where<InventoryItemCurySettings.inventoryID, Equal<Current<InventoryItem.inventoryID>>>> CurySettings;

public PXSelect<INItemClass,
Where<INItemClass.itemClassID, Equal<Current<InventoryItem.itemClassID>>>> ItemClassView;

protected void InventoryItem_RowPersisted(PXCache cache, PXRowPersistedEventArgs e) {
var row = (InventoryItem) e.Row;
if (row == null) return;

// Retrieve the DAC extension
var ext = PXCache < InventoryItem > .GetExtension < InventoryItemExt > (row);

PXTrace.WriteInformation(row.InventoryCD.ToString());


// InventoryItem items = item.current;

// Retrieve the ItemClassCD value using the global view
var itemClass = ItemClassView.SelectSingle();
if (itemClass == null) {
PXTrace.WriteInformation("ItemClassView is null");
return;
}
string itemClassCD = itemClass ? .ItemClassCD;

// Extract the first 3 characters of the itemClassCD
string firstThreeChars = itemClassCD ? .Substring(0, 3);

// Initialize the appropriate Root object based on firstThreeChars
object obj;
if (firstThreeChars == "ST2") {
obj = new RootST2 {};
} else if (firstThreeChars == "ST3") {
obj = new RootST3 {};
} else {
throw new PXException("Unsupported item class prefix");
}

// Populate the PrcEntries list
foreach(InventoryItemCurySettings setting in CurySettings.Select()) {
((dynamic) obj).prcEntries.Add(new PrcEntries {
ProductPrice = setting.BasePrice ? .ToString(),
currencyName = "USD"
});
}

List < object > list = new List < object > { obj};
JavaScriptSerializer serializer = new JavaScriptSerializer();
var json = serializer.Serialize(list);
PXTrace.WriteInformation(json);

if (ext.UsrSFRemark == null || ext.UsrSFRemark == "NonStock Item Creation Failed") {
try {

}

var httpResponsePost = (HttpWebResponse) httpRequestPost.GetResponse();
using(var streamReaderPost = new StreamReader(httpResponsePost.GetResponseStream())) {
PXTrace.WriteInformation(httpResponsePost.StatusCode.ToString());
var responseText = streamReaderPost.ReadToEnd();
var responseJson = Newtonsoft.Json.JsonConvert.DeserializeObject(responseText);
PXTrace.WriteInformation(responseText);

var responseParsed = Newtonsoft.Json.JsonConvert.DeserializeObject < Response/] > (responseJson.ToString());
if (httpResponsePost.StatusCode.ToString() == "OK" && responseParsed<0].productId != null) {
ext.UsrSFRemark = "NonStock Item Created";
ext.UsrSFMessage = responseParsed.0].message;
} else {
ext.UsrSFRemark = "NonStock Item Creation Failed";
ext.UsrSFMessage = responseParsedm0].message;
}

// Mark the extension fields as updated
PXCache caches = Base.Caches typeof(InventoryItem)];
caches.Update(row);
caches.Persist(row,PXDBOperation.Update);

}
httpResponsePost.Close();
} catch (WebException ex) {
PXTrace.WriteInformation(((HttpWebResponse) ex.Response).StatusCode.ToString());
using(var streamReaderPost = new StreamReader(((HttpWebResponse) ex.Response).GetResponseStream())) {
var responseText = streamReaderPost.ReadToEnd();
var responseJson = Newtonsoft.Json.JsonConvert.DeserializeObject(responseText);
PXTrace.WriteInformation(responseText);

var responseParsed = Newtonsoft.Json.JsonConvert.DeserializeObject < Response/] > (responseJson.ToString());

ext.UsrSFRemark = "NonStock Item Creation Failed";
ext.UsrSFMessage = responseParsedS0].message;

// Mark the extension fields as updated
PXCache caches = Base.Caches typeof(InventoryItem)];
caches.Update(row);
caches.Persist(row,PXDBOperation.Update);
}
}
}
}

 

2 replies

Userlevel 3
Badge +2

Hi @malinthawarnakulasooriya08 

Since you need to set fields and save them, I think you need to move your code from InventoryItem_RowPersisted to InventoryItem_RowPersisting.

RowPersisted is executed after the DB operations cycle and is not the correct place to update values you need to store in the database. By using RowPersisitng you can update the row directly and the values will get saved automatically without any additional work.

 

Userlevel 4
Badge +1

Thank you so much @Marco Villasenor for the valuable reminder. Earlier I missed that point.

Reply