Saturday, December 1, 2018

Apex Trigger On Account To Fetch Parent Hierarchy

Trigger : Use Case - Fetch Parent Hierarchy on Account Object. 

APEX Trigger to Fetch the Parent Hierarchy of a Particular Account and Populates all the Parent Names in a Custom Field on Account.
Before Writing a Trigger, Make sure that you create a Custom Text Field on Account Object.

Here I have Created a Parent_Hierarchy__c Custom Text Field on Account Object.

Source code

 /*   
  * Developer : Dev Pro  
  * Use Case : Trigger To Fetch Parent Hierarchy, and Update All Parent Names on Child Account (Custom Field : Parent_Hierarchy__c )  
  */  
 trigger ParentHierarchy on Account (before update, before insert)   
 {  
   for(Account currentAcc : Trigger.new)  
   {  
     Account acc = trigger.new[0];  
     String ParentName = 'Parent Names : ' +acc.Name+ ',' ;  
     //String parentName = acc.Name;  
     //String parentName = acc.Name + acc.Id;  
     if(acc.ParentId!=null)  
     {  
       do{  
         acc = [SELECT ID, Name, ParentId FROM Account WHERE Id = : acc.ParentId];  
         //parentName += acc.Name + acc.Id;  
         parentName += acc.Name;  
         System.debug('Parent Names : ' +acc.Name+',');  
       }while(acc.ParentId!=null);  
     }   
     trigger.new[0].Parent_Hierarchy__c = ParentName;  
   }  
 }  

Trigger On Contact Object - Auto Create Account When New Contact is Created

Trigger : Use Case - Auto Create Account When New Contact is Created

Description : This Trigger is to Create an Account When ever New Contact is Created.
Acceptance Criteria : Create New Account With Contact First Name + Contact Last Name = Account Name.

Append Both First Name & Last Name of an Contact to create new Account automatically.
Create New Contact
Account Created with Contact : First Name + Last Name

Source code

 /*   
  * Developer : Dev Pro  
  * Use Case : Trigger To Create Account When ever New Contact is Created.   
         Account Name Must Be Append with Contact First Name + Last Name   
  */  
 trigger AutoCreateAccountFromContact on Contact (before insert)   
 {  
   //Collect List Of Contacts Being Inserted without an Account  
   List createAccounts = new List();  
   for (Contact con : trigger.new)   
   {  
     if (String.isBlank(con.accountid))   
     {  
       createAccounts.add(con);  
     }  
   }  
   if (createAccounts.size() > 0)   
   {  
    List newAccounts = new List();  
    Map contactsByNameKeys = new Map();  
     //Create Account For Each Contact  
     for (Contact con : createAccounts)   
     {  
       String accountName = con.firstname + ' ' + con.lastname;  
       contactsByNameKeys.put(accountName,con);  
       Account acc = new Account(Name = accountName);  
       newAccounts.add(acc);  
     }  
     Insert newAccounts;  
     for (Account acc : newAccounts)   
     {  
       //Put Account Id's on Contacts  
       if (contactsByNameKeys.containsKey(acc.Name))   
       {  
         contactsByNameKeys.get(acc.Name).accountId = acc.Id;  
       }  
     }  
   }   
 }