• Sammy Shk
  • NEWBIE
  • 10 Points
  • Member since 2019

  • Chatter
    Feed
  • 0
    Best Answers
  • 2
    Likes Received
  • 0
    Likes Given
  • 9
    Questions
  • 5
    Replies
Newbie in apex, I have a list of collection of strings (Email addrsses) in a flow, i want to sort this list with alphabetically. Since flow supports collection sorts only asc or desc, i have decided to use invocable apex to input this collection and sort it alphabetically and pass the list back to flow? How do i sort Collection of strings in invocable apex
Thanks
global class SortCollectionOfStrings {
    
 @InvocableMethod(label='Sort String Collection')    
    global static void SortCollectionOfStrings(list<string> FinalistofEmailcollection){
       FinalistofEmailcollection.sort();
 }
}

 
Hello, 

I'm new in apex and trying to run a batch apex. We have 2 objects, obj1 and obj2 and they have master detail relationship. We also have a End date time on obj1 and obj2 There could be multiple obj2 records related to obj1 , we want to run a batch that looks up all records in  Obj2 which has more than 2 records related to obj1 and update the old ones with the End date time field from obj1. Is there any sample code that I can start with?
New in Apex, Trying to update No.of total reviews and no.of open reviews on Patient_event__c from child records(Root_Cause_Analysis__c). The code works fine however when i run the test code, it throws error "System.LimitException: Too many SOQL queries: 101".

Trigger CommitteeEventReviewTrigger on Root_Cause_Analysis__c(After insert, After Update, After Delete,After Undelete)
{
  Set<Id> setSEIds = new Set<Id>();
  if(Trigger.isInsert || Trigger.isUndelete || Trigger.isUpdate )
  {
   for(Root_Cause_Analysis__c CER : Trigger.new)
   {
    setSEIds.add(CER.Patient_Safety_Incident__c);
   }
  }
  
  if(Trigger.isDelete)
  {
  
   //To fix System.NullPointerException:Attempt to de-reference a null object
   for(Root_Cause_Analysis__c CER : Trigger.old) 
   {
    setSEIds.add(CER.Patient_Safety_Incident__c);
   }
  }
   
 List<Patient_Event__c> listSE = [Select id,name,No_of_Commitee_Event_Reviews__c ,(Select id from Root_Cause_Analysis__r) from Patient_Event__c where Id in : setSEIds];
    for(Patient_Event__c SE :listSE)
    {
   SE.No_of_Commitee_Event_Reviews__c = SE.Root_Cause_Analysis__r.size();
  }
  update listSE;
    List<Patient_Event__c> listSEStatusOpen = [Select id,name,No_of_Open_Committee_event_Review__c,(Select id from Root_Cause_Analysis__r where Status__c ='New' OR Status__c ='Reopened' OR Status__c ='Investigation') from Patient_Event__c where Id in : listSE];
     for(Patient_Event__c SEOpen :listSEStatusOpen) 
     {
      SEOpen.No_of_Open_Committee_event_Review__c = SEOpen.Root_Cause_Analysis__r.size();   
     }
    update listSEStatusOpen;
}
 
Hello I'm trying to write a test class for Rest API post Method which i wrote, need help in writing test method. This gives me System.DmlException: Update failed. First exception on row 0; first error: MISSING_ARGUMENT, Id not specified in an update call:". Please help
trigger RadarFirst on Patient_Satisfaction__c (after update) {
for (Patient_Satisfaction__c cc : Trigger.new){
if(cc.Patient_Relation__c!= Trigger.oldMap.get(cc.id).Patient_Relation__c && cc.Patient_Relation__c != null) {
      if(cc.Patient_Relation__c.contains('Referred to Privacy Office')){
      RadarUpdate.postcallout(cc.id);
      }
  }
}
}
 
