Tuesday, 8 May 2018

Create record in lightning

With the advancement of lightning, we are getting introduced to new features of lightning that helps in creating a better functionality in short span of time.
One of the example is force:createRecord.
To understand this lets go through a scenarion. We have a lightining component where we have a text box to enter account name and a button "Search".
On click of button we query the account records matching the name given in textbox and display in a form of table.
If it doesn't have any record with the name given in textbox then we want to create an account with going away from the lightning page.
In this case we use force:createRecord.

We create a button named as "Create Account". Onclick of the button we call a method in lightning controller.








Below is the code of the method.












If you notice, we have used an event ie "e.force:createRecord" and assign them some parameters and finally firing the event.
This opens account edit form in a popup with prepopulated value. You can input values and click on save. This will create a new account record in the database without hitting server apex class.


Parameters used in force:createrecord :-
1. Entityapiname --> In this we need to provide the Object Api name whose record we want to create. In our case this parameter value will be "Account".
2. defaultFieldValues --> If we want to prepopulate the field values in the popup while creating record, we can assign the value to the fieldname using this parameter.
3. recordTypeId --> If your object is using recordType thenyou can specify recordTypeId.

Use Lightning Component In VF Page

In this blog we will see how we can use the lightning component in visualforce page.
As a first step lets create a lightning component which takes account Id as input and display the Account Id in lightning component.

Lightning Component :-










Once the lightning component is created, we need to create a lightning app which will contain the component. The lightning app should have "ltng:outApp" interface. This interface makes the lightning app visible on visualforce page.

Lightning App :-






Once the lightning app is created, lets create a visualforce page that will display the lightning app. Below is the screenshot of the page.




In the above code, we are using <apex:includeLightning/> which enables the inclusion of lightning app on visualforce page. also there should be an empty div element which will contain the lightning component.
Once the page is loaded, we use $Lightning.use to load the lightning application. Then we use $Lightning.createComponent to initialize the lightning component dynamically in the page. During component initialization we pass 4 parameters ie :

1. In the first parameter Lightning component name.
2. In the second parameter we will assign the values to lightning component attributes.
3. In 3rd parameter we give the div section where we need to display the component.
4. In 4th parameter we will define a function that fires once the component is initialized.

In the above example you can notice that we are passing the account Id to the lightning component which will be displayed in the component.
the above example shows the complete process of using the Lightning component in visualforce page.

Friday, 16 March 2018

Custom labels in salesforce

Custom labels are used to store text values which can later be used in apex classes, triggers, visualforce pages etc.
These enables the developers to display text values in multiple languages to help customer to read texts in its native language.

For creating a custom label developer can go to setup --> custom labels --> new custom label



Enter the values and click save.

To access value in apex class and trigger use below syntax:-
system.label.Custom label api name

To access value in VF pages and lightning components use below syntax:-
{!$Label.Custom label api name}

Friday, 5 January 2018

Custom settings salesforce

Custom settings are like custom objects which is used to create and store set of data that can be used at different place in salesforce like apex classes, validation rules, formulae fields etc. The data stored in custom settings is exposed to application cache and can be accessed without hitting the queries to the database.

Custom settings are of two types :-
1. List custom settings :- This type of custom settings is used where we want to create a set of static data which will remain same irrespective of OWD, roles, profiles etc. These are sets of data which is very frequently used in application. Since these data is exposed in cache, it can be accessed very easily without using SOQL or SOSL query.
Example :- If you have set of cities with pincode and you want to access it frequently in application, in that case a list custom setting will be created and pincode and city data can be stored in it.

2. Hierarchy custom settings :- This type os custom setting is used when we want to specify different data value at oranganization level, profile level and user level. This custom setting follows an in build hierarchial logic which checks the field value first on user level then on profile level and then finally on organization level.
Example :- If you get a requirement where you need to set a default value in a formulae field. If the requirement speifies that the default value should be different for profile, user and organization level. In this type of scenario we can use hierarchy based custom setting.

Methods used for Hierarchy Custom setting :-
1. getInstance() --> This  method returns the custom setting data set record for current user.
2. getInstance(userId) --> This  method returns the custom setting data set record for userId provided in the method. The lowest level record values are returned. This method is used if you want to retrieve data speifically on user level.
3. getInstance(profileId) --> This  method returns the custom setting data set record for profileId provided in the method. The lowest level record values are returned. This method is used if you want to retrieve data speifically on profile level.
4. getOrgDefaults() --> This gives data set record for organization.
5. getValues(userId) --> Returns Custon setting data for the userid specified.
6. getValues(profileId) --> Returns Custon setting data for the profileId specified.

Methods used for List Custom setting :-
1. getAll() --> Returns the complete data stored in custom setting.
2. getInstance(dataSetName) --> Return the data set for the name passed in the method.
3. getValues(dataSetName) --> Return the data set for the name passed in the method.

Saturday, 25 November 2017

