+ Start a Discussion
priyanka m 8priyanka m 8 

How to call service now api's in salesforce and map the fields between incident and case

I want to integrate servicenow and salesforce. I am able to create the incident from salesforce from apex class. But I want to create the incident in servicenow when case is created in salesforce.
How this can be achieved?
Hi Priyanka,

You can write Apex trigger on case object, that trigger will call an Apex class, which will then create an 'Incident' in ServiceNow. Something like the code given below.
trigger CreateIncidentInServiceNow on Case (after insert) {
  for(Case c : Trigger.New){                                                  

public class ServiceNowPostRequest{
        public static void servicenowPostMethod(string incidentCase){  
        Http http = new Http();
        HttpRequest req =  new HttpRequest();
        HttpResponse res = new HttpResponse();
        String username = 'xxxxx';
        String password = 'xxxxxxxxxxxxx';
        Blob headerValue = Blob.valueOf(username + ':' + password);
        String authorizationHeader = 'BASIC ' +
        req.setHeader('Authorization', authorizationHeader);
        req.setEndpoint('https://Your Instance/api/now/v1/table/incident');
        req.setHeader('Content-Type', 'application/json');
        JSONGenerator gen = JSON.createGenerator(true);
        Case c=[select Description from case where id=: incidentCase];
        gen.writeStringField('number', c.id);
        gen.writeStringField('caller_id','Alene Rabeck');
        gen.writeStringField('short_description', c.description);
        //you can now use String pretty as your body
        String jsonBody = gen.getAsString();
        res = http.send(req);


Mark Best Answer if this fulfills your requirement.
Bhojraj DhakateBhojraj Dhakate

Hi Arun,

I am new to salesforce. I am trying to integrate salesforce to servicenow.

I have created "APEX Class and the APEX trigger" as mentioned in your thread.

When i am trying to ececute the code i am getting "Unexpected token trigger error".

Am i doing something wrong ? Can you help me on that?

Thanks in advance!

Bhojraj Dhakate