Each Acumatica entity corresponds to a message in this external API system. Each message is basically just a JSON object with certain keys. There are several scenarios, but I’ll stick with a stock item. If someone creates a new stock item, we need to create a JSON object for the 1110 message and send it to this receiving endpoint. For testing right now, I have the message creation attached to actions. When someone creates a new stock item and saves it, the action is called. When someone updates an existing stock item, the action is called. We build the message and then need to populate it with the values from the Acumatica record (currently being done with the export scenario mappings). The message is saved as a string on a custom Acumatica record. An action on this custom record will send the JSON value via the Rest API to the external system. All of this works, but I wanted an easier way to extract the value similar to how Acumatica does in export scenarios.
public PXAction<InventoryItem> Export1110;
PXUIField(DisplayName = "Export 1110", MapViewRights = PXCacheRights.Select, MapEnableRights = PXCacheRights.Update)]
PXButton(CommitChanges = true, VisibleOnDataSource = true)]
public virtual IEnumerable export1110(PXAdapter adapter)
{
if (ItemHelper.shouldPullItem(Base) == true)
{
MWA_1110.export1110Process(null, Base, MWASetup.Current);
}
return adapter.Get();
}
public static void export1110Process(string mode, InventoryItemMaint Base, MWAIntegrationSetup mwaSetup)
{
Dictionary<string, string> dictToSend = new Dictionary<string, string>();
if (mode != null)
{
dictToSend.Add("Mode", mode);
}
DataExtractor extractor = new DataExtractor(Base, mwaSetup);
extractor.prepareData("MWA_1110", dictToSend, null);
}
protected virtual void InventoryItem_RowPersisted(PXCache sender,
PXRowPersistedEventArgs e)
{
if (e.TranStatus == PXTranStatus.Completed)
{
switch (e.Operation.Command())
{
case PXDBOperation.Insert:
if (ItemHelper.shouldPullItem(Base) == true)
{
// “U” corresponds to “Upsert”
MWA_1110.export1110Process("U", Base, MWASetup.Current);
}
break;
………………………………..
public void prepareData(string objectName, Dictionary<string, string> extraVals, object currentRecord)
{
// Find export scenario with name “MWA_1110”
SYMapping map = PXSelect<SYMapping, Where<SYMapping.name, Equal<Required<SYMapping.name>>>>.Select(graph, objectName);
if (map != null)
{
// get all mapping details for current scenario
var mapDetails = PXSelect<SYMappingField, Where<SYMappingField.mappingID, Equal<Required<SYMappingField.mappingID>>>, OrderBy<Asc<SYMappingField.orderNumber>>>.Select(graph, map.MappingID);
foreach (SYMappingField d in mapDetails)
{
if (d.IsVisible == true)
{
// call function that extracts value from currentRecord based on the mappings we have set up
string translatedVal = getValueFromText(d.ObjectNameHidden, d.FieldNameHidden, currentRecord, map.ScreenID);
if (!dictToSend.ContainsKey(d.Value))
{
dictToSend.Add(d.Value, translatedVal);
}
}
}
}
CreateJSONRequest(dictToSend, messageType);
………………………………………………………
public void CreateJSONRequest(Dictionary<string, string> dictToSend, string messageType)
{
var json = JsonConvert.SerializeObject(dictToSend, Newtonsoft.Json.Formatting.Indented);
MWATestRequestMaint trMaint = PXGraph.CreateInstance<MWATestRequestMaint>();
MWARequest tr = new MWARequest();
tr.RequestJsonData = json;
tr.MessageType = messageType;
trMaint.TestRequest.Insert(tr);
trMaint.Actions.PressSave();
if (MWASetup.AutoExport == true)
{
trMaint.Export.Press();
// export to external Rest API, save response and mark as successful/failed for further processing
}
}
….