• Subbu
  • NEWBIE
  • 10 Points
  • Member since 2015

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 11
    Questions
  • 8
    Replies
I created a resource to insert an Account record with fields- Name and Description in SF1(say), and i want to send the values from another instance SF2(say) using REST API from URL parameters.

I created one VF page with fields Name and Description. when i entered Name and Description fields respectively they should go to contoller dynamically and create the record in resource instance.

Target Org :

User-added image


@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;
    }
}

Source Org :

User-added image
Controller :


public class ApexController {
    
    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='3MVG9fMtCkV6eLheWOt4w544Nluwm1zx0HUAFGdCpPoWBGVOQv2K2fD7t8XotR3c9OE2bIHuXOhRv.ZEzjHYNI';
        Httprequest req=new httprequest();
        String domainName='subbuorg2-dev-ed.my.salesforce.com';
        String endPointURL='https://'+domainName+'/services/data/v27.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);
        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;
    }
}


Visualforce Page ;

<apex:page controller="ApexController">
<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>


I am getting below error


User-added image


Could you please explain about Access_token ,I think issue with Access_token Plz correct......


Is it combination of  Consumer Key + Consumer secret + security token

 
Thakns in Advance
Subbu


 
I have a custom picklist on Account objects it contains values A,B and C. I am accessing this picklist at visualforce by using the <apex:inputfield> .So I don't want to get all the picklist values  , I want values (B and C) at visualforce, Could you please suggest me.
string ratingValue = 'Hot';
public List<Account> lstAccount{get;set;}
string strQuery = 'select id, name, rating, industry,( select id, firstname, lastname, phone, fax from contacts) from account where rating =: ratingValue';
lstAccount = Database.Query(strQuery);
    if(lstAccount.size()!= 0)
    {
    for(Account acc : lstAccount)
    {
        system.debug(acc.id + ' ----> '+ acc.name + ' ----> '+acc.rating + ' ----> '+ acc.industry );
        
        system.debug('Contact Records Information.');
        system.debug('Contact Count ....: '+ acc.contacts.size());
        
        if(acc.contacts.size() > 0)
        {
            for(contact con : acc.contacts)
            {
                system.debug(con.id + ' ---> ' + con.firstname + ' ----> ' + con.lastname);
            }
        }
        else
            system.debug ('No Contacts for this Account');
    }
}
else
{
    system.debug('Account Record Not Found.');
}
  
string ratingValue = 'Hot';
public List<Account> lstAccount{get;set;}
string strQuery = 'select id, name, rating, industry,( select id, firstname, lastname, phone, fax from contacts) from account where rating =: ratingValue';
lstAccount = Database.Query('strQuery');
    if(lstAccount.size()!= 0)
    {
    for(Account acc : lstAccount)
    {
        system.debug(acc.id + ' ----> '+ acc.name + ' ----> '+acc.rating + ' ----> '+ acc.industry );
        
        system.debug('Contact Records Information.');
        system.debug('Contact Count ....: '+ acc.contacts.size());
        
        if(acc.contacts.size() > 0)
        {
            for(contact con : acc.contacts)
            {
                system.debug(con.id + ' ---> ' + con.firstname + ' ----> ' + con.lastname);
            }
        }
        else
            system.debug ('No Contacts for this Account');
    }
}
else
{
    system.debug('Account Record Not Found.');
}
   
I created a resource to insert an Account record with fields- Name and Description in SF1(say), and i want to send the values from another instance SF2(say) using REST API from URL parameters.

I created one VF page with fields Name and Description. when i entered Name and Description fields respectively they should go to contoller dynamically and create the record in resource instance.

My question is how to pass the values from VF page to Controller(into URL parameters.)?