function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Nuno.CarvalhoNuno.Carvalho 

Appex trigger , need help pls.

Hi guys,
Im trying to populate the account name lookup field on contracts with the right account , first of all i need to know if its possible to create this trigger because the account name lookup field is required and i can´t save it with any value as you guys know... 
You can ask me what you need to know , i will try my best to help you, help me :)
Amit Chaudhary 8Amit Chaudhary 8
On Contract object account field is required then no need to create trigger to populate account name field as without account name you can't save the record.

If base on some logic you want to populate account name then you can write trigger as well.

 
Shiva RajendranShiva Rajendran
Hi Nuno ,
If i'm right ,you are trying to populate account name from the contract's account object record associated with it right?

Create a custom field called AccountName on contract.

Trigger :
trigger ContractTrigger on Contract (after insert,after update) {
   

        if (Trigger.isInsert) {
//To avoid recursion
            if (ContractTriggerHandler.runAI()) {
            	ContractTriggerHandler.processAfterInsert(Trigger.new);    
            }
        } 
    else {
//To avoid recursion
            if (ContractTriggerHandler.runAU()) {
            	ContractTriggerHandler.processAfterUpdate(Trigger.new);    
            }
        }
}

TriggerHandlerClass :
 
public class ContractTriggerHandler {


    
    private static boolean runAfterInsert = true;
    private static boolean runAfterUpdate = true;
    
    public static boolean runAI() {
        if (runAfterInsert) {
            runAfterInsert = false;
            return true;
        } 
        else {
            return false;
        }
    }
     
    public static boolean runAU() {
        if (runAfterUpdate) {
            runAfterUpdate = false;
            return true;
        } else {
            return false; 
        }
    }
    
   
    
  
    
    public static void processAfterInsert(List<contract> Inputs) {
         List<contract> allContracts= [select id,accountId from contract where id in :Inputs];
    Set<Id> allAccountIds=new set<id>();
    for(contract cc:allContracts)
    {
        allAccountIds.add(cc.accountId);
    }
    Map<Id,Account> allAccountMap= new Map<Id,Account>([select id,name from account where id in :allAccountIds]);
    for(Contract conn:allContracts)
    {
        conn.shivamindtree__AccountName__c= allAccountMap.get(conn.accountId).name;
    }
    update allContracts;
        
        
       
    }
    
    public static void processAfterUpdate(List<contract> Inputs) {
         List<contract> allContracts= [select id,accountId from contract where id in :Inputs];
    Set<Id> allAccountIds=new set<id>();
    for(contract cc:allContracts)
    {
        allAccountIds.add(cc.accountId);
    }
    Map<Id,Account> allAccountMap= new Map<Id,Account>([select id,name from account where id in :allAccountIds]);
    for(Contract conn:allContracts)
    {
        conn.shivamindtree__AccountName__c= allAccountMap.get(conn.accountId).name;
    }
    update allContracts;
    }
}


This trigger will execute as expected.


Also one easy solution to your question is 

create a text formula field
eg :FormulaAccountNameField
 assign Account.Name to that field on creation.

Let me know if this solution helps you.
Thanks and Regards,
Shiva RV

 
Nuno.CarvalhoNuno.Carvalho
Hi shiva,
Where am i suppose to put the TriggerHandlerClass?
Sorry to bother you and thank you.
Shiva RajendranShiva Rajendran
Hi Nuno ,
Nothing as such as calling it as bother.I will be happy if i made you to understand the code and help you resolve in your issue.Infact every developer love's it. By the way TriggerHandlerClass is an public apex class.This kind of concept of delegating the logic is best practise for trigger writing.
Let me know if you need further help.
Thanks and Regards,
Shiva RV 
Nuno.CarvalhoNuno.Carvalho
Hi Shavan,
But do i put all the code in the same trigger?
 
Amit Chaudhary 8Amit Chaudhary 8
Hi Nuno,

Please create on apex class for TriggerHandlerClass  (ContractTriggerHandler )
Shiva RajendranShiva Rajendran
Hi Nuno ,
You can put all the code in the same trigger. But delegating is a better practise.
Also if you create a trigger like this trigger ContractTrigger on Contract (after update , before update)
then if you update the trigger in both before and after trigger ,then it will cause recursion error.
That is why the below code is used  
if (Trigger.isInsert) {
//To avoid recursion
            if (ContractTriggerHandler.runAI()) {
               ContractTriggerHandler.processAfterInsert(Trigger.new);   
            }
        }
    else {
//To avoid recursion
           if (ContractTriggerHandler.runAU()) {
                ContractTriggerHandler.processAfterUpdate(Trigger.new);   
            }
        }
}

else
you could have used 
if (Trigger.isInsert) {
 ContractTriggerHandler.processAfterInsert(Trigger.new);   // or simply could have written all the code logics here.
}

Thanks and Regards,
Shiva RV