Thursday 26 March 2015

SALESFORCE TO SALESFORCE INTEGRATION USING REST/SOAP API

For sales force to sales force integration required  Two organization

1. Sales Force Org 1 
2. Sales Force Org 2

STEP 1 : Create one ViSsula Force Page and Controller in Sales Force Org 2


Visual Force Page: (Page Name : sfdc_to_sfdc)

<apex:page controller="ExternalServiceCall">
<apex:form >
<apex:commandButton value="CallService" action="{!callMethod}" title="CallService"/>
</apex:form>
</apex:page>

Apex Controller:

public class ExternalServiceCall {
public PageReference callMethod() {
String Endpoint = '';
Http http = new Http();
HttpRequest req = new HttpRequest();
req.setEndpoint(Endpoint);
req.setMethod('POST');
HTTPResponse res = http.send(req);
System.debug('----------------'+res.getBody());
return null;
}
}

STEP 2 : Create one CONNECTED APP in Sales Force Org 1

1. Go to SETUP > BUILD > CREATE > APP > CONNECTED APP > NEW

Connected App Name : S_TO_S_APP

Contact Email : Your Email Address

API (Enable OAuth Settings) : Check (Enable OAuth Settings)

Callback URL : Sales Force Org 2 visual force page URL (sfdc_to_sfdc)

EX: https://c.ap2.visual.force.com/apex/CallExternalService

Selected OAuth Scopes : Select all and move from left to right


2. SAVE

STEP 3 : Copy consumer key and Consumer Secret and paste in note pad


1. Consumer Key (3MVG9Y6d_Btp4xp46JlONG7HS6J2U7.oBhNF2mAUQhyotYkFWexxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx)

2. For consumer secret click on reveal copy the code (86658178********)

STEP 4 :

https://**salesforce instance**/services/oauth2/authorize?response_type=code&client_id=**Cosumer key**&redirect_uri=**Callback URL**&state=mystate1

In Above URL replace below given parameters

1. **salesforce instance** : Replace with your sales force org instance

2. **Cosumer key** : Consumer Key of connected APP which we copied in the above step

3. **Callback URL** : Sales Force Org 2 visual force page URL

4. State : give your own string in state

Note : state should be different every time your calling this URL


STEP 5 : Copy the Above URL and open in a new browser

1. It will open an window which asking to permission (Allow IT)

2. It will ask your Sales Force Org 2 credentials (Give your credential and login)

3. Page Redirect to visual force page (sfdc_to_sfdc)

4. Visual force URL should look like 
https://c.ap2.visual.force.com/apex/CallExternalService&code=aPrxqJ8A8kLOza.1WWrtEdk2HbfhS3ZFa7OAR20zsgGyVdnZVO_ieN6nRxWBwka19lulk3RoTg%3D%3D&state=mystate1 

5. From the URL copy the CODE and paste in note pad

STEP 6 :

https://**salesforce instance**/services/oauth2/token?code=**code**&grant_type=authorization_code&client_id=**Consumer Key**&client_secret=**Consumer Secret**&redirect_uri=**Call Back URL**

Above URL is to get the Access Token

1. **salesforce instance** : Replace with your sales force org instance

2. **code** : STEP 5

3. **Consumer Key** : STEP 3

4. **Consumer Secret** : STEP 3

5. **Call Back URL** : Sales Force Org 2 visual force page URL

NOTE: Go to Remote site settings and create new remote site (Name : salesforceOrg, URL : **salesforce instance**)

STEP 7: Copy the above URL and paste in STEP 1 class > method > String  Endpoint


1. Class : ExternalServiceCall

Method : callMethod

String Endpoint = STEP 6 (URL)

2. Save

3. Click on Save button

4. Go to the debug log

5. Copy the Access Token

Now Integration is Done with sales force now you can make calls from sales force to sales force

   STEP 8: To call class of Sales Force Org 1 from Sales Force Org 2

EX:

Create a class in Sales Force Org 1

Apex Controller :

@RestResource(urlMapping='/sfdcCalling/*')
global class SalesforceOrg{
@HttpGet
global static List<Contact> sfdcMethod(){
List<Contact> sfdcContact = new List<Contact>();
sfdcContact = [select Name,Email from Contact];
return sfdcContact;
}
}

Call Above calss (SalesforceOrg) From Org 2

Copy the below code and paste in developer console or workbench in Sales Force Org 2 and executive

String accessToken = '00D90000000iCmb!AR4AQKhpsb2wJYLLxdafV71KdkRxBmRuRmWD_LTmEtRkFdxTWYh_V7CPqc*******************************';
Http http = new Http();
HttpRequest req = new HttpRequest();
req.setEndpoint('https://immu-dev-dev-ed.my.salesforce.com/services/apexrest/sfdcCalling');
req.setMethod('GET');
req.setHeader('Authorization',' Bearer '+accessToken);
HTTPResponse res = http.send(req);
System.debug('----------------'+res.getBody());

Note : 
Access Token : From STEP 7

setEndpoint : should be Sales Force Org 1 Endpoint ,In above http call the end point is Sales Force Org 1 class Annotation Name(sfdcCalling)

STEP 9: For more endpoints please refer the below URL

https://developer.salesforce.com/page/Creating_REST_APIs_using_Apex_REST


Tuesday 3 March 2015

TRIGGER RECURSION IN SALESFORCE

How to avoid Trigger Recursion in salesforce


If an executing trigger calls itself then it goes to into infinite loop and is called recursion.
Use Case
trigger DemoTrigger on Demo__c (before insert) {
insert new Demo__c();
}
In the above example when try to insert a record in trigger again Trigger will invoke(Because when try to insert a record again trigger will invoke) until trigger limit exceeded

If a trigger on a particular object executing on a particular DML does the same DML on that object then the same trigger would be called and it will go into recursion.
A static variable can be set in a class before executing the trigger to avoid the recursion.
Solution 

Class:
public class p { 
  public static boolean firstRun = true; 
}
Trigger:
trigger TestTrigger on Demo__c (before insert) {
if(p.firstRun)
{
p.firstRun = false;
insert new Demo__c();
}    
}