0

I'm a beginner in webservices and future calls. I have been trying post parameters to an external system (ServiceNow). As of now I'm able to post a generic description field from Salesforce to ServiceNow.

Here's what I have tried so far..

trigger Case2ServiceNow on Case (after insert) {
for(Case c : [Select Id, RecordTypeId, RecordType.Name,Subject From Case Where Id IN : Trigger.new]) {
if(c.RecordType.Name == 'Incident'){
    AuthCalloutPost.basicAuthCalloutPost();
  //  uniqueNames.add(c.Subject);
   }
else{
    System.debug('##false##');
 }
}   
}

here's the class...

public class AuthCalloutPost{
public String Response { get; set;}

@future(callout=true)

public static void basicAuthCalloutPost(String names){
HttpRequest req = new HttpRequest();
req.setEndpoint('xxx/api/now/table/incident');
req.setMethod('POST');
String postData = '{\"short_description\":\" Posting from Salesforce.com case trigger\"}';
req.setBody(postData);
req.setTimeout(120000);

 String authorizationHeader = 'Basic xyz=';
 req.setHeader('Authorization', authorizationHeader);
 req.setHeader('Accept', 'application/json');
 req.setHeader('Content-Type', 'application/json'); 
 req.setHeader('Accept', 'application/json');

 Http http = new Http();
 HTTPResponse res = http.send(req);
 System.debug(res.getBody());
 // this.response=res.getBody();

  }
  }

I'm trying to post Salesforce Case parameters to the ServiceNow like name, description. I would really appreciate if I'm pointed to the right direction. Thanks in advance.

UPDATE: when I comment //req.setBody(requestbody); and post this instead, it works! String postData = '{\"short_description\":\"Posting a Sentence\"}'; req.setBody(postData); I'm still not able to figure out the issue in Json generation.

1

You can't pass sObjects in future method so you will pass all the object ids and after that you can query data inside future method and generate your JSON which you will pass to your external service so your code can be modified in following way

  1. Trigger code

    trigger Case2ServiceNow on Case (after insert) {
      AuthCalloutPost.basicAuthCalloutPost(Trigger.newmap.keyset());
    }
    
  2. Your class code

    public class AuthCalloutPost{
    public String Response { get; set;}
    
    @future(callout=true)
    
     public static void basicAuthCalloutPost(set<id> caseids){
    
     //call to generate request body
     String requestbody = generateBody(caseids);
    
     HttpRequest req = new HttpRequest();
     req.setEndpoint('xxx/api/now/table/incident');
     req.setMethod('POST');
    
     req.setBody(requestbody);
     req.setTimeout(120000);
    
     String authorizationHeader = 'Basic xyz=';
     req.setHeader('Authorization', authorizationHeader);
     req.setHeader('Accept', 'application/json');
     req.setHeader('Content-Type', 'application/json'); 
     req.setHeader('Accept', 'application/json');
    
     Http http = new Http();
     HTTPResponse res = http.send(req);
     System.debug(res.getBody());
     // this.response=res.getBody();
    
     }
    
     //This method will generate request body.
     public static String generateBody(set<id> casesids)
     {
           JSONGenerator gen = JSON.createGenerator(true);
           gen.writeStartObject();
           gen.writeFieldName('Cases');
           gen.writeStartArray();
           for(Case c : [Select Id, RecordTypeId, RecordType.Name,Subject       
             From Case Where Id IN : casesids]) {
             if(c.RecordType.Name == 'Incident'){
               //define different fields which you want to pass.
               gen.writeStartObject();
               gen.writeStringField('RecordTypeName', c.RecordType.Name);  
               gen.writeStringField('short_description',c.Subject);
               gen.writeStringField('RecordTypeId',c.RecordTypeId);
               gen.writeEndObject();   
             } 
    
          }  
               gen.writeEndArray();
               gen.writeEndObject();
               return gen.getAsString(); 
     }
    
    }
    
  • I just tried your code. It throws me this error "Error: Compile Error: Method does not exist or incorrect signature: generateBody(Set<Id>) at line 9 column 23" – MnZ Nov 13 '15 at 7:48
  • deefine generateBody method as static. public static String generateBody(set<id> casesids) – Himanshu Nov 13 '15 at 7:49
  • Thanks that worked. Isn't trigger.new is a list? I get incorrect signature on trigger....Error: Compile Error: Method does not exist or incorrect signature: [List<Case>].keyset() Do I need to convert that to a set and then pass it to basicAuthCalloutPost? – MnZ Nov 13 '15 at 8:10
  • aah sorry is my bad. I did typo it supposed to be Trigger.newmap.keyset() – Himanshu Nov 13 '15 at 8:15
  • Thanks that worked. An incident is being created in SN. On debug logs the System.debug('##requestbody##'+requestbody); correctly gets the json format but the for some reason the subject is not copied into SN from Salesforce. – MnZ Nov 13 '15 at 9:26

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.