Saturday, 15 April 2017

salesforce interview Question Answers part 19

188. How can you prioritize the batch apex jobs pending in the queue.
Ans: This can be done by reordering jobs from set up -> Apex Flex Queue Or can even be done through apex code.
     We can use FlexQueue methods to do this in apex. Example: moveAfterJob,moveBeforeJob etc

189. How many records can a SOSL query return?
Ans: A SOSL Queury can return upto 2000 records only.

190. How many @future method invocations are allowed per apex transaction?
Ans: 50 future method invocation are allowed per apex transaction

191. What is the total heap size limit in case of synchronous and Asynchronous transaction?
Ans: 6MB in case of synchronous and 12 MB in case of Asynchronous transactions

192. How can you track the progress of a scheduled job thorugh apex?
Ans: This can be done query the object CronTrigger which stores all the relevant information like number of times the job was run, next run time etc.

193. How many scheduled jobs can you have at one time?
Ans: We can have upto only 100 jobs scheduled in the org.

194. How can you make callouts in @future methods?
Ans: this can be done by mentioning callout=true
      @future(callout=true)

195. How can you expose your class for external application as a REST resource?
Ans: This can be done by using the annotation @RestResource 

196. When is @HttpPut annotated method calles in case of REST?
Ans: This method is calles when a put request is sent which creates or updates.

197. Can a map key hold a null value?
Ans: Yes a key can be a null value.

198. What happens when you add a entry that has the key which is already exists in the map?
Ans: This overwrites the already existing value against that key.

199. Can you have 2 different keys of type string only differing in case?
Ans: Yes this is possible as the keys of type string are case sensitive and can have multiple simialr strings differing only in case.

200. Do test classes count against the apex code limit for the organisation?
Ans : No classes marked as isTest are not counted against apex code limit.

201. What is the use of creating remote site settings?
Ans: A apex callout can succefully call the external site only if it is defined in remote site setting else the callout fails.

Friday, 1 July 2016

Using @future method in salesforce and its limitations

Salesforce provides different ways to run your code asynchronously like Batch apex, @future method. In this post we will see how to use @future method, its advantages, limitations and the precautions that we need to follow while using it. 

We can use the @future methods in cases where methods are long running. In such cases we can prevent governers limits by making it as asynchronous transaction. @ future methods also help in preventing  mixed dml errors by isolating multiple dmls.

In order to mark a method  as future we have to simply mark it with the '@future' keyword as shown in below snippet.

@future
Public static void futureMethod(){
//Your Code Here
}


Points to remember while creating @future methods:
1. future methods must always be static methods.
2. @future methods cannot return anything and its return type should always be void.
3. The input parameters to such methods can only be primitive types, arrays of primitive type or collections of primitive types.
4. Input parameters cannot be sobjects or collection of sobjects.

@future methods can also be used to make callouts to external services. To use this we must mark callout=true specifically. If it is not specified then the default value is taken as false.

@future(callout=true)

public static void myfuturecalloutmethid(){
//code to call external services
}


Why are sObjects not allowed as parameters in case of @future methods?
It is because the sobjects may change by the time the method is executed, in such cases the @future method may overwrite the already saved values of the sObject record.

What is the workaround for above sobject case?
We can pass the object Id as parameter and then query the record within the @future method and then work on the latest data.

Limitations of @future method:
1. You cant call a future method from another future method.
2. You can call up to 50 @future methods per transaction.
3. You can call up to 250000 future methods per 24 hours. This is in conjunction with all types of asynchronous methods like batch apex.
4. @future methods are not executed during salesforce downtime and any already running jobs during the start of downtime are halted and restarted after the downtime.

Precautions that we need to follow while using future methods:
1. Make sure you dont put too much in a future method as this will increase the execution time and may lead to delaying of other requests after the organisation limit of 2000 unprocessed future jobs is hit.
2. Opt for batch apex when you are processing large no of records.
3. Test your future methods with maximum possible data as this will give you an idea of how much the job may take during real time.

Sunday, 6 March 2016

Salesforce Lightning component example

In this post we will see a basic lightning component example and will also include it on a salesforce 1 app. Lightning framework uses Java script on the client side and apex on the server side. Lightning components can be used on salesforce 1 mobile as well as for dekstop(Lightning Experience).

Let us build a simple search page and make it available for salesforce 1 app. Page will have 2 input boxes: "Opportunity stage" and "Amount" and will also include a search button. Pressing the button will show all the opportunities with that stage and having amount greater than the input value. We will also include navigation facility to the searched opportunities.

