Friday, November 30, 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;  
       }  
     }  
   }   
 }  

Saturday, November 17, 2018

Apex Trigger For Creating a Related Record on Account Object

Trigger On Account Object To Create a Related Record 

Hi Friends. . . 😄😄😄
Hope you are all doing well.
Today I'm going to discuss on How to Create a Related record on any Object (Standard / Custom) In Salesforce.

Suppose, we are creating a New Account & We need to have a related records like Contact, Opportunity . . . 
For this kind of Situations, we will use APEX Trigger that will solve all the Business use cases.
Here we are going to create a Trigger for creating the Related records on Account Object.

USE CASE : Trigger For Creating the Related Record ( Opportunity Record ) When ever New Account is Created or If an Account is Already created but NO OPPORTUNITIES are created for that Account then will create a Related Opportunity for that Account (Existing Accounts).

 /*  
 Description : Create New Opportunity on Account (Related Record) when no Opportunity was created before to that Account.  
 This Trigger Even Used for Creating an Opportunity When ever New Account was created.  
 */  
 trigger AddRelatedRecord on Account(after insert, after update)   
 {  
   //Create Opportunity List To Store the Opportunities   
   List<Opportunity> opportunityList = new List<Opportunity>();  
   //Get Related Opportunity Records For The Accounts  
   Map<Id,Account> accountsWithOpp = new Map<Id,Account>  
   ([SELECT Id,(SELECT Id FROM Opportunities) FROM Account WHERE Id IN :Trigger.New]);  
   // Check for All the Accounts & If Any Accounts doesn't have the Opportunity   
   Then ADD The Related Opportunity to that account.  
   // Iterate Through Each Account.  
   for(Account acc : Trigger.New) {  
     System.debug('accountsWithOpp.get(acc.Id).Opportunities.size()=' + accountsWithOpp.get(acc.Id).Opportunities.size());  
     // Check if the account already has a related opportunity.  
     if (accountsWithOpp.get(acc.Id).Opportunities.size() == 0) {  
       // If it doesn't have then, add Default opportunity  
       opportunityList.add(new Opportunity(Name=acc.Name + ' Opportunity',  
                     StageName='Prospecting',  
                     CloseDate=System.today().addMonths(2),  
                     AccountId=acc.Id));  
     }        
   }  
   if (opportunityList.size() > 0) {  
     insert opportunityList;  
   }  
 }