I am new to the language used in salesforce Apex Triggers:
I'm listening to change on a field and sending the current and previous data values to an external API. But I am not able to set the custom fields to send it to the HTTPRequest method present in ContactUpdateToExternalService
class with method callApi
. I don't want these properties to be Custom fields on SObject. I want to use it just for the current instance of trigger execution.
trigger ContactUpdate on Contact (after update) {
for (Contact contact : Trigger.New) {
if (trigger.newMap.get(contact.Id).AccountId != trigger.oldMap.get(contact.Id).AccountId) {
Map<Object, Object> data = new Map<Object, Object>contact;
data.previousAccountId = trigger.oldMap.get(contact.Id).AccountId;
data.currentAccountId = trigger.newMap.get(contact.Id).AccountId;
ContactUpdateToExternalService.callApi(JSON.serialize(data));
}
}
}
The data values are sent to external API services.
I don't want these properties added to Contact
sObject, when I use the same trigger for before update.