I tried the following code to insert incident from salesforce to ServiceNow but not sure what I am doing wrong here,

Can anyone guide me what changes needed in this code or do I need to create anything in ServiceNow like we have Remote site setting in salesforce?

public class sfdcTOsnow{
    @Future(callout=true)
    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 = 'Stech1234';
        Blob headerValue = Blob.valueOf(username + ':' + password);
        String authorizationHeader = 'BASIC ' + EncodingUtil.base64Encode(headerValue);
        req.setHeader('Authorization', authorizationHeader);
        req.setEndpoint('https://dev62899.service-now.com/');
        req.setMethod('POST');
        req.setHeader('Content-Type', 'application/json');

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

        gen.writeStringField('number', c.CaseNumber);
        gen.writeStringField('caller_id','62826bf03710200044e0bfc8e5df1');
        gen.writeStringField('short_description','This is Test');
        gen.writeEndObject();

        String pretty = gen.getAsString();
        system.debug('---Pretty---'+pretty);

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

        system.debug('---Body---'+res.getBody());
    }
}

Here is my trigger code,

trigger servicenow on Case (after insert){
    sfdcTOsnow.servicenowPost(trigger.new[0].Id);
}
up vote 0 down vote accepted

Ohh I got as I was not aware of that EndPoint for the ServiceNow,

I just make changes in following line and it worked as i needed,

req.setEndpoint('https://dev62899.service-now.com/api/now/v1/table/incident');

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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