Sunday, October 14, 2018

salesforce soap integration

Salesforce soap integration:

Today we will be learning salesforce soap integration by taking an example of two salesforce systems.

Let say we have below two systems,

The first system:(Parent system)

The second system:(Child system)


salesforce soap integration,salesforce soap integration example, salesforce integration soap api,salesforce soap api integration,soap api integration salesforce example
Parent system is the one where we will be creating records manually and child system is the one in which we will pass the created record so as to create the same record in the child system as well.

Now, for connection between parent system and child system we use Partner WSDL, we will be generating partner WSDL in child system and consuming the same in parent system so that parent system can connect with child system. As we are passing records from the parent system to child system we need one apex class (i.e Webservice class) in the child system to accept parameters and create a record.



Activities we need to do in the child system,

1)Generate WSDL file.  // Partner WSDL

Go to setup>Api>Click generate partner wsdl>Save

This is your first WSDL file called partner wsdl which is required for connection.


salesforce soap integration,salesforce soap integration example, salesforce integration soap api,salesforce soap api integration,soap api integration salesforce example

2)Webservice class // to accept parameters from the parent system.

global class receiveparameterorg2{
webservice static string createcontactinorganisationsecond(string name,string name2,string title,string email1)
{
contact obj=new contact();
obj.FirstName=name;
obj.LastName=name2;
obj.Title=title;
obj.email=email1;
insert obj;
return 'success';
}

}


Generate WSDL file for the above class and save it. (This is your second WSDL file called web service WSDL)

3)Username/Password/security token  // This we need to give to parent system for connection.


Activities we need to do in the parent system,

1)Consume partner WSDL  // Through generate from WSDL button you can consume child system partner WSDL. The button is available while creating a new apex class.

When you generate class you will get some apex classes out of this the main class which we required is:
  • partnerSoapSforceCom
salesforce soap integration,salesforce soap integration example, salesforce integration soap api,salesforce soap api integration,soap api integration salesforce example


2)Consume Webservice WSDL // Through generate from WSDL button you can consume child system web service WSDL.

When you generate class you will get,
  • soapSforceComSchemasClassReceivepar
salesforce soap integration,salesforce soap integration example, salesforce integration soap api,salesforce soap api integration,soap api integration salesforce example

3)Remote site setting. // When you make a callout to an external system that particular site must be registered in remote site settings so as to make proper callout otherwise callout will fail.


Search for "Endpoint" in "partnerSoapSforceCom" and "soapSforceComSchemasClassReceivepar",
Copy the endpoint and create remote site settings for both.

Soap Integration salesforce




Image from partnerSoapSforceCom.


Now, To pass the parameter to the child system we will hit the web service method of the child system i.e(createcontactinorganisationsecond) which exist in  "soapSforceComSchemasClassReceivepar" wsdl in parent system.

Create a simple trigger in the parent system to call helper class.

trigger maintrigger on Contact (after insert,after update) {
contact obj2=new contact();
string fname;
string lname2;
string title1;
id id1;
string emailtest;
contact obj=trigger.new[0];

if ((obj.Name!='' || obj.Name!=null) && (obj.email!='' || obj.email!=null)   )

{
fname=obj.firstName;
lname2=obj.lastName;
title1=obj.Title;
id1=obj.id;
emailtest=obj.email;
sendContactToExternalSystem obj1=new sendContactToExternalSystem ();
obj1.sendcontact(fname,lname2,title1,id1); // Calling helper method...
}

}



Helper class in parent system to send data,

Note:Some important points to keep in mind.


  • Future method will always be static with void return type.
  • First we will generate session id.
  • Second we will pass this session id to session header element.
  • Third we will pass this session header element to session header.


global class sendContactToExternalSystem
{
set<id> id1=new set<id>();

public void sendcontact(string fname,string lname,string title,id id1)

{

senddatatosalesforce2(id1);


}


@future (callout=true)

public static void senddatatosalesforce2(id id2)
{
contact condata=[select firstname,lastname,title,email from contact where id =:id2];
string nametest;
string nametest2;
string titletest;
string emailtest1;
nametest=condata.firstname;
nametest2=condata.lastname;
titletest=condata.title;
emailtest1=condata.email;

partnerSoapSforceCom.Soap mypartnersoap=new partnerSoapSforceCom.Soap();  // Main class:partnerSoapSforceCom  Sublcass:Soap which contains login method

partnerSoapSforceCom.LoginResult  partnerloginresult=mypartnersoap.login('user-name-of-child-system','passwordSecurityTokenCombination');  

// Return type of login method is "partnerSoapSforceCom.LoginResult"

system.debug('Generated session id is'+partnerloginresult.sessionId);  // Generating session id 

string sessionidfetch=partnerloginresult.sessionId; // Storing session id

soapSforceComSchemasClassReceivepar.SessionHeader_element webservicesessionheader=newsoapSforceComSchemasClassReceivepar.SessionHeader_element();


webservicesessionheader.sessionId=sessionidfetch;  // Passing session id to session header element

soapSforceComSchemasClassReceivepar.receiveparameterorg2 objA=new soapSforceComSchemasClassReceivepar.receiveparameterorg2();

objA.SessionHeader=webservicesessionheader;  // Passing session header
string status=objA.createcontactinorganisationsecond(nametest,nametest2,titletest,email);
system.debug('status of task is'+status );


}



}

No comments:

Post a Comment