2

VF Page

<apex:page controller="Apex_Rest_DemoController">
<apex:form >
    <apex:pageBlock >
        <apex:pageBlockButtons >
            <apex:commandButton value="Create Account in SF1 instance" action="{!CreateAccount}"/>
        </apex:pageBlockButtons>
        <apex:pageBlockSection >
            <apex:pageBlockSectionItem >
                <apex:outputLabel for="aname" value="Name"></apex:outputLabel>
                <apex:inputText value="{!accName}" id="aname"/>
            </apex:pageBlockSectionItem>
            <apex:pageBlockSectionItem >
                <apex:outputLabel for="aphone" value="Phone"></apex:outputLabel>
                <apex:inputText value="{!accPhone}" id="aphone"/>
            </apex:pageBlockSectionItem>
            <apex:pageBlockSectionItem >
                <apex:outputLabel for="aw" value="Website"></apex:outputLabel>
                <apex:inputText value="{!accWebsite}" id="aw"/>
            </apex:pageBlockSectionItem>
        </apex:pageBlockSection>
    </apex:pageBlock>

    <apex:pageBlock title="Response">
       <apex:pageBlockSection >
            <apex:outputText value="{!Response}"></apex:outputText>
        </apex:pageBlockSection>
    </apex:pageBlock>
</apex:form>
</apex:page>

Class1:

@RestResource(urlMapping='/Account/*')
global with sharing class MyRestResource {

  @HttpPost
    global static String doPost(String name, String phone, String website) {
        Account account = new Account();
        account.Name = name;
        account.phone = phone;
        account.website = website;
        insert account;
        return account.Id;
    }
}

Class2:

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() { 

    httprequest hreq=new httprequest();
   // String strbody = '{"grant_type":"umakid23", "password":"umakid23", "username":"uma.aries24@gmail.com",  "client_secret":"1336958468500245951",   "client_id":"3MVG9ZL0ppGP5UrDxVvrgRI9Y9tpDN6j_GR7hNZMHdU8CRD8lgjNVK7t1wLvcRu5oj8S4ueokFzLK5PCfarI8"}';
    string refresh_url='https://login.salesforce.com/services/oauth2/token?grant_type=refresh_token&client_id=3MVG9Y6d_Btp4xp7AyV8b3ouuXI_CkGlTRIJLDmriyEpT59eoFmWPZBwjZmoI8Raq3cRrYzFZ.Oa9ZpBayp87&client_secret=3678419209862860540&refresh_token=5Aep8617VFpoP.M.4v8pn3P87bBuGgaM4uJAHwQ6XC44uC_2KAfvo14lu15SmrvnbaNfaS5mLC_6OluEQtzdwb6';
  //string refresh_url='https://login.salesforce.com/services/oauth2/authorize?response_type=token&client_id=3MVG9Y6d_Btp4xp7AyV8b3ouuXI_CkGlTRIJLDmriyEpT59eoFmWPZBwjZmoI8Raq3cRrYzFZ.Oa9ZpBayp87&redirect_uri=https://login.salesforce.com/services/oauth2/success';
    system.debug('_________________refresh_url______________'+refresh_url);
    hreq.Setendpoint(refresh_url);
    hreq.setBody('<?xml version="1.0" encoding="utf-8"?><request><name>'+accName+'</name><phone>'+accPhone+'</phone><website >'+accWebsite+'</website > </request>');
    hreq.setmethod('POST');

    //hreq.setHeader('Accept', 'application/x-www-form-urlencoded');
    //hreq.setHeader('Authorization', 'Bearer ' +refresh_url);
    hreq.setHeader('Content-Type', 'application/x-www-form-urlencoded');
    hreq.setHeader('Host','login.salesforce.com');
    hreq.setHeader('Authorization', 'Bearer ' +refresh_url);
    http htp=new http();
    httpResponse res=htp.send(hreq);
    response=res.getbody();

    Map<String, Object> oauth = (Map<String, Object>)JSON.deserializeUntyped(res.getBody());
    String accessToken = (String)oauth.get('access_token');
    String instanceUrl = (String)oauth.get('instance_url');
    System.debug('------>accessToken' +accessToken );
    System.debug('------>instanceUrl ' +instanceUrl );
    System.debug('------>FutureRefreshToken_ID-------->' +res.getBody());
  //string result=readXMLResponseOfFutureAccessToken(res.getBody());
    HttpRequest req = new HttpRequest(); 
    req.setEndpoint(instanceUrl+'/services/apexrest/Account?');


        req.setMethod('POST');
        req.setBody('<?xml version="1.0" encoding="utf-8"?><request><name>'+accName+'</name><phone>'+accPhone+'</phone><website >'+accWebsite+'</website > </request>');
        req.setBody('{ "name" : "accName", "phone" : "accPhone", "website " :"accWebsite" }');
        req.setHeader('Authorization', 'OAuth '+accessToken);
        req.setHeader('Content-Type', 'application/x-www-form-urlencoded');
        Http http = new Http();

        HTTPResponse res1 = http.send(req);

        System.debug('BODY: '+res1.getBody());
        System.debug('STATUS:'+res1.getStatus());
        System.debug('STATUS_CODE:'+res1.getStatusCode());

       // return res.getBody()    


        return null;
    }
}

Output:Two org gets connected i cannot create account in second salesforce org.How do i pass the parameters and create a account in another org O=

Please help me.I am a new learner in Webservice REST API...Thanks in advance....

  • 1
    I have done things similar to this but that was based on JSON. I simply created a request body that contained all the parameters. Then serialized the same using Json.serialize() and sent the serialized request using http send() method. In the target org, I deserialized the request and got all the parameters. Please mind, your method doPost accepts arguments. I would rather work on a no argument method. Makes it simple. – the_phantom May 6 '15 at 12:09
  • How you are getting refresh token ? – Jitendra Zaa May 7 '15 at 4:18

Your Answer

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

Browse other questions tagged or ask your own question.