Wednesday 30 December 2015

Before Insert Trigger

As discussed before we have clear idea about  trigger and context variable .Lets start before insert trigger .
 Basically trigger is the most important part in  project ,why because when ever we will do any DML the trigger will execute always .We can restrict execution of code inside but not the trigger .
Still trigger should always bulkified so that all data should mapped properly ,If data will wrong then there is no meaning of software development .To maintain data we should write proper trigger  code .

Trigger BeforeInsertTriggerDemo on Account(before insert ){
   for(Account acc :Trigger.new ){
     acc.Name='Before'+Name;

    }
}

Lets have some discussion on the above code .
Line number one which basically we can say syntax of trigger .or signature of trigger .
All trigger should start with Trigger key word next to the trigger is a Name of trigger . then on which object you need to write trigger that object name you need to add after on .
Name of the trigger should be a justified name which we can  give up to 255 character,

Object name you need to give the api name in which object you need to write trigger . 
Next to object name you need to add the event within the bracket .

Here I have added before insert, however we can add all seven event separated by comma .
 Next line is for loop ,why for loop we all developer knowingly or unknowingly writing this line .  
 As  Trigger.new is of type list which contains the list of manipulated records ,So we are implementing list iterator for loop in apex here .
Basically in case of  before insert   the record is not inserted in database .So we can assign any value to the fields  .
Second line the loop will iterate based on your record size .Suppose are going to insert 1000 account record then your loop will iterate 1000 times .

Now as per 3rd line each records name will prefixed by Before context .


We can add validation in trigger also .Observe  below trigger .Here I have added a validation that from a multiselect picklist you can only  select 2 values .

trigger MultiSelectPickListRestriction  on Account (before insert,before update ) {
  for (Account acc :Trigger.new ){
    if(acc.MultiPickList__c.split(';').size() >2 ){
        acc.addError('You can only select any two values');
    }
  }
}


Lets check what is not available in before insert context .
Basically in before insert context id and audit fields are not available .Why because the record is not yet inserted in database .So once record will save to data base then only those fields will available .

Some people says DML is not possible in before context .I think it is partially correct .Because DML is not possible in same object or record ,however if you have any relationship(Master Detail /Lookup ) fields then you can collect the id and do DML on those records .

One more thing which we have already discussed in before insert there is not concept of Map as Id is not there in before context .Also no old concepts as record is completely new .
 
I think before insert part is clear .we will discuss before update next very soon  enjoy  :)

 
 



No comments:

Post a Comment