function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
MnZ321MnZ321 

Attempting to post Case parameters to external system

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....
 
trigger Case2ServiceNow on Case (after insert) {
  AuthCalloutPost.basicAuthCalloutPost(Trigger.newmap.keyset());
}
Apex...
public class AuthCalloutPost1{
public String Response { get; set;}

@future(callout=true)

 public static void basicAuthCalloutPost(Set<Id> caseids){

 //call to generate request body
 String requestbody = generateBody(caseids);
 System.debug('##requestbody##'+requestbody);
 HttpRequest req = new HttpRequest();
 req.setEndpoint('xxx/now/table/incident');
 
 req.setMethod('POST');
 req.setBody(requestbody);
 req.setTimeout(120000);

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

 Http http = new Http();
 HTTPResponse res = http.send(req);

 System.debug('##res##'+res);
 System.debug('##res.getBody##'+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('My_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('short_description',c.Subject);
           gen.writeEndObject();   
         } 
         else{
         System.debug('not incident');
         }
      }  
           gen.writeEndArray();
           gen.writeEndObject();
           return gen.getAsString(); 
 }

}
I'm trying to post Salesforce Case parameters to the ServiceNow like name, description, because a description is enough to create an incident.
Currently I'm able to create a record in external system, but not able to post the parameters included (short_description in this case). Am I missing something? Thanks in advance.
I would really appreciate if I'm pointed to the right direction. Thanks in advance.
 
RAM AnisettiRAM Anisetti
Hi ,
modify your code like this...
 
public class AuthCalloutPost1{
public String Response { get; set;}

@future(callout=true)

 public static void basicAuthCalloutPost(Set<Id> caseids){

 //call to generate request body
 String requestbody = generateBody(caseids);
 System.debug('##requestbody##'+requestbody);
 HttpRequest req = new HttpRequest();
 req.setEndpoint('xxx/now/table/incident');
 
 req.setMethod('POST');
 req.setBody(requestbody);
 req.setTimeout(120000);

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

 Http http = new Http();
 HTTPResponse res = http.send(req);

 System.debug('##res##'+res);
 System.debug('##res.getBody##'+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('My_Cases');
       gen.writeStartArray();
       for(Case c : [Select Id, RecordTypeId, RecordType.Name, Subject,Contact.name,Description       
         From Case Where Id IN : casesids]) {
         if(c.RecordType.Name == 'Incident'){
           
           gen.writeStartObject();
           gen.writeStringField('short_description',c.Subject);
		   gen.writeStringField('Description',c.Description);
		   gen.writeStringField('Name',c.Contact.name);
		   
           gen.writeEndObject();   
         } 
         else{
         System.debug('not incident');
         }
      }  
           gen.writeEndArray();
           gen.writeEndObject();
           return gen.getAsString(); 
 }

}

 
MnZ321MnZ321
No it didn't work.
RAM AnisettiRAM Anisetti
do one thing ,debug the requestbody and post the Json data here.
 
MnZ321MnZ321
System.debug('##requestbody##'+requestbody); produces .....
DEBUG|##requestbody##{ 
"Cases" : [ { 
"short_description" : "Parameter yo" 
} ] 
}
RAM AnisettiRAM Anisetti
once confirm if you add below lines of code in your class
 
for(Case c : [Select Id, RecordTypeId, RecordType.Name, Subject,Contact.name,Description      

         From Case Where Id IN : casesids]) {

         if(c.RecordType.Name == 'Incident'){

            

           gen.writeStartObject();

           gen.writeStringField('short_description',c.Subject);

           gen.writeStringField('Description',c.Description);

           gen.writeStringField('Name',c.Contact.name);

            

           gen.writeEndObject();

 
MnZ321MnZ321
I tried working with your code, It didn't work.