public class RadarUpdate {
    @future (callout=true)
	public static void postcallout(string Id) { 
	Patient_Satisfaction__c c = [select id, Name, Reporter_Phone__c,Reporter_First_Name__c,Reporter_Last_Name__c, Reporter_Email__c,
    Description_of_Feedback__c from Patient_Satisfaction__c where Patient_Relation__c ='Referred to Privacy Office' order by lastmodifiedDate desc limit 1];
    JSONGenerator gen = JSON.createGenerator(true);
	gen.writeStartObject();
	gen.writeObjectField('name', c.Name);
	gen.writeObjectField('incident_group_id', 7387);
    gen.writeObjectField('description',c.Description_of_Feedback__c);
	gen.writeFieldName('submitted_by');
	gen.writeStartObject();
	gen.writeStringField('given_name',c.Reporter_First_Name__c);
	gen.writeStringField('surname', c.Reporter_Last_Name__c);
	gen.writeStringField('phone',c.Reporter_Phone__c);
    gen.writeStringField('email',c.Reporter_Email__c);
	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());
	}
    }
}
@isTest
public class RadarUpdateTest{

    static testMethod void  postcalloutTest(){
        Account ac = new Account();
        ac.Name= 'Test';
        insert ac;
        
        Contact con= new Contact();
        con.AccountId= ac.Id;
        con.FirstName ='First Name';
        con.LastName = 'Test Contact';
        con.Phone= '7896541233';
        con.Email= 'Test@123.com';
        con.RUSH_Email__c= 'Test@123.com';
        insert con;
         		
        Patient_Satisfaction__c Pr = new Patient_Satisfaction__c();
        Pr.Primary_Campus__c = 'a1pP0000001lfaKIAQ';
        Pr.Primary_Facility__c= 'a1pP00000022FLRIA2';
        Pr.Primary_Location__c= 'a1pP00000022FLcIAM';
        Pr.Reporter__c=con.Id;
        Pr.Reporter_Phone__c=con.Id;
        Pr.Reporter_Email__c= 'Test@123.com';
        Pr.Description_of_Feedback__c= 'Testing Privacy office';
        Pr.Patient_Relation__c = 'Referred to Privacy Office';
        
     update Pr;
  	 RadarUpdate reqst=new RadarUpdate();
   	 String JsonMsg=JSON.serialize(reqst);
     Test.startTest();

    RestRequest req = new RestRequest(); 
    RestResponse res = new RestResponse();

    req.requestURI = '/services/apexrest/DemoUrl';  //Request URL
    req.httpMethod = 'POST';//HTTP Request Type
    req.requestBody = Blob.valueof(JsonMsg);
    RestContext.request = req;
    RestContext.response= res;
    RadarUpdate.postcallout(Pr.Id);
  
    update Pr;
    Test.stopTest();

   }
}

 
Hello,

I'm new in Apex, I'm finding it difficult to write test class for Http Post, I'm sending some values from Salesforce to external system however I'm not sure how to write test class for this scenario. I did created test records but don't  know how to add Http post Test class.

Can someone help me how to add Http post test class in the test class which i created?
 
public class RadarUpdate {
    @future (callout=true)
	public static void postcallout(string Id) { 
	Patient_Satisfaction__c c = [select id, Name, Reporter_Phone__c,Reporter_First_Name__c,Reporter_Last_Name__c, Reporter_Email__c,
    Description_of_Feedback__c from Patient_Satisfaction__c where Patient_Relation__c ='Referred to Privacy Office' order by lastmodifiedDate desc limit 1];
    JSONGenerator gen = JSON.createGenerator(true);
	gen.writeStartObject();
	gen.writeObjectField('name', c.Name);
	gen.writeObjectField('incident_group_id', 7387);
       gen.writeObjectField('description',c.Description_of_Feedback__c);
	gen.writeFieldName('submitted_by');
	gen.writeStartObject();
	gen.writeStringField('given_name',c.Reporter_First_Name__c);
	gen.writeStringField('surname', c.Reporter_Last_Name__c);
	gen.writeStringField('phone',c.Reporter_Phone__c);
        gen.writeStringField('email',c.Reporter_Email__c);
	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());
	}
    }
}
 
