The post Writing a Simple Apex Trigger !! appeared first on Salesforce Next Gen.
]]>Now we know where to write a Trigger, let’s begin with a simple example. In this scenario I would like to add a custom text in the last name of a contact.
Let’s have a look at the basic structure of a trigger
trigger MyCustomTriggerName on CustomObject__c (trigger_events) {
Line of codes to execute(Business Logic)
}
Using the above-mentioned structure we will write our code, in the above code we can give Trigger any name we want, and since we want the trigger to be on contact standard object, the structure of trigger looks like below
trigger ContactTestTrigger on contact (before insert) {
Line of codes to execute(Business Logic)
}
Now that we have defined the basic structure of the trigger now we will write the business logic as discussed in the beginning.
trigger ContactTestTrigger on contact (before insert) {
for(contact con : Trigger.new){
con.lastname = ‘New Last Name using Trigger’;
}
}
This is it, we have written a simple Trigger which initiates at before insert event of a contact. Based on the occurrence of the DML event, there are broadly two types of Triggers; Before Trigger and After Trigger. Further, the trigger can be classified into the below groups based on the events.
In Trigger, we can modify the data without explicitly calling any DML operation, and if the operation is performed then it results in an error. The record is committed to the database once the trigger has completed its execution
Here you also see the use of a variable Trigger.new in the for loop, it is a trigger’s context variable. Trigger’s context variable holds the values of the record which initiated the execution of the trigger, for e.g. Trigger.New contains the list of contacts which initiated the trigger. There are some Trigger’s context variables which return Boolean values and with the help of context variables, we can determine which event fired the trigger. There are many context variable available in Salesforce and can be studied in detail here.
This concludes how to write a Simple Trigger
Next, I would like to present, how to call apex class method in trigger, callout, and using trigger exceptions
find interview Questions on Trigger here
Interview Questions Apex Trigger
The post Writing a Simple Apex Trigger !! appeared first on Salesforce Next Gen.
]]>The post Batch class : Salesforce Interview Question appeared first on Salesforce Next Gen.
]]>What is Batch Apex?
It is a class which is capable of accepting records in bulk and then depending upon the batch size the group of records are broken into Batches and are processed asynchronously.
When should we use a batch class?
We should batch apex when we are dealing with records in bulk mainly the record size above 10000 and cannot be handled by the normal DML operations.
What is the basic structure of a Batch class?
the basic structure of a batch consists of three methods:
Describe each method of a batch class?
Start method:
Syntax: global Database.QueryLocator start(Database.BatchableContext BC)
This method receives the batchable context variable as an argument and returns a query locator variable.
Execute method:
Syntax: global void execute(Database.BatchableContext BC, List<Account> scope)
This method receives the batchable context variable and an additional argument as scope which of list of subject type we are working with.
Finish method:
Syntax: global void finish(Database.BatchableContext BC)
This method receives the batchable context variable and is the last method which gets executed after all the batches have been processed, we can perform all the post execute logic in this method like sending an email etc.
What is a scheduler class?
A scheduler class implements the Schedulable interface and have to implement its execute method.
how to schedule a batch class ?
there are two ways to schedule a batch class:-
By Developer console we have to pass a cron variable like below:-
AScheduleClass A = new AScheduleClass();
String schedule = ’20 30 8 10 2 ?’; // cron object
String jobID = system.schedule(‘Merge Job’, schedule, A);
Via System Scheduler:
Can we call a batch class from another batch class?
Yes, we can call one batch class from another batch class but only if it is called from its finish method.
Can we call a Future method from batch class?
no, we cannot directly call a future method from a batch class.
What is Database.Stateful used for in batch class?
As batch class runs asynchronously, to maintain the state of a variable across each batch we implement Database.Stateful.
How many can concurrent batch jobs be added to the queue?
At most, there can be 5 bath jobs queued at a time.
How can we track the status of the current running batch job?
The job Id returned by the batchable context variable helps us in finding the status of a batch through AsyncApexJob.Status.
Q . What is the purpose of start method in Batch Class?
Start object returns the list or group of records on which we are going to make changes.
What is database.getQuerylocator and what are its advantages?
It is used to fetch records from the database, if used in batch class it can fetch up to 50 millions records.
******* More questions to come
Want to learn more about batch Classes and Asynchronous Apex have a look at link below
The post Batch class : Salesforce Interview Question appeared first on Salesforce Next Gen.
]]>The post Salesforce Interview Questions on Trigger appeared first on Salesforce Next Gen.
]]>What is a Trigger?
Trigger can invoke Apex code, they come in handy when we want to perform some custom task just before or after a record is either created updated or deleted.
What are the various event on which a trigger can fire?
A trigger is a set of statement which can be executed on the following events:
Broadly classify the Trigger?
Triggers can be broadly classified as before or after Trigger.
What are the considerations while implementing the Triggers?
Consider the following before implementing the triggers.
Read the Apex Developer Guide for more detailed considerations.
Write the syntax of Apex Trigger?
Trigger TName On ObjName(name the events){
……. Apex code here ……..
}
What are context variables in regards to trigger?
Following are the context variable available in triggers. Please note variable’s availability varies according to the type of trigger events.
How is Trigger.New Different from Trigger.newMap?
Trigger.New variable returns the list of sObject which has invoked the trigger and Trigger.NewMap returns the map of ID’s with the newly entered records. NewMap is only available in after insert, before and after the update and after undelete.
How is Trigger.new different from Trigger.old?
Trigger.New variable returns the list of sObject which has invoked the trigger and Trigger.old returns a list of the older versions of the records which have invoked the trigger. Trigger.Old is only available in update and delete events
Can a trigger call a batch class?
Yes, we can call a batch class in the trigger as we do in the normal apex code.
Can a trigger make a call to Apex callout method?
we can call a callout method in Apex Trigger but the only condition is that it has to be an asynchronous callout because the trigger flow cannot wait on the response received by the callout method.
Define Recursive Trigger and how to avoid it?
There is a possibility that the result of the trigger can end up calling the same trigger again and can run in a loop, this is known as a recursive trigger. To avoid this scenario we should create a static variable and check the value of this variable before we execute anything in the trigger.
What do you mean by the bulkifying trigger?
A trigger that can handle both single record and thousands of record. It is capable of handling mass action.
Is there any limit on number of triggers define on an object?
We can define as many triggers on an object as we want but it is recommended to have one trigger per object because the order of execution of different trigger is not guaranteed and any trigger can fire first.
Can you explain the order of execution in Triggers?
Following is the order of execution of events which Salesforce perform before a DML Event.
If the request originates other than UI then Salesforce only checks for Validation of foreign keys.
The post Salesforce Interview Questions on Trigger appeared first on Salesforce Next Gen.
]]>The post Salesforce Interview Questions and Answers Part 1 appeared first on Salesforce Next Gen.
]]>A. Salesforce is a Cloud based software made on Force.com platform, it has a pre-built feature to serve as a CRM, but Salesforce is not limited to be called CRM, It has many out of the box features. Salesforce uses Multitenant architecture
2. What is Multitenancy?
A. It is the methodology in which all users share IT related resources to make it cost effective even for a small business. It helps in reducing the initial cost of starting a new.
Images sourced from site https://developer.salesforce.com/page/Multi_Tenant_Architecture
3. Can you explain multi-tenant Data model?
A. To deal with a tremendous, constantly changing arrangement of the genuine database, Force.com implements it storage model with the help of metadata, data and pivot table as described in the diagram below.
Images sourced from site https://developer.salesforce.com/page/Multi_Tenant_Architecture
4. Describe an object in Salesforce? How many types of objects are there in Salesforce?
A. They are similar to database tables of an organization and are used to store a record in Salesforce. Salesforce has provided us with some predefined objects which are known as standard objects and Salesforce has provided us with the ability to create new objects which are known as the Custom object.
5. What is the distinguishing factor between the standard object and custom object?
A. API name of Custom objects contains a suffix of ‘__c’. It is added automatically by Salesforce. E.g customobjectName__c
6. What is a field and how many types of field are there in Salesforce?
A. There are 4 types of fields, 1) Compound Field 2) system Field 3) required Field 4) custom field
7. What is a relationship field in Salesforce and how many types of relationships are there in Salesforce?
A. A relationship field is the one which associates one object with another object. A relationship can be formed between standard objects, custom objects and also between custom and standard objects. Salesforce provides three type of relationships, 1) Lookup 2) Master-Detail Relationship 3) External Relationship, the first two are the most common relationship used throughout the Salesforce.
8. The difference between lookup and master-detail relationship?
A. There are fundamental differences in the behavior and record sharing that you need to read up on.
The most important difference is that master-detail has a direct dependency between the objects:
You cannot have a detail record without a master.
The detail record inherits sharing rules from the master.
The maximum number of master-detail relationships are limited to two.
All related detail objects are deleted if the master object is deleted.
Lookups are used when the behavior of the master-detail – particularly around sharing rules, profile permissions and cascade delete are not required.
when multiple parents need to be referenced then we use look-up
9. Define Self Relationship in Salesforce?
A. It is a look up Relation to the same object.
10. How many types of Object Relationships are supported by Salesforce?
A. Salesforce supports three types of object relationship: 1) one -to- one 2) one – to – many 3) many – to – many
11. What is a trigger, when do we use before and after trigger?
A. A trigger is the flow of code which initiates before or after a DML Event. i.e. before insert or after update etc. We use before trigger if we want to process data before the record is committed and after trigger where we want to use the values from the committed record to change the value in other object’s record.
Salesforce interview Questions based Apex Collections
Apex Collections are set of identical elements grouped together in a collection. A collection can be of three types list, set and map. Interview related questions as per the ones which I have faced during my interviews.
Q.What are the types of collections which are supported by salesforce?
A. Salesforce supports three types of collections
Q. What are the difference between list, set and maps?
A. There are many differences between these collections.
Q. what is the difference between add() and addAll() method?
A add() is used to add only one element in the collection and addAll() is used to add a collection to another similar collection.
Q. how to check whether a list is null or empty?
A. We check a list is empty or null by checking following condition/
If((listObj != null)&&(list.size()>0))
****More Questions to come
Next: Salesforce Interview Question and Answers Part 2
The post Salesforce Interview Questions and Answers Part 1 appeared first on Salesforce Next Gen.
]]>