To begin with we will have to first enable custom domain for your org after which you can start building lightning component. You can visit here (https://developer.salesforce.com/trailhead/project/quickstart-lightning-components/quickstart-lightning-components1) to see how to enable custom domain for your org.  To build the component we will need lightning component, client side controller (Java script) and server side controller (apex class). These things can be coded from developer console.
We can start with lightning component first but since it has dependencies with other two we can write the server controller first, then the client side controller and lightning component in the end. Or can also start with lightning component and build the others hand in hand.

Within Developer console you can navigate to File->New->Lightning Component to create the new lightning component.
Once you have this in new tab within developer console you will get option on right hand side to create client side controller (Controller) and you can create new server side controller by navigating to file->new->apex class

Lighting Component


Lightning component as you see above starts and ends with aura tag. Since we want to use the component on salesforce 1 we will have to overide it on a tab and hence
implements="force:appHostable" has been used which allows overriding a tab with the lightning component. We need two input boxes for which inputText has been used and Ui button that calls the java script controller function. <aura:iteration has been used to iterate through all the opportunities.


Client Side Controller


Client side controller uses the inputs from UI and calls the server side controller which does the searching and passes the results which are then set on UI table. Function navigaterecord has been used to provide a facility to navigate to the records displayed in table. The lightning component are referenced using v. (view) and server side methods variable can be referenced via c. (controller)

Server Side Controller


This is a simple apex class with method having @AuraEnabled annotation which enables it for use in lightning component. Method simply queries the opportunities based on the received parameters and returns the list of opportunities back to the client side controller.

To include this component on salesforce 1 create a new lightning tab and override it with this lightning component and include the tab in mobile navigating setup. You can install any browser extension for salesforce 1 to see the results or from salesforce 1 app on your mobile. The output image above is from add on extension for chrome.

Sunday, 25 October 2015

REST API Integration Salesforce apex

In this post we will see a working example for REST API integration. Generally when we use webservice or rest api we use it to integrate our salesforce instance with other systems. For a fresh salesforce developer having little or no knowledge about other programming languages like java, .net or other systems it becomes little difficult to get the real feel of integration specially if you haven't had the opportunity to work on real projects related to integration. Considering the dilemma of such fresh developers, i am writing this article which will demo REST api integration between two salesforce orgs. 

In the example, we will be writing a apex class exposed as REST api in one org and in the second org we will consume that particular class. This will help us in learning both exposing and consuming REST API. Before we write the code let us see what pre-requisites are needed. 

First you will need two different salesforce developer orgs. Secondly, create a remote site setting with the url 'login.salesforce.com' in the source org. Let us write our REST api class that we want to expose in the target org.

@RestResource(urlMapping='/createNewAccountUsingRestAPI/*')
global with sharing class RESTCreateNewAccount{

    @HttpPost

    global static string createAccountRecord(String name) {
     string succError;
     account acc = new account();
     acc.name = name;      
     insert acc;
     acc.accountsource = 'Other';
     update acc;
     succError = 'New account has been created. Account Record id is:'+acc.id;
     return succError;
    }
 
    @httpput
    global static string updateAccountRecord(String name) {
       string returnmessage = 'Account are updated successfully';
       List<account> accupdateList = [select name,id,AccountSource from account where name =: name];
       for(account acc : accupdateList){
           acc.AccountSource = 'Web' ;
           returnmessage = returnmessage +acc.id;
       }
       try{
         update accupdateList;
       }
       catch(exception ex){
         returnmessage = ex.getmessage();
       }
       return returnmessage ;
    }          
}

Let us try to understand above class, specifically alien things(if you see closely theres only annotations that we need to understand rest of the things we use normally in our classes or controllers). Class has first line as @RestResource(urlMapping='/createNewAccountUsingRestAPI/*')

annotation '@RestResource' means you are exposing the class as REST api so that methods from this class can be called from outer systems. Secondly we have
--> urlMapping='/createNewAccountUsingRestAPI/*  this specifies the url that outer system needs to use to call that particular class.

Other annotations that we have used include @HttpPost and @HttpPut now this are being used against each method. @HttpPost is used to indicate that the method will have code to a dml that will create/insert new records on the other hand @HttpPut specifies that the code in that method will update the records (While writing the methods you have to make sure you follow these things other wise the code will give run time exception so make sure you only do update in @HttpPut method and so on). Apart from these method annotations we also have @Httpdelete and @HttpPatch annotations. Apart from annotations we also have to make sure the class is global for obvious reasons, class will be called by outside world.

In our @HttpPost method if you see closely we have written the code to do only a single and simple transactions of inserting one record which actually can be also be done by simply passing the information over a single url by outside system, but for sake for simplicity and understanding have used a REST exposed class in target org. Once we have the REST exposed class in target org its time to write a class in other org that would call above class. If you see the two methods above they create a new record and update an existing record, so when we will call 'post' method from our other org it will actually create the new account record in our target org.

Lets write a class in other org that will create a new account record in target org (by calling the exposed REST api class),

public class RestAPIcalloutExample{

   Public void loginToOtherOrgAndCreaetNewAccount(){

        /*Login to Other Salesforce Org to grab session id - begin*/
        HTTP h1 = new HTTP();
        HttpRequest request = new HttpRequest();      
        request.setEndpoint('https://login.salesforce.com/services/Soap/u/22.0');
        request.setMethod('POST');
        request.setHeader('Content-Type', 'text/xml;charset=UTF-8');
        request.setHeader('SOAPAction', '""');
        request.setBody('<Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/"><Header/><Body><login xmlns="urn:partner.soap.sforce.com"><username>' + 'myusername@cloudforece.com'+ '</username><password>' +'my_password1234'+ '</password></login></Body></Envelope>');
        String SERVER_URL;
        String SESSION_ID;
        HTTPResponse loginResponse = h1.send(request);     
        Dom.Document doc = loginResponse.getBodyDocument();
        Dom.XMLNode receivedXml= doc.getRootElement();
        SESSION_ID = receivedXml.getChildElement('Body', 'http://schemas.xmlsoap.org/soap/envelope/').getChildElement('loginResponse', 'urn:partner.soap.sforce.com').getChildElement('result','urn:partner.soap.sforce.com').getChildElement('sessionId','urn:partner.soap.sforce.com') .getText();      
        /*Login to Other salesforce Org to grab session id - finish*/
     
        /* Send https request to the custom REST API defined in other org that has urlmapping as 'createNewAccountUsingRestAPI' -begin*/
        HTTP h2 = new HTTP();
        HttpRequest request2CreateAccount = new HttpRequest();
        request2CreateAccount.setMethod('POST');
        request2CreateAccount.setHeader('Content-Type', 'application/json;charset=UTF-8');
        request2CreateAccount.setHeader('Accept', 'application/json');
        request2CreateAccount.setHeader('SOAPAction', '""');
        request2CreateAccount.setbody('{"name":"Cloudforce4u Technologies Ltd"}');
            request2CreateAccount.setEndpoint('https://ap1.salesforce.com/services/apexrest/createNewAccountUsingRestAPI');
        request2CreateAccount.setHeader('Authorization', 'OAuth '+SESSION_ID);             
        HTTPResponse resppnceFromOtherSFOrg = h2.send(request2CreateAccount);
        /* Send https request to the custom REST API defined in other org that hass urlmapping as updateaccountrec - finish*/
        system.debug('**-Output-**'+resppnceFromOtherSFOrg.getbody());
   }
 }

Before we decode each line of above class lets write point wise what we we need to do so as to call the target org exposed REST api class. It is obviously not the way (create instance of the class and then call that method)we call normal method from different classes within salesforce orgs.

1. Programatically login to the target org using login url, username and password
2. Once you are logged in try to get the session id
3. call the exposed class method using http callout

Now lets decode the whole code for each of the 3 steps separately.
1. Programatically  login to the target org using login url, username and password
You can login to salesforce org either by soap api or by REST api but since here we are discussing REST lets use REST to login and 

Subsequently get the session id. So basically in REST we will have to create a instance of http, httpresponse and then send the request over http. Now to send this http request what all things need to set? below are the details
a. End point url (this will the login url)
b. Kind of method you want to call POST,PUT etc (we have set it to POST that means we we will be calling our post method from target org)
c. Header like body content type, it could be json or xml in our case we are using xml.
d. set body of the request which will have username,password

Once our request is ready we need to send it over http and store the response in httpresponse variable.

2. Once you are logged in try to get the session id
Once we get response in httpresponse variable its time to segregate the response and get the real thing out of it. The response will have many things but we should take out the exact thing/data that we need, in this case it is the session id. When we sent the login request in step 1 it logged in and whenever you log in to slaesforce it automatically creates a session id, this session id will be needed while sending the actual request to call the POST method in target org. Now, how do we get the session id from response? To do that we need to parse the response xml. This is done by getting the body element and getting the root elements (you can go through articles which give insights above parsing xml). Once we have the session id we are ready to call the exposed REST class from target org through http callout.

3.  Call the exposed class method using http callout
This will be similar to step 2 except few changes. End point url in this case is as below
 request2CreateAccount.setEndpoint('https://ap1.salesforce.com/services/apexrest/createNewAccountUsingRestAPI');
This url specifies that we are calling REST api exposed class  (https://ap1.salesforce.com/services/apexrest) and then specifies the class name.
Now the question is how does it which method to call? It will call post method 'createAccountRecord' because we have set setmethod as 'POST'         
request2CreateAccount.setMethod('POST'); 
to call put method just change POST to PUT         
request2CreateAccount.setMethod('PUT')

Next thing that we have set is authorisation -        
request2CreateAccount.setHeader('Authorization', 'OAuth '+SESSION_ID);
This where we need session id and this was the reason we used first http callout to login into salesforce org. We also need to set the body of the request, in this case have set to json. Make sure you are sending the target org method parameter in the body. 

Once you have both the classes saved and remote site setting created in target org just execute the method in other org using developer console.

RestAPIcalloutExample restcall = new RestAPIcalloutExample(); 
restcall.loginToOtherOrgAndCreaetNewAccount();

New account record would be created in target org, you can see the id of the record using debug log.
 system.debug('**-Output-**'+resppnceFromOtherSFOrg.getbody());