@isTest
private class RadarUpdateTest {
    @isTest  public static void CreatePR(){
        
        rkl__RK_Hierarchy_Node__c Hr = new rkl__RK_Hierarchy_Node__c();
        Hr.Name= 'Hieararchy';
        Hr.rkl__Node_Level__c = 1.0;
        Hr.rkl__Node_Name__c = 'Node Name';
        insert Hr;
        
        Account ac = new Account();
        ac.Name= 'Test';
        insert ac;
        
        Contact con= new Contact();
        con.AccountId= ac.Id;
        con.FirstName ='First Name';
        con.LastName = 'Test Contact';
        con.Phone= '7896541233';
        con.Email= 'Test@123.com';
        con.RUSH_Email__c= '';
        insert con;       	
		
        Patient_Satisfaction__c Pr = new Patient_Satisfaction__c();
        Pr.Primary_Campus__c = Hr.Id;
        Pr.Primary_Facility__c= Hr.Id;
        Pr.Primary_Location__c= Hr.Id;
        Pr.Reporter__c=con.Id;
        Pr.Reporter_Phone__c=con.Id;
        Pr.Reporter_Email__c=con.Id;
        Pr.Description_of_Feedback__c= 'Testing Privacy office';
        Pr.Patient_Relation__c = 'Referred to Privacy Office';
        
        insert Pr;
        update Pr;
            
        
    }

}

 
I'm trying to generate Json in following format
{
   "name":"TestAPIFromTrigger",
   "discovered":null,
   "occurred_on":null,
   "group":{
      "id":7387,
      "name":"Default Group"
   },
   "description":"",
   "channel":{
      "source":"API"
   },
   "submitted_by":{
      "given_name":"First Name",
      "surname":"Last Name",
      "email":"SSS@edu",
      "phone":"78965555"
   }

Apex code

Patient_Satisfaction__c c = [select id, Name, Reporter_Phone__c, Description_of_Feedback__c from Patient_Satisfaction__c where Patient_Relation__c ='Referred to Privacy Office' order by lastmodifiedDate desc limit 1];
    JSONGenerator gen = JSON.createGenerator(true);
    gen.writeStartObject();
    gen.writeObjectField('Name',c.Name);
    gen.writeObjectField('incident_group_id',7387);
    gen.writeObjectField('description',c.Description_of_Feedback__c);
    gen.writeStartObject();
    gen.writeFieldName('submitted_by');
    gen.writeStartArray();
    gen.writeObjectField('phone',c.Reporter_Phone__c);
    gen.writeEndArray();
    gen.writeEndObject();
    String jsonS = gen.getAsString(); 
    System.debug('jsonMaterials'+jsonS)
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());
    }
    }
}

 
I'm new in apex I tried my best. Here is my requirement We are sending a Post request to External system and we want salesforce fields to go to external system along with external system required fields(Name and Incident_group_id). I'm not able to conctenate Salesforce fields along with external system required field in Json format. Not sure what is wrong in the code. I'm calling this code in trigger. sorry for messy code, still trying to figure out apex. It doesn't send Json object (Patient satisfaction) it just sends incident_group_id and Name(both external system fields)

public class RadarUpdate {
@future (callout=true)
public static void postcallout(string id) { 
List<Patient_Satisfaction__c> PrIds = new list<Patient_Satisfaction__c>();
List<Patient_Satisfaction__c> PrRecord  = [select id, Name, Reporter_First_Name__c, Reporter_Last_Name__c, 
Reporter_Phone__c, Description_of_Feedback__c from Patient_Satisfaction__c where id IN :PrIds ] ;
JSONGenerator gen = JSON.createGenerator(true);
gen.writeStartObject();
gen.writeFieldName('Patient satisfaction');
gen.writeStartArray();
for (Patient_Satisfaction__c  patientSatisfaction : PrRecord) {
gen.writeStartObject();
gen.writeObjectField('First name', patientSatisfaction.Reporter_First_Name__c);
gen.writeObjectField('Last name', patientSatisfaction.Reporter_Last_Name__c);
gen.writeObjectField('Phone', patientSatisfaction.Reporter_Phone__c);
gen.writeObjectField('description', patientSatisfaction.Description_of_Feedback__c);gen.writeEndObject();
     } 
gen.writeEndArray();
gen.writeObjectField('incident_group_id',7387);
gen.writeObjectField('Name', 'TestAPI');
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 abc');
request.setBody(jsonS);
// Set the body as a JSON object
HttpResponse response = http.send(request);
if (response.getStatusCode() != 422) {
System.debug('The status code returned was not expected: ' +
response.getStatusCode() + ' ' + response.getStatus());
} else {
System.debug(response.getBody());
}
}
}
 
