Wednesday 19 August 2015

Salesforce integration with Salesforce

Accessing Apis of external system is call integration in simple words. Here I will talk about how salesforce org gets integrated with another salesforce org.
Going forward I would call these two Salesforce orgs as Source org & Target org.

Requirement: Requirement is to fetch user information from target org in to source org.

Step1. It is the master key point that always you have to build an App in Target Org so now you need to create a connected App.
Once you have created Connected App then you get two important keys :
1. Client ID
2. Client Secret

These are common keys which you get while creating connected Apps.

Step2. Now you need to write a VF page and Apex class.
VF page


Apex Class
Note: Here you have to use client id, client secret & redirection URI in below class:

public class SF2SFIntegerationcontroller {

    string code;
public Pagereference SF2SFIntegerationClassMethod()
{   
    String clientId ='xxxxxx';// 
    String clientSecret = 'YYYYY';    
    String redirecurl = EncodingUtil.urlEncode('https://csopke1.ap1.visual.force.com/apex/SF2SFIntegeration','UTF-8.');
    
    if (ApexPages.currentPage().getParameters().containsKey('code') && ApexPages.currentPage().getParameters().containsKey('code') != null) 
    {
        code = ApexPages.currentPage().getParameters().get('code');
        String oauthURlText = 'https://test.salesforce.com/services/oauth2/token?grant_type=authorization_code&client_id='+clientId+'&client_secret='+clientSecret+'&redirect_uri='+redirecurl+'&code='+code;
        Http h = new Http();
        HttpRequest req = new HttpRequest();
        req.setEndpoint(oauthURlText);
        req.setMethod('POST');
        req.setTimeout(60*1000);
        HttpResponse res = h.send(req);
        String resString = res.getBody();
        OAuth2 objAuthenticationInfo = (OAuth2)JSON.deserialize(res.getbody(), OAuth2.class);
                
        //USAGE
        HttpRequest req1 = new HttpRequest();
        req1.setHeader('Authorization', 'Bearer '+objAuthenticationInfo.access_token);
        req1.setEndpoint(objAuthenticationInfo.instance_url+'/services/data/v29.0/query/?q=SELECT+id,+name+FROM+user+limit+1');
        req1.setMethod('GET');
        req1.setTimeout(60*1000);
        HttpResponse res1 = h.send(req1);
        String resStringfromExternalOrg = res1.getBody();
        system.debug('------'+resStringfromExternalOrg);
        return null;
    }
    else
    {   PageReference pgRef = new PageReference('https://test.salesforce.com/services/oauth2/authorize?response_type=code&client_id='+clientId+'&redirect_uri='+redirecurl);
        return pgRef;
    }
    
  }
    /*To get aouthentication detail Wrapper*/
    public class OAuth2{
    
     public String id{get;set;}
     public String issued_at{get;set;}
     public String instance_url{get;set;}
     public String signature{get;set;}
     public String access_token{get;set;}    
    }

}
 

Step3: Now you just need to execute VF page. You might need to setup remote site setting for URIs, you are hitting while executing Page. If any mediator screen asks to allow permission just allow it (to avoid it you need to use target org's credential in your logic) and see the magic in your debug log.

as these two orgs integrated so now You will get user information from target org in to source org.

No comments:

Post a Comment