Heap Size In Salesforce

What is heap size?
Many times developers have faced this problem. It is very important to understand what is heap size in salesforce.
Heap size is the memory allocated to the objects created in apex to store data in it during run time.
In apex code whenever an object is created, certain memory is allocated to it dynamically during the runtime of the program.
This memory is used to store data in it.

Watch for heap size:-
1. use limit methods to get heapsize in at any instance of time during runtime.
   Limits.getHeapSize() --> Returns the amount of memory already used.
   Limits.getLimitHeapSize() --> Returns the total memory size allocated in the current context.

2. You can watch out for heap size in the debug log as shown below :-
   

How to avoid heap size errors :- 
1. Use soql for loop. This avoid heap size as we are not storing the data in any variable but directly processing it in the loop.
    example :- for(Account ac:[select id, name from account]){ // insert logic here}

2. Store required amount of data only. While doing a query ususaly we retrieve all fields which increases the heap size. One should only query those fields which they will work on.

3. Use local variables instead of class level variable to store large amount of data.
   storing data in local variable will go out of scope as soon as the method gets over. This helps in           cleaning heap size after use.

Wednesday, 19 July 2017

Workflow vs process builder

Many times we come across situations where we have to decide whether i should use workflow rules or process builder.
Below are few comparisons between them to help you to decide over the two.

Workflows :-
1. Can be used to fire an action when any record is created or updated.
2. Can be used to fire an action when the record subsequently meets the workflow criteria
   Eg:- We have a workflow rule which should fire an action when account type is "Hot".
   If we select "subsequently meets" option then the workflow will fire when account type field is changed from some value to "Hot".
3. To update a field on the record initiated the workflow or to update the parent record field.
4. To send email alerts when the workflow is fired.
5. To create a task on the record which initiated the workflow.
6. To create an outbound message.
   Outbound message are single line integrations to the third party system where we specify the      URL   and select the fields whose values we want to send along with the URL.
7. You can reuse already created workflow actions. 

Process Builder :-
1. Can be used to fire an action when any record is created or updated.
2. Subsequently meet the criteria option is not possible. you need to write formulae logic with ISCHANGED functions.
3. You can create IF ELSE condition logic in process builder and can schedule the actions.
4. Can do field updates for the records which initialted the process builder. Also you can update the field on child recrods as well as parent records.
5. You can call an apex class method from process builder. The method should be defined as @invocable method.
6. You can send email alerts from process builder.
7. You can initiate a flow from process builder provided the flow should be an autoflow. Auto flows are thos visual flows which doesn't have any screen in it.
8. You can initiate a an approval process associated to the record object.
9. A record can also be created from process builder.
10. You can create a chatter post form process builder and post it as user post, group post, record post etc.
11. You can initiate one process builder from another process builder.
12. you can use quick actions for the record object.

I hope this helps you to decide whether to use process builder or workflows.

Wednesday, 14 June 2017

Batch class salesforce

Batch apex is an interface given by salesforce to process large amount of data.
As we know, salesforce provides governer limits on data extraction and DML operations in classes.
In a SOQL query we can get only 50000 records and at once we can use only 10000 records in one DML operation.
Total number of DML operations allowed in apex class is 150 hence we can process upto 1500000 records in an apex class.
But to work on these many records in apex class we need to implement various complex loopings that will be tiring and time consuming job.
Also for large scale industries the amount of data is more than 1500000 hence this approach cannot be used effectively.

To overcome this problem, salesforce provides batch apex interface.
This interface allows to process 50 million records without any complex logic and looping to implement.

Batch is divided into 3 methods:- 
1. Query Locator :- this methods runs as soon as batch stats running. This method is used to query the required records from database using database.getQueryLocator.

global Database.queryLocator start(database.batchablecontext BC)
 {
   string query= 'select id, name from account';
   return Database.getqueryLocator(query);
 }

 In the above example first a dynamic query will be created which will then be passed in Database.getqueryLocator as parameter to get records from database.
 
2. Execute :- after fetching th records from database, the records are divided in batches and then each batch is processed in the execute.
   By default the batch size is 200 records but it can increased upto 2000 records in each batch.
   For example if the we get 1000 in query locator and batch size is defined as 200 records, then 5 batches will be created and execute method will process 5 times.
   
   global void execute(database.batchablecontext BC , List<SObject> scope)
{
  List<Account> acc = scope;
  for(Account a: acc)
  {
 
a.name= a.name+'batch class';
 
  }
  
  update acc;
}
In the above example scope variable contains list of records which will be processed in that execute method.
3. Finish :- Once all records are processed and execute is completely over, finish method will be called. This methods can be used in various scenarios like sending the batch status to a user after the records are processed.
    global void finish (database.batchablecontext bc)
{   
}
 
This method executes at the end of each batch process.

Create record in lightning

With the advancement of lightning, we are getting introduced to new features of lightning that helps in creating a better functionality in ...