We have two objects Patient Object and Review Object which is master-detail relationship. I have 2 Picklist fields called Feedback one on Master Patient object and one on Child Review Object. I want to Auto Populate Patient's Feedback with a value " Grievance" if one or more Review Feedback is "Greivance" . Populate it as "Complaint" if Complaint is available (when Grievance is not selected in Review) and if Complaint or grievance is not selected, populate it as "Compliment. 
The order has to Grievance>Complaint> Compliment.
Grievance always wins.

Thanks
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());
    }
    }
}

 
I'm new in apex I tried my best. Here is my requirement We are sending a Post request to External system and we want salesforce fields to go to external system along with external system required fields(Name and Incident_group_id). I'm not able to conctenate Salesforce fields along with external system required field in Json format. Not sure what is wrong in the code. I'm calling this code in trigger. sorry for messy code, still trying to figure out apex. It doesn't send Json object (Patient satisfaction) it just sends incident_group_id and Name(both external system fields)

public class RadarUpdate {
@future (callout=true)
public static void postcallout(string id) { 
List<Patient_Satisfaction__c> PrIds = new list<Patient_Satisfaction__c>();
List<Patient_Satisfaction__c> PrRecord  = [select id, Name, Reporter_First_Name__c, Reporter_Last_Name__c, 
Reporter_Phone__c, Description_of_Feedback__c from Patient_Satisfaction__c where id IN :PrIds ] ;
JSONGenerator gen = JSON.createGenerator(true);
gen.writeStartObject();
gen.writeFieldName('Patient satisfaction');
gen.writeStartArray();
for (Patient_Satisfaction__c  patientSatisfaction : PrRecord) {
gen.writeStartObject();
gen.writeObjectField('First name', patientSatisfaction.Reporter_First_Name__c);
gen.writeObjectField('Last name', patientSatisfaction.Reporter_Last_Name__c);
gen.writeObjectField('Phone', patientSatisfaction.Reporter_Phone__c);
gen.writeObjectField('description', patientSatisfaction.Description_of_Feedback__c);gen.writeEndObject();
     } 
gen.writeEndArray();
gen.writeObjectField('incident_group_id',7387);
gen.writeObjectField('Name', 'TestAPI');
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 abc');
request.setBody(jsonS);
// Set the body as a JSON object
HttpResponse response = http.send(request);
if (response.getStatusCode() != 422) {
System.debug('The status code returned was not expected: ' +
response.getStatusCode() + ' ' + response.getStatus());
} else {
System.debug(response.getBody());
}
}
}
 
Hello, 

I'm new in apex and trying to run a batch apex. We have 2 objects, obj1 and obj2 and they have master detail relationship. We also have a End date time on obj1 and obj2 There could be multiple obj2 records related to obj1 , we want to run a batch that looks up all records in  Obj2 which has more than 2 records related to obj1 and update the old ones with the End date time field from obj1. Is there any sample code that I can start with?
New in Apex, Trying to update No.of total reviews and no.of open reviews on Patient_event__c from child records(Root_Cause_Analysis__c). The code works fine however when i run the test code, it throws error "System.LimitException: Too many SOQL queries: 101".

