1

I have two Salesforce Orgs A (Source) & B (Target).

I have created a webservice for that creates an account in B with just one parameter "nam".

I have created an connected app (OAuth settings like Consumer Key & Consumer secret have all been completed)

The following screenshot shows a REST client (Advanced Rest Client which is an addon in Chrome for simulating REST requests) making a REST request and getting the Salesforce Id of the newly created account in Destination Org B.

enter image description here

When I try to simulate the same from an Anonymous Apex in Source Org, it is erroring out stating

09:34:07:493 USER_DEBUG [63]|DEBUG|Response for Payload : [{"errorCode":"URL_NOT_RESET","message":"Destination URL not reset. The URL returned from login must be set"}]

This is my anonymous apex.

Can you someone take a look at this and let me know what is wrong ?

Basically there are two steps involved

Step 1 : Get the authentication token Step 2 : Using the authentication token perform the REST call.

I am indeed able to get the authentication token successfully from Step 1 but I am getting the error only at the Step 2.

public class OAuth2
{

    public String id{get;set;}
    public String issued_at{get;set;}
    public String instance_url{get;set;}
    public String signature{get;set;}
    public String access_token{get;set;}

}

RESTCreds__c r = RESTCreds__c.getValues('1');

String clientId = r.ConsumerKey__c;
String clientSecret = r.ConsumerSecret__c;
String userName = r.Username__c;
String password = r.Password__c;
String url = r.URL__c;
System.debug(clientId);
System.debug(clientSecret);
System.debug(userName);
System.debug(password);
System.debug(url);

String reqbody = 'grant_type=password&client_id='+clientId+'&client_secret='+clientSecret+'&username='+username+'&password='+password;

HTTP h = new HTTP();
HttpRequest req = new HttpRequest();
req.setBody(reqbody);
req.setMethod('POST');
req.setEndPoint(url);

HttpResponse res = h.send(req);
System.debug('Response : '+res);
System.debug('Response Body : '+res.getBody());

OAuth2 objAuthenticationInfo = (OAuth2) JSON.deserialize(res.getBody(),OAuth2.class);
System.debug(objAuthenticationInfo);
System.debug('Id = '+objAuthenticationInfo.id);
System.debug('Issued At = '+objAuthenticationInfo.issued_at);
System.debug(' Instance URL = '+objAuthenticationInfo.instance_url);
System.debug(' Signature = '+objAuthenticationInfo.signature);
System.debug(' Access Token = '+objAuthenticationinfo.access_token);




String jsonstr = '{"nam":"Test 1"}';
System.debug(jsonstr);


if(objAuthenticationInfo.access_token != null)
{
    Http h1 = new Http();
    HttpRequest req1 = new HttpRequest();
    req1.setHeader('Authorization','Bearer '+objAuthenticationInfo.access_token);
    req1.setHeader('Content-Type','application/json');
    req1.setHeader('accept','application/json');
    req1.setBody(jsonstr);
    req1.setMethod('POST');
    req1.setEndpoint('https://login.salesforce.com/services/apexrest/Account');
    HttpResponse res1 = h1.send(req1);
    System.debug('Response for Payload : '+res1.getBody());


}
1

I have figured this out.

Looks like I need to send the second HTTP request to the URL returned from the response (first step).

//req1.setEndpoint('https://login.salesforce.com/services/apexrest/Account');

req1.setEndpoint(objAuthenticationInfo.instance_url+'/services/apexrest/Account');

The above statement fixed the problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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