0
votes

Please bear with me as I am very new to Acumatica.

In Acumatica (2020.4) we are calling an external REST Web service once a Project is created. The API returns an ID that we want to store in a custom field. However we cannot do this on the Row Persisting as we have queries that run that requires data in the table.

How can we update the field and save that value once the Row Persisted event has been triggered or is there an alternative to calling data in the Row Persisting event?

This is what we have so far, but it only sets the value in the field.

   protected void PMProject_RowPersisted(PXCache cache, PXRowPersistedEventArgs e)
   {
      var row = (PMProject)e.Row;
      // API Class that we call
      APICall.APIResult Rateres = new APICall.APIResult(); 
      Contract ctr = e.Row as Contract;
      ContractExt ctrExt = ctr.GetExtension<ContractExt>();
      Rateres.RateCalc(row.RateTableID);
      cache.SetValue<ContractExt.usrAPIRateID>(ctr, Rateres.contentid);
   }

Thanks in advance.

1
Where the declaration of Rateres?Rick
Apologies. I've updated the code. Rateres is the class we created that calls the API and returns a content ID that we want to save.Dewald Henning

1 Answers

1
votes

The RowPersisted event fires when the data has been written to the database. When doing cache.SetValue, you go back into the cache that was just saved and modify data again. You need to execute a Persist on the cache that you modify to flush that data to the database.

You can set the value and persist it with the following.

Caches[typeof(Contract)].SetValue<ContractExt.usrAPIRateID>(ctr, Rateres.contentid);
Caches[typeof(Contract)].Persist(ctr, PXDBOperation.Update);

I don't work with PMProject and Contract, so I must trust that the rest of your sample is correct when you cast e.Row as both PMProject AND Contract. Inherently, as defined, the data in e.Row is PMProject.

Also, you should consider that putting your code in RowPersisted will cause it to fire every time the record is saved in this graph, even if the data being saved has nothing to do with your custom field.

There are several ways to update the value, and Hugues Beauséjour explains it quite well in his answer on What is the proper way to update values of DAC's retrieved via PXResultset? if the sample provided here does not solve your issue.