Saturday, February 7, 2015

Salesforce Integration example

Salesforce POST Method Integration
===================================
/*
    In this example, we get the account name from any other platform or any other salesforce. If the insertion process is success, we send the response as 'success'.
 
    - 'postconnectform' - This is the URL for this class. We can call this class using this URL with session Id of this Org.  
        Ex: https://ap1.force.com/services/apexrest/postconnectform/
 
    - https://ap1.force.com - is salesforce org instance
        Note: we need to change the url form salesforce.com to force.com
 
    - /services/apexrest - standard syntax
*/

***************************************************
Example for inserting a Account with requested name
***************************************************

@RestResource(urlMapping='/postconnectform/*')
global class AccountForm {  
 
    public class AccountClass{
 
        public String Name;
     
    }
 
    @HttpPOST
    global static string postFormRegistration(){
     
          try{
     
            RestRequest req = RestContext.request;
            Blob body = req.requestBody;          
            String JSONString = body.toString();
            AccountClass RegCls = new AccountClass();
            RegCls = (AccountClass)JSON.deserialize(JSONString, AccountClass.class);
         
            if(RegCls != NULL){
                Account acc =  new Account();
                acc.Name = RegCls.Name;
                insert acc;
            }
        }catch(Exception e){
     
        }
        return 'success';
    }
}

-------------
Execution flow
--------------
 i) Create the above class in salesforce
 ii) Get the class map url
 iii) Get the sessionId (String SessionId = UserInfo.getSessionId();)
 iv) Open Advanced Rest Client
 v) Select POST Radio button
 vi) Select Form Tab
 vii) Enter the key as 'Authorization'
 viii) Enter the value as 'Bearer sessionId'
 ix) Click Raw tab
 x)Enter the data
    {
        "Name":"Test1234"
    }
  xi) Click Send

Salesforce GET Method Integration
==================================

***************************************************
Example for get the Accounts from salesforce
***************************************************

@RestResource(urlMapping='/getconnectform/*')
global class GetAccount {

    public class InnerClass2{
        public List<AccountClass> testIns;
    }
 
    public class AccountClass{
     
        public String AccId;
        public String Name;
     
    }
 
    @HttpGET
    global static string postFormRegistration(){
     
          try{
     
            RestRequest req = RestContext.request;
            List<Account> listAcc = [SELECT Id, Name FROM Account];
            List<AccountClass> ns = new List<AccountClass>();
            for(Account acc : listAcc){
                AccountClass ins = new AccountClass();
                ins.Name = acc.Name;
                ins.AccId = acc.Id;
                ns.add(ins);
            }
            return JSON.serialize(ns);
         
        }catch(Exception e){
         
        }
        return 'error';
    }
}

----------------
Execution flow
---------------
 i) Create the above class in salesforce
 ii) Get the class map url
 iii) Get the sessionId (String SessionId = UserInfo.getSessionId();)
 iv) Open Advanced Rest Client
 v) Select GET Radio button
 vi) Select Form Tab
 vii) Enter the key as 'Authorization'
 viii) Enter the value as 'Bearer sessionId'
 ix) Click Send button

No comments:

Post a Comment

Salesforce - Generate dynamic inner query to fetch parent and related child records

Use Case: In many scenarios, we need to clone the records with related child records. Issue / Limitation: We may simply use the "cl...