Skip to main content
Hi,

I need make a integration between Salesforce and ServiceNow.

And I NEED do that using APEX. Because we need make a especific sync between both system.

So, here are the things I did.

For test:

- I Consume the REST API of ServiceNow with POSTMAN and JAVA (And work normally).

- I Can make a POST and a GET using POSTMAN and JAVA.

- I did the same integration using SOAP for Salesforce (Create and GET with JAVA).

- All this work normally when I tested.

Testing with Salesforce.

- After the success of the tests above, I tested consuming the ServiceNow API with APEX inside Salesforce.

- I Configurated the Remote Settings in SF.

- I Configurated the Connect Apps in SF.

Until now everything is fine, but.....

Test with APEX

- When I created the APEX code for make a POST for example, occurs a error of Authetication

- The user name and passwords is the same that I used in JAVA.

- So, I think the code APEX is correct, but I don't know what is going on.

My Company Have the Salesforce Enterprice and SeviceNow helsinc.

Here is the code that I'm using in Salesforce:

public class Authentication {

    

    //@future (callout=true)

    public static void basicAuthToken(String user, String password){

           

        try{

        

        HttpRequest request = new HttpRequest();

        

        request.setEndpoint('https://svcdev.service-now.com/api/now/table/pm_project');

        Blob auth = Blob.valueOf(user+':'+password);

        System.debug('Auth: '+ auth);

        String authorizationHeader = 'BASIC' + EncodingUtil.base64Encode(auth);

        

        request.setMethod('POST');

 

        request.setHeader('Authorization', authorizationHeader);

       

        request.setHeader('Content-Type', 'application/json;charset=UTF-8');

       

        String jsonString = '{\n\"u_customer\":\"92f53eee6f186d00a18b2d232e3ee471\",\"description\":\"Created by Salesforce APEX\"}';

        request.setBody(jsonString);

        

            Http http = new Http();

            HttpResponse res = new HttpResponse();

            res = http.send(request);

            

            System.debug('Body:'+ res.getBody());

            System.debug('Status CODE: '+res.getStatusCode());

            System.debug('Authorization in Bytes: '+authorizationHeader);

            System.debug('Data are been saved: '+jsonString);

            

        }catch(System.CalloutException e){

            System.debug('Erro: '+ e.getMessage());

        }

    }

}

here where I test in "Execute Anonymous":

Authentication.basicAuthToken('Test@test', 'Test@123') ;

here is the Response:

10:19:08.9 (9865311)|EXECUTION_STARTED

10:19:08.9 (9869411)|CODE_UNIT_STARTED|[EXTERNAL]|execute_anonymous_apex

10:19:08.9 (10287851)|SYSTEM_MODE_ENTER|false

10:19:08.9 (10634129)|USER_DEBUG|[21]|DEBUG|Auth: Blob[21]

10:19:08.9 (10881280)|CALLOUT_REQUEST|[41]|System.HttpRequest[Endpoint=https://svcdev.service-now.com/api/now/table/pm_project, Method=POST]

10:19:08.9 (862366996)|CALLOUT_RESPONSE|[41]|System.HttpResponse[Status=Unauthorized, StatusCode=401]

10:19:08.9 (862539261)|USER_DEBUG|[43]|DEBUG|Corpo :{"error":{"detail":"Required to provide Auth information","message":"User Not Authenticated"},"status":"failure"}

10:19:08.9 (862625845)|USER_DEBUG|[44]|DEBUG|Status CODE: 401

10:19:08.9 (862660661)|USER_DEBUG|[45]|DEBUG|Autorização em Bytes: BASICdXNlci5zb2FwOlNlcnZpY2VAMTIz

10:19:08.9 (862679896)|USER_DEBUG|[46]|DEBUG|Blob[21]

10:19:08.9 (862697205)|USER_DEBUG|[48]|DEBUG|Dados a serem gravados: {

"u_customer":"92f53eee6f186d00a18b2d232e3ee471","description":"Created by Salesforce APEX"}

NOTE: This code is equals of the code JAVA (Only change is the sintaxe) and in Java Works.

I must doing something wrong in APEX or I have to do some configuration in ServiceNow or Salesforce to allow the integration between the systems.

Can Anyone help me please?

Thanks

Rafael
4 respostas
  1. 17 de fev. de 2017, 14:56
    Hi Rafael,

    Believe that you API uses Basic Authentication that you wanted to consume from Salesforce using APEX. Seems that problem is with below line.

    String authorizationHeader = 'BASIC' + EncodingUtil.base64Encode(auth);

    There must be space between BASIC and Base64 encoded String replace above line with below line.

    String authorizationHeader = 'Basic ' + EncodingUtil.base64Encode(auth);

    Try the code and let me know if this helps :)

    Thanks!
0/9000