Learn Salesforce Trigger in a Simple way Part 2

In the previous post, we learn the basics of the trigger.Today we are going to learn about it in the detail with the help of an example.In this post, we also going to tell you about the use of the List and Set.

Learn Salesforce Trigger in a Simple way Part 2

But as per the rules and regulations, we must have the basic knowledge of what we are going to learn in details.So here are the basics of List and Set:

List: It is used to store more than one number of records in it with duplicity.Syntax: List<ObjectName> referenceName = new List<ObjectName>();

List has various methods to be used in it and these are:

  • add(listElement)
    Adds an element to the end of the list.
  • add(index, listElement)
    Inserts an element into the list at the specified index position.
  • addAll(fromList)
    Adds all of the elements in the specified list to the list that calls the method. Both lists must be of the same type.
  • addAll(fromSet)
    Add all of the elements in specified set to the list that calls the method. The set and the list must be of the same type.
  • clear()
    Removes all elements from a list, consequently setting the list’s length to zero.
  • clone()
    Makes a duplicate copy of a list.
  • deepClone(preserveId, preserveReadonlyTimestamps, preserveAutonumber)
    Makes a duplicate copy of a list of sObject records, including the sObject records themselves.
  • equals(list2)
    Compares this list with the specified list and returns trueif both lists are equal; otherwise, returns false.
  • get(index)
    Returns the list element stored at the specified index.
  • getSObjectType()
    Returns the token of the sObject type that makes up a list of sObjects.
  • hashCode()
    Returns the hashcode corresponding to this list and its contents.
  • isEmpty()
    Returns true if the list has zero elements.
  • iterator()
    Returns an instance of an iterator for this list.
  • remove(index)
    Removes the list element stored at the specified index, returning the element that was removed.
  • set(index, listElement)
    Sets the specified value for the element at the given index.
  • size()
    Returns the number of elements in the list.
  • sort()
    Sorts the items in the list in ascending order.

 

Set: This is same as List but removes duplicacy i.e. it stores Uniquely identified records. It has all the methods that are used in List.

 

Now the example to understand the concept of these things.In the example, we are creating related records or can say that we are creating Contacts of a particular Account.

 

trigger createContact on Account (after insert, after update){

    List<contact> contlist=new List<contact>();

    for(Account a:Trigger.new){

        integer i=0;

        if(a.No_of_Contact__c!=null)

            if((trigger.isUpdate && a.No_of_Contact__c != Trigger.oldMap.get(a.id).No_of_Contact__c) || trigger.isInsert)

                while(i<a.No_of_Contact__c){

                    contact c=new contact();

                    c.LastName='Cont Of '+a.Name+'-'+i;

                    c.AccountId=a.id;

                    contlist.add(c);

                    i++;

                }

       

    }

   

    if(contlist.size()>0)

        insert contlist;

}

 

In this code, we make a list of contact which can stre more than one record at a time.Then there is this for loop of Account which fetch the data from trigger.new.

In the for loop first we are checking if the account field No_of_Contact__c is not null,if it is not then it will go inside the loop and check for the second if in which trigger.isUpdate operation is fired and then checking for the previous value of the field if it is not as same as current value and then go fir the insertion part.after the validation of this if loop the contacts will insert or update as per the value given in the field like If it is 2 then two contact created.

In the end it will check if the contact list is not empty then it will insert all the contact records in the org.

 

if(contlist.size()>0)

        insert contlist;

 

That’s all for this post. We’ll talk further about it in the more complex example in the next post or may be in the continuation of it.

To watch the video go to the link below:

A Salesforce Developer at AlmaMate Info Tech PVT LTD. An Aligarian and also your query solver in database field.This site mark the difference as Black or White to make you choose the best for you. Don't feel pressurized, feel confident..!!

Leave a reply:

Your email address will not be published.