• sandeep teerth
  • NEWBIE
  • 0 Points
  • Member since 2016

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 2
    Replies
Hi All,

I am trying to connect one sf org to anaother. I am using the below class to initiate the call from command button. I copied session id of target org and hard coded here in the class which then worked. But for next time it expires and i get invalid session id.

Can anyone help me out how i can dynamically have the session id of source org, so that i need not to hard code it.

public class Apex_Rest_DemoController {
    
    public String response{get;set;}
    public String accName{get;set;}
    public String accPhone{get;set;}
    public String accWebsite{get;set;}
    
    public PageReference CreateAccount() {
        //find access token using Auth 2.0 
       String Access_Token = '00D28000000Heme!AQ8AQAFOnSjXlGvbNT9JXj5vcC2XjQyFtz8ls_x0IMmoDuX91WDLvb9j5qficiTwQpCGjTYIRCUOmvwQOlWvP2.0HPA3x9M.';
       
        Httprequest req=new httprequest();
        String domainName='ap2.salesforce.com';

        String endPointURL='https://'+domainName+'/services/data/v34.0/sobjects/Account';
        req.setendpoint(endPointURL);
        req.setHeader('Content-Type', 'application/xml; charset=utf-8');
        req.setBody('<?xml version="1.0" encoding="UTF-8" ?><request><name>'+accName+'</name><phone>'+accPhone+'</phone><website >'+accWebsite+'</website > </request>');
        req.setmethod('POST');
        req.setHeader('Authorization','Authorization: Bearer '+Access_Token);
        //req.setHeader('Authorization','Bearer '+Access_Token);
        //req.setHeader('Authorization', 'OAuth '+UserInfo.getSessionId());
        Http http = new Http();
        HTTPResponse res = http.send(req);
        response=res.getbody();
        System.debug('****************res.getStatusCode();'+res.getStatusCode());
        System.debug('****************res.getbody();'+res.getbody());
        return null;
    }
}

Regards

I am using cUrl to test out the REST api. I am not using OAuth  to get the token. I am using the

curl https://test.salesforce.com/services/Soap/c/23.0/xxxxxxxxxx-H "Content-Type: text/xml; charset=UTF-8" -H "SOAPAction: login" -d @login.txt

 

to get the session id.

 

Then I am trying to use the session id as mentioned in the REST Api quick start guide. Though I am kind of confused what url I should be using to request Resources?

 

Thanks,

Janak.