0

I have developed a trigger and apex class, now what I want is that whenever a field is updated in salesforce object I want to update a similar field in the 3rd party application.

Trigger:

trigger FieldUpdateTrigger on Contact (after update) {    
        Set<Id> contactIds = new set<Id>();
        for(Contact Values : Trigger.new) {
                Contact oldValues = Trigger.oldMap.get(Values.Id);
                if(Values != oldValues) {
                    ContactIds.add(values.id);
                }
        }
        if (!ContactIds.isEmpty()){
        FieldUpdateCallouts.UpdateFieldOnExternalServer(ContactIds);
        }  
}

Future Method Class:

public class FieldUpdateCallouts {
    @future (callout=true)
    public static void UpdateFieldOnExternalServer(Set<Id> ContactIds){
    List<Contact> ConList = new List<Contact>();
    Http http = new Http();

        HttpRequest req = new HttpRequest();
        req.setMethod('POST');
        req.setHeader('Content-Type', 'application/x-www-form-urlencoded');
        String username='';
        String password='';
        Blob headerValue = Blob.valueOf(username +':' +password);
        String authorizationHeader = 'Basic ' + EncodingUtil.base64Encode(headerValue);
        req.setHeader('Authorization', authorizationHeader);
        String targetString ='{"str": "Newcon.get(str)"}';
        String encodedString = EncodingUtil.urlEncode(targetString,'UTF-8');
        req.setBody('encodedString');
        req.setEndpoint('/admin/api');    
        HttpResponse res = new HttpResponse();
        res = http.send(req);    
}

1 Answer 1

1

You have a number of problems with your code. Your first problem is this:

if(Values != oldValues) {

I can almost certainly guarantee that this will be true, because the LastModifiedDate field should have been updated by the database at this point.

Instead, you'll want to probably clone and clear audit fields, like this:

for(Integer index = 0, size = Trigger.new.size(); index < size; index++) {
  Contact oldValue = Trigger.old[index].clone(true, false, false, true),
          newValue = Trigger.new[index].clone(true, false, false, true);
  if(oldValue != newValue) {
    contactIds.add(newValue.Id);
  }
}

See the SObject class for more details on how clone works. The first parameter preserves the Id field, the second parameter indicates a shallow clone (faster), the third parameter clears audit fields, and the fourth parameter preserves auto-number fields.

Now, once in your future method, you need to query your records again:

public class FieldUpdateCallouts {
  @future (callout=true) public static void UpdateFieldOnExternalServer(Set<Id> ContactIds){
    List<Contact> ConList = [SELECT ... FROM Contact WHERE Id = :contactIds];

Since you're comparing all fields, you might want to create a dynamic query. There's plenty of other answers here on the site that provide examples on how to do this, so I won't include that here.

The next step is to loop over this list:

for(Contact record: conList) {
  Http http = new Http();
  HttpRequest req = new HttpRequest();
  req.setMethod('POST');
  // Did you mean this, or application/json?
  req.setHeader('Content-Type', 'application/x-www-form-urlencoded');
  String username='';
  String password='';
  Blob headerValue = Blob.valueOf(username +':' +password);
  String authorizationHeader = 'Basic ' + EncodingUtil.base64Encode(headerValue);
  req.setHeader('Authorization', authorizationHeader);
  // Serialize your data somehow
  String targetString = JSON.serialize(record);
  String encodedString = EncodingUtil.urlEncode(targetString,'UTF-8');
  // Don't use quotes to pass a variable
  req.setBody(encodedString);
  // Remember to include https://servername
  req.setEndpoint('/admin/api');
  HttpResponse res = new HttpResponse();
  res = http.send(req);    
}

Finally, to make your life easier, you might want to use a Named Credential, which allows you to specify the server's authentication via configuration instead of hard-coding a value in your code.

That simplifies the your callout part to simply:

  Http http = new Http();
  HttpRequest req = new HttpRequest();
  req.setMethod('POST');
  // Did you mean this, or application/json?
  req.setHeader('Content-Type', 'application/x-www-form-urlencoded');
  // Serialize your data somehow
  String targetString = JSON.serialize(record);
  String encodedString = EncodingUtil.urlEncode(targetString,'UTF-8');
  // Don't use quotes to pass a variable
  req.setBody(encodedString);
  req.setEndpoint('callout:ServerName/admin/api');
  HttpResponse res = new HttpResponse();
  res = http.send(req);    

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.