Friday 9 February 2018

Salesforce to Salesforce Integration using PartnerSoap API



If any one new to Webservices, this blog post will help you to get webservices experience. 
It's very easy to integrate with another salesforce instance using SOAP API. Just follow below steps.

Requirement:-
We have 2 Salesforce instances, Salesforce A And Salesforce B.
Share(create) a Account record from Salesforce A to Salesforce B using Share button on Account record in Salesforce A instance.

Step 1: Generate Partner WSDL in Salesforce B instance.



Step 2: Save the generated Partner WSDL in your local system.



Step 3: Generate Apex code using the generated Partner WSDL in Salesforce A instance.







It results in generation of four classes.
1)sobjectPartnerSoapSforceCom
2)faultPartnerSoapSforceCom
3)partnerSoapSforceCom
4)AsyncPartnerSoapSforceCom

Step 4: Creating a custom button in Account in Salesforce A instance.

Button Code:

{!REQUIRESCRIPT("/soap/ajax/25.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/25.0/apex.js")} 

var agree = confirm("Are you sure you want to share?"); 
if(agree){ 
sforce.apex.execute("SharewithCricket","share",{localId:"{!Account.Id}"}); 
alert("shared"); 

else{ 
alert("not shared"); 
}

Step 5: Add the custom button to Account page layout.

Step 6: Create SharewithCricket apex class, which is calling from button. 

global class SharewithCricket
{
    webservice static void share(Id localId)
    {   
    
        String username = 'username;
        String password = 'passwd+security token';
        
        List<Account> acct = new List<Account>();
        String sql = 'SELECT Name,Phone,Type,Industry,AccountNumber,Description,FAX,SLA__c,Rating FROM Account WHERE ID=:localId';
        acct = Database.Query(sql);
        
        partnerSoapSforceCom.Soap con = new partnerSoapSforceCom.Soap();
              
        partnerSoapSforceCom.LoginResult loginResult = con.login(username, password);    
        
        con.SessionHeader = new partnerSoapSforceCom.SessionHeader_element();
        con.endpoint_x =loginResult.ServerUrl;
        con.Sessionheader.sessionid = loginResult.sessionid;
        
        List<sobjectPartnerSoapSforceCom.sObject_x> sObjects = new List<sobjectPartnerSoapSforceCom.sObject_x>();           
        sobjectPartnerSoapSforceCom.sObject_x tmpObj = new sobjectPartnerSoapSforceCom.sObject_x();
        for(Account ac:acct)
        {
            tmpObj.type_x = 'Account';
            tmpObj.AcctName = ac.Name;
            tmpObj.Phone = ac.Phone;            
            tmpObj.AcctType = ac.Type;
            tmpObj.AcctIndustry = ac.Industry;
            tmpObj.AcctNum = ac.AccountNumber;
            tmpObj.AcctDesc= ac.Description;             
            tmpObj.AcctFAX = ac.FAX;
            tmpObj.AcctSLA = ac.SLA__c;    
            tmpObj.AcctRating = ac.Rating;    
            tmpObj.Acctactive='Yes';
            sObjects.add(tmpObj);
        }
        con.create(sObjects);           
    }
}

Note: Don't forgot to add Salesforce B instance credentials - username and passwd+security token in above code.


Step 6: Add the field details in sobjectPartnerSoapSforceCom apex class.

public String AcctName; 
private String[] AcctName_type_info = new String[]{'Name','urn:partner.soap.sforce.com','string','1','1','true'};      

public String Phone;
private String[] Phone_type_info = new String[]{'Phone','urn:partner.soap.sforce.com','string','1','1','true'};       

public String AcctType;
private String[] AcctType_type_info = new String[]{'Type','urn:partner.soap.sforce.com','string','1','1','true'};      

public String AcctIndustry;
private String[] AcctIndustry_type_info = new String[]{'Industry','urn:partner.soap.sforce.com','string','1','1','true'};       

public String AcctNum;
private String[] AcctNum_type_info = new String[]{'AccountNumber','urn:partner.soap.sforce.com','string','1','1','true'};        

public String AcctDesc;
private String[] AcctDesc_type_info = new String[]{'Description','urn:partner.soap.sforce.com','string','1','1','true'};       

public String AcctFax;
private String[] AcctFax_type_info = new String[]{'Fax','urn:partner.soap.sforce.com','string','1','1','true'};       

public String AcctSLA;
private String[] AcctSLA_type_info = new String[]{'SLA__c','urn:partner.soap.sforce.com','string','1','1','true'};        

public String AcctRating;
private String[] AcctRating_type_info = new String[]{'Rating','urn:partner.soap.sforce.com','string','1','1','true'};

public String Acctactive;       
private String[] Acctactive_type_info = new String[]{'Active__c','urn:partner.soap.sforce.com','string','1','1','true'};

//End

Note: Edit the private String[] field_order_type_info = new String[]{'type_x','fieldsToNull','Id'}; 
To
private String[] field_order_type_info = new String[]{'type_x','fieldsToNull','Id','AcctName','Phone','AcctIndustry','AcctType','AcctNum','AcctDesc','AcctFax','AcctSLA','AcctRating'};

Step 7: Edit partnerSoapSforceCom class, update endpoint_x in Soap inner class with your Salesforce B instance URL.


public String endpoint_x = 'https://ap1.salesforce.com/services/Soap/u/41.0';


Step 8: Adding Remote site settings. 
Add Salesforce B instance URL in Remote site settings.




OUTPUT




Now you can see the Records created in Salesforce B instance.


No comments:

Post a Comment