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


Wednesday 4 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();
}    
}

Tuesday 24 February 2015

DISPLAY ONE VISUAL FORCE PAGE DATA IN TO ANOTHER VISUAL FORCE PAGE

This can be done using attribute include.


 We have to just specify the name of the visual force page and include this attribute in our visual force page.

<apex:insert>
<apex:composition>
<apex:define>


EX:
VF 1:
----

                 VF page name Template.

                <apex:page >
<apex:outputText >Welcome to Accord</apex:outputText><br/><br/>
<apex:insert name="header"/><br/><br/>
<apex:outputtext >We are Pioneer in training students</apex:outputtext><br/><br/>
<apex:insert name="body"/><br/><br/>
<apex:outputtext >Thank You</apex:outputtext>
</apex:page>

VF 2:
----

<apex:page sideBar="false">
<apex:composition template="Template">
<apex:define name="header">
Accord welcomes you!
</apex:define>
</apex:composition>
</apex:page>

Monday 23 February 2015

SALESFORCE INTEGRATION WITH .NET USING SOAP API

SALESFORCE INTEGRATION WITH .NET USING SOAP API  PART - 1
Required
1. .Net end point (or) WSDL wile 
NOTE : If client provides WSDL file no need to follow STEP 1 
STEP 1 :

Copy the .Net Endpoint given by the client (https://xxxxxxxxxxxx/gw/xxxx/nextgencrm/ldap.getpicture.dev)
Go to the web browser open new tap paste it and add (?wsdl
EX: https://xxxxxxxxxxxx/gw/xxxx/nextgencrm/ldap.getpicture.dev?wsdl 
Copy the XML data and paste in notepad or notepad++
Remove one binding and related port in that XML because sales force support only one binding
EX: 
Remove second binding and related port
Binding:
-------
<wsdl:binding name="LdapSoap12" type="tns:LdapSoap">
.
.
.</wsdl:binding>
Port:
-----
<wsdl:port2 name="LdapSoap" binding="tns:LdapSoap">
<soap:address location="xxxxxxxxxxxx" />
</wsdl:port2>
Save the file as MyService.wsdl on your desktop 
STEP 2:
Login in to your sales force account
Go to setup > Build > Develop > Apex
Click on Generated Apex

Browser and select MyService.wsdl file and Parse it
If successfully parse Change the name of class as MyDotnetService and save it

STEP 3 :

Open the class MyDotnetService it look like below example 
EX: 
public class MyDotnetService {
public class HelloWorld_element {
private String[] apex_schema_type_info = new String[]{'http://tempuri.org/','true','false'};
private String[] field_order_type_info = new String[]{};
}
public class HelloWorldResponse_element {
public String HelloWorldResult;
private String[] HelloWorldResult_type_info = new String[]{'HelloWorldResult','http://tempuri.org/',null,'0','1','false'};
private String[] apex_schema_type_info = new String[]{'http://tempuri.org/','true','false'};
private String[] field_order_type_info = new String[]{'HelloWorldResult'};
}
public class LdapSoap {
public String endpoint_x = 'https://xxxxxxxxxxxxxxxxxxxxxx/gw/xxxxxxxxxx/nextgencrm/ldap.getpicture.dev';
public Map<String,String> inputHttpHeaders_x;
public Map<String,String> outputHttpHeaders_x;
public String clientCertName_x;
public String clientCert_x;
public String clientCertPasswd_x;
public Integer timeout_x;
private String[] ns_map_type_info = new String[]{'http://tempuri.org/', 'MyDotnetService'};
public String HelloWorld() {
MyDotnetService.HelloWorld_element request_x = new MyDotnetService.HelloWorld_element();
MyDotnetService.HelloWorldResponse_element response_x;
Map<String, MyDotnetService.HelloWorldResponse_element> response_map_x = new Map<String, MyDotnetService.HelloWorldResponse_element>();
response_map_x.put('response_x', response_x);
WebServiceCallout.invoke(
this,
request_x,
response_map_x,
new String[]{endpoint_x,
'http://tempuri.org/HelloWorld',
'http://tempuri.org/',
'HelloWorld',
'http://tempuri.org/',
'HelloWorldResponse',
'MyDotnetService.HelloWorldResponse_element'}
);
response_x = response_map_x.get('response_x');
return response_x.HelloWorldResult;
}
}
STEP 4
Go > setup > Administer > Security Controls > Remote Site Settings > New Remote Site
Add Endpoint URL given by client
If client given WSDL file , Go to generated class ( MyDotnetService )

MyDotnetService > LdapSoap > endpoint_x

copy the end point and paste in remote site settings

STEP 5 :

Open the workbench or developer console

Copy class Name , sub class name , and method name like below example 
EX:

MyDotnetService.LdapSoap ldap = new MyDotnetService.LdapSoap();
String response = ldap.HelloWorld();
System.debug('response--------'+response); 
NOTE :

If time out error came, Add below code
ldap.TimeOut_x = 60000;

Now you successfully integrated sales force with .Net using SOAP API 
NOTE : This process is works only when client end point has no Authorization

Tuesday 30 December 2014

Salesforce Organization File and Data Storage usage find out by using APEX

To find org file and data usage copy below class and save 


Step 1:


public class OrgFileSpaceLimit{

public void fileParseMethod(){

Datetime TDate = datetime.now();
String fileStorage = 'Used File Space</td><td class="dataCol">';
String dataStorage = 'Used Data Space</td><td class="dataCol">';
String endpointUrl = 'https://immu-dev-dev-ed.my.salesforce.com/00D90000000iCmb'; //-- endpointUrl = sales force instance/organization id-------

Http http = new Http();
HttpRequest req = new HttpRequest();
req.setEndpoint(endpointUrl);
req.setMethod('GET');
req.setHeader('Cookie','sid='+UserInfo.getSessionId());
HTTPResponse res = http.send(req);
String pageData = res.getBody();

//------Parsing the html body to fetch file and data usage and storing in a string-------------// 

String fileSize = pageData.substringAfter(fileStorage).left(5).remove('&n')+' MB ' + ' and percent used ' + pageData.substringAfter(fileStorage).substringBetween('(',')');
String DataSize = pageData.substringAfter(dataStorage).left(5).remove('&n')+ ' MB ' + ' and percent used ' + pageData.substringAfter(dataStorage).substringBetween('(',')');

//-----send email with out any condition-------------//
 
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setToAddresses(new string[] {'skimran1038@gmail.com'});
mail.setSubject('Organization File and Data Storage Usage Space');
mail.setPlainTextBody('Total Used File Space by '+ TDate +' is '+ fileSize + '\n \n' + 'Total Used Data Space by ' + TDate + ' is ' + DataSize);
mail.setReplyTo('skimran1038@gmail.com');
mail.setSenderDisplayName(UserInfo.getUserEmail());
Messaging.SendEmailResult[] resp = Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
}

Step 2:


Go to developer console or work bench executive below code or schedule the class

OrgFileSpaceLimit org = new OrgFileSpaceLimit();
    org.fileParseMethod();


Wednesday 17 December 2014

STREAMING API IN SALESFORCE

What is Streaming API ?


To put it simple, streaming API is a mechanism which monitors data changes in Salesforce based on the SOQL query you define, and if the condition matches, sends push notifications to all the subscribed clients.

Since Summer ’12, streaming API is available in any API-valid organizations. You can test it to see how it works in the Developer Edition.

Also, push notifications can be received from a page in a Salesforce application, a server and a page outside of Salesforce, and Java client.

For this example, we will use a Visualforce page to receive push notifications.

What is a Push Notification ?


Push notification is the technology that allows you to send information without the clients’ request when an event occurs in the server.

In "pull technology" where information is sent in response to the clients’ request, the page will not be updated unless the user intentionally refreshes or changes the page. Therefore, in order for the client to know an event has occurred in the server, you need to send a page request periodically and receive the result (called polling).

On the server side, a series of processes to generate an HTTP connection, send information, and close the connection by each and every polling is necessary. iI polling is performed by many clients, then it would consume a huge amount of server resources and network bandwidth.

If you use Ajax that sends a request asynchronously by restricting one part of the page, you can reduce some network bandwidth consumption. However, this is not effective as you still need to respond to the requests that are sent regularly regardless of the server event.

I would not recommend you to use polling frequently, especially if you are using Salesforce, because polling from outside of Salesforce, such as SOAP/REST API consumes API request counts.

This is when streaming API becomes useful.

Note : Before going to code we need some java script files 

STEP 1 :  Go through the URL  and download the zip file  (https://github.com/cometd/cometd)

STEP 2 :  Extract the zip file and follow the below process 

              Next, upload the following files as static resources (Your Name > Setup > Develop > Static 

              Resources > New)

                FILE                                                 NAME

             cometd.js                                             cometd

             jquery-1.5.1.js                                      jquery

             json2.js                                                 json2

             jquery.cometd.js                                  jquery_cometd


STEP 3 :  we have to create one push topic on object 
            
            Go to the developer console or workbench copy the below code and executive it 

            PushTopic pushTopic = new PushTopic();
       pushTopic.ApiVersion = 29.0;
       pushTopic.Name = 'pushOnAccout';
       pushTopic.Description = 'All records for the Account object';
       pushtopic.Query = 'SELECT Id, Name,Phone FROM Account';
       insert pushTopic;
       System.debug('Created new PushTopic: '+ pushTopic.Id);

STEP 4

                Visual force Page :

               
<apex:page controller="StreamingApiController"> 
<apex:includeScript value="{!$Resource.cometd}"/>
<apex:includeScript value="{!$Resource.jquery}"/>
<apex:includeScript value="{!$Resource.json2}"/>
<apex:includeScript value="{!$Resource.jquery_cometd}"/>

<script type="text/javascript">

(function($){
    $(document).ready(function() {

    $.cometd.init({
        url: window.location.protocol+'//'+window.location.hostname+'/cometd/29.0/',
        requestHeaders: { Authorization: 'OAuth {!$Api.Session_ID}'}
    });
    $.cometd.subscribe('/topic/pushOnAccout', function(message) {
        var list = new Array();
    list.push(message.channel,message.data.sobject.Name,message.data.sobject.Id,message.data.event.type,message.data.event.createdDate);

Visualforce.remoting.Manager.invokeAction('{!$RemoteAction.StreamingApiController.methodTosendMessage}',list,function(result,event){

},{escape:true});

    $('#content').append('<p>Notification: ' +
        'Channel: ' + JSON.stringify(message.channel) + '<br></br>' +
        'Record name: ' + JSON.stringify(message.data.sobject.Name) + '<br></br>' +
        'ID: ' + JSON.stringify(message.data.sobject.Id) + '<br></br>' +
        'Event type: ' + JSON.stringify(message.data.event.type)+'<br></br>' +
       'Created: ' + JSON.stringify(message.data.event.createdDate) + '</p>');
    });
});
})(jQuery)

</script>

<body>
<div id="content">
<p>Notifications should appear here...</p>
</div>
</body>
</apex:page>

           Controller : 

            public class StreamingApiController {
                @Remoteaction()
                public static void methodTosendMessage(List<String> streamData){
                    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                    mail.setToAddresses(new string[] {'xxxxxxxxx@gmail.com'});
                    mail.setCcAddresses(new string[] {'xxxxxxx@gmail.com'});
                    mail.setSubject('Record ' + streamData[3]);
                    mail.setPlainTextBody('Record Details ' + streamData);
                    mail.setReplyTo('xxxxxxxxxx@gmail.com');
                    mail.setSenderDisplayName(UserInfo.getUserEmail());
                    Messaging.SendEmailResult[] res = Messaging.sendEmail(new             
                    Messaging.SingleEmailMessage[] { mail });       
               }
           }

Note

            visual force page is used to monitor the Real time data 

           Controller is used to send a mail , Real time data to  user 's
         


Friday 12 December 2014

TEST CLASSES IN SALESFORCE


Test.startTest() and Test.stopTest() are very useful when your test class hits Sales force Governor Limits.

The code inside Test.startTest() and Test.stopTest() have new set of Sales force Governor Limits.

As a good practice, make sure initializing the variables, fetching records, creating and updating records are coded before Test.startTest() and Test.stopTest()

and calling the controllers for code coverage is done inside Test.startTest() and Test.stopTest().

The code before Test.startTest() and after Test.stopTest() have new set of Sales force Governor Limits and code between Test.startTest() and Test.stopTest()

have new set of Sales force Governor Limits.


EX:

private class TestClass {
    static testMethod void test() {
        Test.startTest();
        Call the controller for code coverage
        Test.stopTest();
    }
}

Normal Test class:

public class demoTestClass(){ 
public void demoMethod(){
.

.

.

}

}

@isTest

public class TestMethodClass {

static testMethod void testMethod() {

demoTestClass newDemo = demoTestClass();

newDemo.demoMethod();

}

}



Test Class for Callout :


To cover callout in class we have to write a test class first which is used for dummy callout and that test class has to use in our original test class

so total we have to write 2 test classes

1. To call dummy web service

2. Real test class



Class :

-----
public class CalloutClass {

public static HttpResponse getInfoFromExternalService() {

HttpRequest req = new HttpRequest();

req.setEndpoint('https://graph.facebook.com/oauth/access_token');

req.setMethod('GET');

Http h = new Http();

HttpResponse res = h.send(req);

return res;

}

}

Test class for dummy web service :


@isTest

global class MockHttpResponseGenerator implements HttpCalloutMock {

global HTTPResponse respond(HTTPRequest req) {

System.assertEquals('https://graph.facebook.com/oauth/access_token', req.getEndpoint());

System.assertEquals('GET', req.getMethod());

HttpResponse res = new HttpResponse();

res.setHeader('Content-Type', 'application/json');

res.setBody('{"foo":"bar"}');

res.setStatusCode(200);

return res;

}

}



Test class for CalloutClass class:


@isTest

private class CalloutClassTest {

@isTest static void testCallout() {

Test.setMock(HttpCalloutMock.class, new MockHttpResponseGenerator());

HttpResponse res = CalloutClass.getInfoFromExternalService();

String contentType = res.getHeader('Content-Type');

System.assert(contentType == 'application/json');

String actualValue = res.getBody();

String expectedValue = '{"foo":"bar"}';

System.assertEquals(actualValue, expectedValue);

System.assertEquals(200, res.getStatusCode());

}

}



Test class for PageReffernce :


Class:

-----

public PageReference add() {

insert technology;

return null;

}

Test Method:

-----------

public static testMethod void testMyController() {

PageReference pageRef = Page.yourPageName;

Test.setCurrentPage(pageRef);

MyController controller = new MyController();

controller.add();

}



Test Class for batch apex:



@isTest

private class myTestClass {

static testMethod void myTestMethod() {

generateTestData();

Test.startTest();

DemoBatchClass batchObj = new DemoBatchClass();

batchObj.execute();

Test.stopTest();

}

}