Another Salesforce blog!!!

Salesforce, Apex

INTEGRATION using SOAP API


Enterprise WSDL

A strongly typed WSDL for customers who want to build an integration with their salesforce.com organization only.
Generate Enterprise WSDL

Partner WSDL
A loosely typed WSDL for customers, partners, and ISVs who are building client applications for multiple organizations. It can be used to access data within any organization.
Generate Partner WSDL

Source Steps:
1. Generate partner wsdl
2. Write an apex webservice class
3. User name, password and security token required

Target Steps:
1. Generate classes for both partner wsdl and apex webservcie wsdl
2. Write a new apex class,
a Connect to source org
b Pass the parameters to the source service class

Soap API Integration
——————–

SOURCE org:

1 go to api in source org and serach for API and download generate partner WSDL
2 create a apex Webservice class (AccountService )
3 user parameters (SOURCEUSERNAME, MYPASSWORD + seucirty token (XXXXXX))

Target org

1) Consume the partner/web services wsdl class in the target (generate apex classes)
you do that by following: apex classes -> click on generate Gnerate from WSDL –> browse your files, select partner and account services to generate the apex classes
your main class is “partnerSoapSforceCom”

2) Remote site settings configuration
a go to the “partnerSoapSforceCom” look for endpoint url in the apex class
endpoint url is: https://login.salesforce.com/services/Soap/u/39.0
b go to Apex Class “soapSforceComSchemasClassAccountser” look for
endpoint urls : https://na35.salesforce.com/services/Soap/class/AccountService

3) Creating visualforce page in the target vfp

a first get the connection by passing username/pass and security token
partnerSoapSforceCom inside that look for “class soap”

SOURCE APEX CLASS:

global class AccountService {

   Webservice static String createCustomer(String name, String phone, String city) 
   {
       Account a = new Account();
       a.Name = name;
       a.Phone = phone;
       a.BillingCity = city;
       
       insert a;
       
       if(a.Id != null) {
           return 'Success';
       }
       else {
           return 'Failure';
       }
   
   
   }

}

PASSING WRAPPER CLASS:

global class CustomersCitySearchService 
{

       webservice static List<AccountWrapper> accountByCitySearch(String city) 
    {
      accountsWrap = new List<AccountWrapper>();
      for(Account a : [select id,name,phone,billingcity from ACCOUNT WHERE billingcity =: city]) {
        accountWrap = new AccountWrapper();
        if(a.name != null) 
           accountWrap.name = a.name;
        if(a.phone != null) 
           accountWrap.phone = a.phone;
        if(a.billingcity != null) 
           accountWrap.city= a.billingcity;          
        
         accountsWrap.add(accountWrap);
      }
      return accountsWrap;
    }  
 
     global static AccountWrapper accountWrap {get;set;}
     global static List<AccountWrapper> accountsWrap {get;set;}
    global class AccountWrapper 
    {
       webservice string name {get;set;}
       webservice string phone {get;set;}
       webservice string city {get;set;}
    }
}

2. DOWNLOAD THE WSDL FOR AccountService / CustomersCitySearchService / PARTNER WSDL

Target org

STEP 1: Generate the APEX classes through WSDL provided by the source
STEP 2: ADD Remote Site Settings Configuration (whatever the source url is, it should be in the WSDL or generated apex classes)
STEP 3: Create VFP/APEX classes

<apex:page controller="CustomerServiceController" tabStyle="Account">
 <apex:form >
 
 <apex:pageBlock title="New Customer">
     <apex:outputText value="{!statusMessage}" style="color:green;font-size:18px;font-bold;"></apex:outputText>
     
    <apex:actionregion >
     <apex:pageblockSection title="Search City" columns="1">
         <apex:inputText value="{!cityName}" ></apex:inputText>
         
         
         <apex:commandButton value="City Search" action="{!doCitySearch}" reRender="op"  />
        
         <apex:outputPanel id="op" >
           totalRecordsFound:   {!totalRecordsFound}
         </apex:outputPanel>
         
     </apex:pageblockSection>
     </apex:actionregion>
     
     <apex:pageblockSection title="Personal Information" columns="1">
         <apex:inputField value="{!a.Name}" ></apex:inputField>
         <apex:inputField value="{!a.Phone}" ></apex:inputField>
         <apex:inputField value="{!a.BillingCity}" ></apex:inputField>
     </apex:pageblockSection>
     
     <apex:pageBlockButtons >
         <apex:commandButton value="Save" action="{!doSave}"/>
     </apex:pageBlockButtons>
 
 </apex:pageBlock>
 </apex:form>
</apex:page>

public class CustomerServiceController {

    public Account a {get;set;}
    public String statusMessage {get;set;}
    public CustomerServiceController() {
        a = new Account();
        statusMessage = '';
    }
    
    public String cityName {get;set;}
    public String totalRecordsFound {get;set;}
    
    
    public pageReference doCitySearch() {
    system.debug('here');
        AccountCityFinder.SessionHeader_element webServiceSessionHeader = new AccountCityFinder.SessionHeader_element();
        webServiceSessionHeader.sessionId = String.valueOf(SFDCCIntegrationConnection.generateSessionId()); 
        
         AccountCityFinder.CustomersCitySearchService citySearchService = new AccountCityFinder.CustomersCitySearchService();      
        citySearchService.SessionHeader = webServiceSessionHeader;
        
        AccountCityFinder.AccountWrapper[] accountsFound = citySearchService.accountByCitySearch(cityName);
        totalRecordsFound = String.valueOf(accountsFound.size()); 
        
        
        return null;
    
    }
    
 public pageReference doSave() {
        
        //step 1: Establish the connection to the source org
        
        /*partnerSoapSforceCom.soap mypartnerSoap = new partnerSoapSforceCom.soap();
        partnerSoapSforceCom.LoginResult partnerLoginResult = mypartnerSoap.login('nisarkhan2@hotmail.com', 'Atlant@2mXf6srsi4oDVAQxQaywG7VXGt');
        system.debug('///partnerLoginResult : ' + partnerLoginResult.sessionId );*/
        
        
        
        //setep 2: Pass the parameters (Name, Phone, City....)
        /*String sessionId = partnerLoginResult.sessionId;*/
        
        soapSforceComSchemasClassAccountser.SessionHeader_element webServiceSessionHeader = new soapSforceComSchemasClassAccountser.SessionHeader_element();
        webServiceSessionHeader.sessionId = String.valueOf(SFDCCIntegrationConnection.generateSessionId());
        
        soapSforceComSchemasClassAccountser.AccountService accService = new soapSforceComSchemasClassAccountser.AccountService();
        
        accService.SessionHeader = webServiceSessionHeader;
        
        String status = accService.createCustomer(a.name, a.Phone, a.BillingCity); //invoke source method
        
        if(status == 'Success') {
            insert a;
        }
        
        if(a.Id != null) {
            statusMessage = 'Customer Created Successfully!';
        } 
        
        return null;
    }
    
    
}
public class SFDCCIntegrationConnection {
    
    public static String generateSessionId() {
        
        //step 1: Establish the connection to the source org
        String userName = 'xxxxx'; //should come from label (Label.SFDCUserName)
        String password = 'xxxx'; //should come from label
        String sToken = 'xxxx'; //should come from label
        
        partnerSoapSforceCom.soap mypartnerSoap = new partnerSoapSforceCom.soap();
        partnerSoapSforceCom.LoginResult loginResult = mypartnerSoap.login(userName, password+sToken);
        system.debug('///sessionId : ' + loginResult.sessionId );
        return loginResult.sessionId;
    }
}

Leave a Reply

Your email address will not be published.

*