Process_data is called via trigger. This code is working with the exception of when I do the actuall http callout I get the Please commit or rollback before callout out.
Based on similar post the issue seems to be saying that because I am doing a get_data within the callout (since it is doing a read aka select) it is the root cause of the error.
What I am trying to do is after the object is inserted or updated and handled on trigger to call the process data which is working as expected. I check to make sure what environment the code is executing in and set the named_credential I intend to use.
After this check I want to query (select) for the contact data columns I want to process in a for loop where I populate a data strucuture and then serialize it before doing my final endpoint settings and http callout where I encounter the error.
How can I change the code so it will still accomplish what I am trying to do and not get the error.
public class ws_mdm_contact
{
static string Environment = GLOBAL.getEnvironment();
static string Named_Credential;
static string EndPoint;
static string JSON_Payload;
static string outcomeMsg;
static string thisClass = 'ws_mdm_contact';
static List<Contact> lstContact;
public static List<Contact> get_data(Set<ID> ids)
{
List<Contact> lstContact = Helper_MDM.get_Contact(ids);
return lstContact;
}
@future(callout=true)
Public static void process_data(Set<ID> ids)
{
if (Environment == GLOBAL.ENVIRONMENT_PRODUCTION)
{
Named_Credential = GLOBAL.namedCred_Prd;
}
else if (Environment == GLOBAL.ENVIRONMENT_TRAIN)
{
Named_Credential = GLOBAL.namedCred_Train;
}
else if (Environment == GLOBAL.ENVIRONMENT_STAGE)
{
Named_Credential = GLOBAL.namedCred_Stage;
}
else if (Environment == GLOBAL.ENVIRONMENT_DEV)
{
Named_Credential = GLOBAL.namedCred_Fake;
}
else
{
return;
}
lstContact = get_data(ids);
for (Contact Contact_Row : lstContact)
{
if (Contact_Row.Recordtype.DeveloperName == GLOBAL.CONTACT_RECORDTYPE_CONTACT_MANAGEMENT)
{
DataDef_MDM.Contact Contact_Record = New DataDef_MDM.Contact();
Contact_Record.id = Contact_Row.Id;
Contact_Record.firstName = Contact_Row.FirstName;
JSON_Payload = JSON.serializepretty ( Contact_Record );
EndPoint = Named_Credential;
EndPoint += '/api/contact/';
EndPoint += Contact_Row.Id;
EmailManager.sendMail(GLOBAL.EMAIL_DARYN, thisClass + ' > Endpoint: ' + EndPoint + ' > JSON (MDM): ' , JSON_Payload);
outcomeMsg = WS_HTTP.Callout(EndPoint, 'PUT', JSON_Payload);
EmailManager.sendMail(GLOBAL.EMAIL_DARYN, thisClass + ' > outcome message', outcomeMsg);
}
else
{
continue;
}
}
}
}
{}button, or press Ctrl/Cmd-K to format code. – sfdcfox Jul 24 '18 at 19:22