+ Start a Discussion
Sammy ShkSammy Shk 

How do I update external system record using Put method?

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);
    String jsonS = gen.getAsString(); 
    Http http = new Http();
    HttpRequest request = new HttpRequest();
    request.setHeader('User-agent', 'Salesforce-integration-client');
    request.setHeader('Authorization','Bearer  123');
    // 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 {

Dhanik L SahniDhanik L Sahni
Hello Sammy,

As you mentioned, when you insert new record then new incident should created in external system and on update in salesforce external system should also updated.

You should call POST method of external system when new record is created. When you are updating record you should call PUT method of that external API. There will be some update api in external system. You should call that API.

@HttpPost - is for creating records.
@HttpPut - is for upsert operation. 

Please try to implement like suggestion. Let me know, if you need any help in implementing. 

Thank You,
Dhanik Lal Sahni
Sammy ShkSammy Shk
Thank for response, I'm new in Apex programming. Can I use Http put in the same code? when i create an incident, it generates an incident Id in response.getBody(), how do i capture that  incident Id and use http put method to update the same incident id whenever update happens on the SF record (Specifically on one field)
Note: I'm mapping Name to Name from salesforce to external system. Name field in salesforce and Created incident Name would be same.

Can you provide me an example using the above code?

Thanks for the help!!
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);