function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
sfdc Beginnersfdc Beginner 

Sending Request

How to Send a Request from Organization1 to Organization2 ?
Ajay K DubediAjay K Dubedi
Hi,
-make a connected app in the target org and get the app secret and the consumer id.
-make a remote site setting in your source org.
-save these credentials in the code below and run
ass WebServiceRestotherOrg2 {
 public Account acc{get; set;}
 public wrapperForHttp wObj{get;set;}
 public String accessToken;
 public String instanceUrl;
 public String lastname;
 public Id conid;
 public String object{get;set;}
 public string fieldname{get;set;}
 public String conbody{get;set;}
 public final string endPointUrl='https://ap2.salesforce.com/services/oauth2/token';
 public final String clientId ='consumerid';
 public final string clientSecret='appsecret';
 public final String userName = 'username';//
 public final String password = 'password'; //
    
    public WebServiceRestotherOrg2(){}
    public void callout(){
        HttpRequest req = new HttpRequest();
        req.setMethod('POST');
        req.setEndpoint(endPointUrl);
        req.setBody('grant_type=password' + 
                    '&client_id='+ clientId + 
                    '&client_secret='+ clientSecret +
                    '&username=' + EncodingUtil.urlEncode(userName, 'UTF-8') + 
                    '&password=' + EncodingUtil.urlEncode(password, 'UTF-8'));
        Http h = new Http();
        Httpresponse res = h.send(req);
        string body = res.getBody();  
        
        system.debug('***** Body -- '+ body);
        
        wObj = (wrapperForHttp)Json.deserialize(body,wrapperForHttp.class);
        accessToken = wObj.access_token;
        instanceUrl = wObj.instance_url;
        system.debug('***** accessToken -- '+ accessToken + ' **** instanceUrl -- '+ instanceUrl);
           // getMethod();
            //deletMethod();
            //updateMethod();
    }
    
    public void getMethod(){
        try{
        string queryUrl = '/services/apexrest/hello?sObjects=obj';//emailId=sourabhgoyalalwar@gmail.com&User_name=sourabhgoyal@gmail.com
        HttpRequest req = new HttpRequest();
        req.setMethod('POST');
        req.setEndpoint(instanceUrl + queryUrl);
        system.debug('*** req**' +req);
        req.setHeader('Content-Length', '0');
        req.setHeader('Authorization', 'OAuth '+ accessToken);
        Http http = new Http();
        HTTPResponse res = http.send(req);
        conbody=res.getBody();
        System.debug('BODY: -- '+conbody);
       
        System.debug('-------sEarched Successfully-----');
    }

    catch(Exception e){
        system.debug('------Error Message------' +e.getMessage());
    }
        }

 public class wrapperForHttp{
        String Id;
        String instance_url;
        String issued_at;
        String token_type;
        String signature;
        String access_token;
        String LastName;
        
    }
}

Thanks