Trigger CommitteeEventReviewTrigger on Root_Cause_Analysis__c(After insert, After Update, After Delete,After Undelete)
{
  Set<Id> setSEIds = new Set<Id>();
  if(Trigger.isInsert || Trigger.isUndelete || Trigger.isUpdate )
  {
   for(Root_Cause_Analysis__c CER : Trigger.new)
   {
    setSEIds.add(CER.Patient_Safety_Incident__c);
   }
  }
  
  if(Trigger.isDelete)
  {
  
   //To fix System.NullPointerException:Attempt to de-reference a null object
   for(Root_Cause_Analysis__c CER : Trigger.old) 
   {
    setSEIds.add(CER.Patient_Safety_Incident__c);
   }
  }
   
 List<Patient_Event__c> listSE = [Select id,name,No_of_Commitee_Event_Reviews__c ,(Select id from Root_Cause_Analysis__r) from Patient_Event__c where Id in : setSEIds];
    for(Patient_Event__c SE :listSE)
    {
   SE.No_of_Commitee_Event_Reviews__c = SE.Root_Cause_Analysis__r.size();
  }
  update listSE;
    List<Patient_Event__c> listSEStatusOpen = [Select id,name,No_of_Open_Committee_event_Review__c,(Select id from Root_Cause_Analysis__r where Status__c ='New' OR Status__c ='Reopened' OR Status__c ='Investigation') from Patient_Event__c where Id in : listSE];
     for(Patient_Event__c SEOpen :listSEStatusOpen) 
     {
      SEOpen.No_of_Open_Committee_event_Review__c = SEOpen.Root_Cause_Analysis__r.size();   
     }
    update listSEStatusOpen;
}
 
Hello,

I'm new in Apex, I'm finding it difficult to write test class for Http Post, I'm sending some values from Salesforce to external system however I'm not sure how to write test class for this scenario. I did created test records but don't  know how to add Http post Test class.

Can someone help me how to add Http post test class in the test class which i created?
 
public class RadarUpdate {
    @future (callout=true)
	public static void postcallout(string Id) { 
	Patient_Satisfaction__c c = [select id, Name, Reporter_Phone__c,Reporter_First_Name__c,Reporter_Last_Name__c, Reporter_Email__c,
    Description_of_Feedback__c from Patient_Satisfaction__c where Patient_Relation__c ='Referred to Privacy Office' order by lastmodifiedDate desc limit 1];
    JSONGenerator gen = JSON.createGenerator(true);
	gen.writeStartObject();
	gen.writeObjectField('name', c.Name);
	gen.writeObjectField('incident_group_id', 7387);
       gen.writeObjectField('description',c.Description_of_Feedback__c);
	gen.writeFieldName('submitted_by');
	gen.writeStartObject();
	gen.writeStringField('given_name',c.Reporter_First_Name__c);
	gen.writeStringField('surname', c.Reporter_Last_Name__c);
	gen.writeStringField('phone',c.Reporter_Phone__c);
        gen.writeStringField('email',c.Reporter_Email__c);
	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());
	}
    }
}
 
@isTest
private class RadarUpdateTest {
    @isTest  public static void CreatePR(){
        
        rkl__RK_Hierarchy_Node__c Hr = new rkl__RK_Hierarchy_Node__c();
        Hr.Name= 'Hieararchy';
        Hr.rkl__Node_Level__c = 1.0;
        Hr.rkl__Node_Name__c = 'Node Name';
        insert Hr;
        
        Account ac = new Account();
        ac.Name= 'Test';
        insert ac;
        
        Contact con= new Contact();
        con.AccountId= ac.Id;
        con.FirstName ='First Name';
        con.LastName = 'Test Contact';
        con.Phone= '7896541233';
        con.Email= 'Test@123.com';
        con.RUSH_Email__c= '';
        insert con;       	
		
        Patient_Satisfaction__c Pr = new Patient_Satisfaction__c();
        Pr.Primary_Campus__c = Hr.Id;
        Pr.Primary_Facility__c= Hr.Id;
        Pr.Primary_Location__c= Hr.Id;
        Pr.Reporter__c=con.Id;
        Pr.Reporter_Phone__c=con.Id;
        Pr.Reporter_Email__c=con.Id;
        Pr.Description_of_Feedback__c= 'Testing Privacy office';
        Pr.Patient_Relation__c = 'Referred to Privacy Office';
        
        insert Pr;
        update Pr;
            
        
    }

}

 
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());
    }
    }
}