1

Hi This my apex class and trigger 'm going to create an incident in serviceNow from salesforce but the below code execution i'm getting some errors anyone guide me to resolve them and creating an incident in serviceNow

This is my apex class:

public class SericeNow{
@future
public static void servicenowPost(Case incidentCase){  

 Http http = new Http();
 HttpRequest req =  new HttpRequest();
 HttpResponse res = new HttpResponse();

 String username = 'admin';
 String password = '$Test12345';

  Blob headerValue = Blob.valueOf(username + ':' + password);
  String authorizationHeader = 'BASIC ' +
  EncodingUtil.base64Encode(headerValue);
  req.setHeader('Authorization', authorizationHeader);

  req.setEndpoint('https://dev23577.service-now.com/');
  req.setMethod('POST');
  req.setHeader('Content-Type', 'application/json');

  JSONGenerator gen = JSON.createGenerator(true);
  gen.writeStartObject();

  //you can add as many mappings as you want
  gen.writeStringField('Number', incidentCase.field);
  gen.writeEndObject();

 //you can now use String pretty as your body
  String pretty = gen.getAsString();

  req.setBody(pretty);
  res = http.send(req);

   system.debug(res.getBody());
  }
}

this my trigger :

trigger servicenow on Case (after insert){
 for(Case c : Trigger.New){                                                  
    SericeNow.servicenowPost(c);                                                                          
  }
}

when executing the above class i'm getting error :

Error: Compile Error: Unsupported parameter type Case at line 3 column 20

Thanks,

1

1 Answer 1

0

Future methods in salesforce does not allow parameter type as Object .So you can pass the Id here

trigger servicenow on Case (after insert){
  SericeNow.servicenowPost(trigger.new[0].Id);
}

Apex class parameter can take Set instead of object

public class SericeNow{
@future
public static void servicenowPost(Id CaseId){  

    Case c = [Select Id, CaseNumber from Case where Id =: CaseId limit 1];
    Http http = new Http();
    HttpRequest req =  new HttpRequest();
    HttpResponse res = new HttpResponse();
    String username = 'admin';
    String password = '$Test12345';
    Blob headerValue = Blob.valueOf(username + ':' + password);
    String authorizationHeader = 'BASIC ' +
    EncodingUtil.base64Encode(headerValue);
    req.setHeader('Authorization', authorizationHeader);
    req.setEndpoint('https://dev23577.service-now.com/');
    req.setMethod('POST');
    req.setHeader('Content-Type', 'application/json');

    JSONGenerator gen = JSON.createGenerator(true);
    gen.writeStartObject();

    //you can add as many mappings as you want
    gen.writeStringField('Number', c.CaseNumber);
    gen.writeEndObject();

    //you can now use String pretty as your body
     String pretty = gen.getAsString();

     req.setBody(pretty);
     res = http.send(req);

     system.debug(res.getBody());
    }
  }

Also try avoiding callout in for loop as there are limits in salesforce regarding no of callout

2
  • i removed the callout in apex class and press save again getting the another error : Error: Compile Error: Invalid field field for SObject Case at line 25 column 41 but here i map the servicenow incident fields help me how to mapping
    – sekhar
    Mar 27, 2017 at 3:45
  • Check now .I edited the code .Sounds like you are new to salesforce development .Recommend you to go to developer.salesforce.trailhead to learn Mar 27, 2017 at 3:46

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