Skip to main content
Hello, My requirement is whenever we create a record in Salesforce, it should create an incident in external system populating salesforce fields I'm sending a post request to External webservice using REST API. I'm able to create an incident in external system and send salesforce record fields, how do i update an incident in external system because In trigger i have before insert and before update which creates a duplicate incident on update. I want to update the old incident with new values if there is any change in Salesforce record not create a new incident?

External system fields (Name and Incident group)

Salesforce fields( Multiple fields)

 

public class RadarUpdate {

@future (callout=true)

  public static void postcallout(string Id) {  

  Patient_Satisfaction__c c = [select id, Name,  Description_of_Feedback__c from Patient_Satisfaction__c where Patient_Relation__c ='Referred to Privacy Office' order by         createdDate desc limit 1];

    JSONGenerator gen = JSON.createGenerator(true);

    gen.writeStartObject();

    gen.writeObjectField('Name',c.Name);

    gen.writeObjectField('description',c.Description_of_Feedback__c);

    gen.writeObjectField('incident_group_id',7387);

    gen.writeEndObject();

    String jsonS = gen.getAsString(); 

    System.debug('jsonMaterials'+jsonS);

    Http http = new Http();

    HttpRequest request = new HttpRequest();

    request.setEndpoint('https://api.radarfirst.com/incidents');

    request.setMethod('POST');

    request.setHeader('Content-Type','application/json;charset=UTF-8');

    request.setHeader('User-agent', 'Salesforce-integration-client');

    request.setHeader('Authorization','Bearer  123');

    request.setBody(jsonS);

    // Set the body as a JSON object

    HttpResponse response = http.send(request);

    if (response.getStatusCode() != 201) {

    System.debug('The status code returned was not expected: ' +

        response.getStatusCode() + ' ' + response.getStatus());

    } else {

    System.debug(response.getBody());

    }

    }

}

 
3 answers
  1. Apr 13, 2020, 11:27 AM

    Do this

    if (trigger.isInsert) {

    postcallout(string Id);

    } else if (trigger.isUpdate) {

    putcallout(string id);

    }

    and in your methods, depending on which one, do the following

     

    request.setMethod('POST') OR request.setMethod('PUT);

     
0/9000