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
asadim2asadim2 

Adding records from one org to another

Hi,

 

All I want to do is login to org1 and press a button that adds an Account to org2 (all I need in response is the Id of the record that was created). I am basically lost with all the different information on the web. I'm currently using user+pass for authentication in my HttpRequest POST call and it's giving me back an invalid session id error. Here are some of the resources that I have read through:

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_restful_http_httprequest.htm

http://www.salesforce.com/us/developer/docs/api_rest/api_rest.pdf

http://boards.developerforce.com/t5/REST-API-Integration/error-quot-message-quot-quot-Session-expired-or-invalid-quot/td-p/409979

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_rest_intro.htm

http://boards.developerforce.com/t5/REST-API-Integration/Calling-Apex-Rest-custom-web-service-from-Apex-code/td-p/327715/page/3

http://boards.developerforce.com/t5/REST-API-Integration/Apex-REST-Integration-Http-POST-Issue/m-p/425445#M1580

 

There's all these talks about tokens, OAuth, session id, remote access services, etc. which have completely lost me.

 

Here's my method:

    public String sendToOrg(String j, String url, String user, String pass)
    {
        HttpRequest req = new HttpRequest();
        req.setMethod('POST');
        req.setEndpoint(url);
        req.setBody(j);
        Blob headerValue = Blob.valueOf(user + ':' + pass);
        String authorizationHeader = 'BASIC ' + EncodingUtil.base64Encode(headerValue);
        req.setHeader('Authorization', authorizationHeader);
        
        Http http = new Http();
        HTTPResponse resp = http.send(req);
        system.debug('**** Response: ' + resp.getBody() );
        if (resp != null && resp.getBody() != null) return resp.getBody();   
        else return null;
    }

 with url being: https://na4.salesforce.com/services/data/v24.0/sobjects/Account/

 

I'd really appreciate a straight-forward, Apex oriented guidance!

BinayakBinayak

Hi ,
You can achieve it using METADATA WSDL.For login into org2 from org 1 use the partner WSDL of org2,generate the APEX class from it and call the login() method of the generated class.With this ,you can obtain the session id.
Now parse and generate the apex class from metadata wsdl.Instantiate this class,add the session id to the session header property of it.Instantiate the createfield class of the metadata class and create the field with the required fields value.