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
Raksha JhaRaksha Jha 

Trigger on contract not working

trigger TriggerOnContract on Contract (after insert, after update, after delete, after undelete) {
    switch on Trigger.operationType{
        when AFTER_INSERT{
            //creating a new set of account Ids from contract whose status is active 
            set<Id> accountIds = new set<Id>();
            for(Contract contract:Trigger.new ){
                if(contract.Status == 'Activated'){
                    accountIds.add(contract.AccountId);
                }
            }
            //creating a list of contract of the selected account Ids of the set
            List<Contract> contracts=[SELECT Id, AccountId, Status, Productc, Property_Rentc FROM Contract 
                                      WHERE Status='Activated' AND AccountId IN:accountIds];

            //build final list of account to update
            List<Account> accountsToUpdate = new List<Account>();
            for(Contract contract:contracts){
                Account acct= new Account(Id = contract.AccountId, 
                                          Property_Namec = contract.Productc,
                                          Rent_Amountc = contract.Property_Rentc);

                accountsToUpdate.add(acct);
            }
            //update the final list of account
            update accountsToUpdate;

this is the trigger, i would like to update Property and rent field on Account form the active contract. account and contract are standard object . property and rent field on both the objects are custom field. this  is not working, how can i fix it. need help...Thanks!
Best Answer chosen by Raksha Jha
AnkaiahAnkaiah (Salesforce Developers) 
Hi Raksha,

Please try with below code.
 
trigger TriggerOnContract on Contract (after insert, after update, after delete, after undelete) {
    Map<Id, List<Contract>> mapAccountIdContractList = new Map<Id, List<Contract>>();
    Map<Id, List<Contract>> mapAccountIdDelContractList = new Map<Id, List<Contract>>();
    Set<Id> AccIds = new Set<Id>();    
    List<Account> AccountList = new List<Account>();
    
    if(trigger.isInsert) {
        for(Contract cs : trigger.New) {
            if(String.isNotBlank(cs.AccountId) && cs.status == 'Activated' ) {
                if(!mapAccountIdContractList.containsKey(cs.AccountId)) {
                    mapAccountIdContractList.put(cs.AccountId, new List<Contract>());
                }
                mapAccountIdContractList.get(cs.AccountId).add(cs); 
                AccIds.add(cs.AccountId);
            }   
        }  
    }
    
    if(trigger.isUpdate) {
        for(Contract cs : trigger.New) {
            if(String.isNotBlank(cs.AccountId) && cs.AccountId != trigger.oldMap.get(cs.Id).AccountId && cs.status == 'Activated') {
                if(!mapAccountIdContractList.containsKey(cs.AccountId)){
                    mapAccountIdContractList.put(cs.AccountId, new List<Contract>());
                }
                mapAccountIdContractList.get(cs.AccountId).add(cs); 
                AccIds.add(cs.AccountId);
            } else if(String.isBlank(cs.AccountId) && String.isNotBlank(trigger.oldMap.get(cs.Id).AccountId)) {
                if(!mapAccountIdDelContractList.containsKey(cs.AccountId)){
                    mapAccountIdDelContractList.put(cs.AccountId, new List<Contract>());
                }
                mapAccountIdDelContractList.get(cs.AccountId).add(cs);   
                AccIds.add(trigger.oldMap.get(cs.Id).AccountId);
            }
        }  
    }
    
    if(trigger.isUndelete) {
        for(Contract cs : trigger.new) {
            if(String.isNotBlank(cs.AccountId)){
                if(!mapAccountIdContractList.containsKey(cs.AccountId)){
                    mapAccountIdContractList.put(cs.AccountId, new List<Contract>());
                }
                mapAccountIdContractList.get(cs.AccountId).add(cs);     
                AccIds.add(cs.AccountId);
            }
        }  
    }      

    if(trigger.isDelete) {
        for(Contract cs : trigger.Old) {
            if(String.isNotBlank(cs.AccountId)){
                if(!mapAccountIdDelContractList.containsKey(cs.AccountId)){
                    mapAccountIdDelContractList.put(cs.AccountId, new List<Contract>());
                }
                mapAccountIdDelContractList.get(cs.AccountId).add(cs);    
                AccIds.add(cs.AccountId); 
            }
        }  
    }   
    
    if(AccIds.size() > 0) {
        AccountList = [SELECT Id, Number_of_Contracts__c,Property_Name__c,Rent_Amount__c FROM Account WHERE Id IN : AccIds];
        
        for(Account Con : AccountList) {
            if(mapAccountIdContractList.containsKey(Con.Id)) {
				con.Property_Name__c = mapAccountIdContractList.get(Con.Id).Property_Name__c;
				con.Rent_Amount__c = mapAccountIdContractList.get(Con.Id).Rent_Amount__c;
            }
            if(mapAccountIdDelContractList.containsKey(Con.Id)) {
                con.Property_Name__c = '';
				con.Rent_Amount__c = 0;
            }
            
        }
        
        update AccountList;    
    }
}

If this helps, Please mark it as best answer.

Regards,
Ankaiah Bandi​​​​​​​

All Answers

AnkaiahAnkaiah (Salesforce Developers) 
Hi Raksha,

Please try with below code.
 
trigger TriggerOnContract on Contract (after insert, after update, after delete, after undelete) {
    Map<Id, List<Contract>> mapAccountIdContractList = new Map<Id, List<Contract>>();
    Map<Id, List<Contract>> mapAccountIdDelContractList = new Map<Id, List<Contract>>();
    Set<Id> AccIds = new Set<Id>();    
    List<Account> AccountList = new List<Account>();
    
    if(trigger.isInsert) {
        for(Contract cs : trigger.New) {
            if(String.isNotBlank(cs.AccountId) && cs.status == 'Activated' ) {
                if(!mapAccountIdContractList.containsKey(cs.AccountId)) {
                    mapAccountIdContractList.put(cs.AccountId, new List<Contract>());
                }
                mapAccountIdContractList.get(cs.AccountId).add(cs); 
                AccIds.add(cs.AccountId);
            }   
        }  
    }
    
    if(trigger.isUpdate) {
        for(Contract cs : trigger.New) {
            if(String.isNotBlank(cs.AccountId) && cs.AccountId != trigger.oldMap.get(cs.Id).AccountId && cs.status == 'Activated') {
                if(!mapAccountIdContractList.containsKey(cs.AccountId)){
                    mapAccountIdContractList.put(cs.AccountId, new List<Contract>());
                }
                mapAccountIdContractList.get(cs.AccountId).add(cs); 
                AccIds.add(cs.AccountId);
            } else if(String.isBlank(cs.AccountId) && String.isNotBlank(trigger.oldMap.get(cs.Id).AccountId)) {
                if(!mapAccountIdDelContractList.containsKey(cs.AccountId)){
                    mapAccountIdDelContractList.put(cs.AccountId, new List<Contract>());
                }
                mapAccountIdDelContractList.get(cs.AccountId).add(cs);   
                AccIds.add(trigger.oldMap.get(cs.Id).AccountId);
            }
        }  
    }
    
    if(trigger.isUndelete) {
        for(Contract cs : trigger.new) {
            if(String.isNotBlank(cs.AccountId)){
                if(!mapAccountIdContractList.containsKey(cs.AccountId)){
                    mapAccountIdContractList.put(cs.AccountId, new List<Contract>());
                }
                mapAccountIdContractList.get(cs.AccountId).add(cs);     
                AccIds.add(cs.AccountId);
            }
        }  
    }      

    if(trigger.isDelete) {
        for(Contract cs : trigger.Old) {
            if(String.isNotBlank(cs.AccountId)){
                if(!mapAccountIdDelContractList.containsKey(cs.AccountId)){
                    mapAccountIdDelContractList.put(cs.AccountId, new List<Contract>());
                }
                mapAccountIdDelContractList.get(cs.AccountId).add(cs);    
                AccIds.add(cs.AccountId); 
            }
        }  
    }   
    
    if(AccIds.size() > 0) {
        AccountList = [SELECT Id, Number_of_Contracts__c,Property_Name__c,Rent_Amount__c FROM Account WHERE Id IN : AccIds];
        
        for(Account Con : AccountList) {
            if(mapAccountIdContractList.containsKey(Con.Id)) {
				con.Property_Name__c = mapAccountIdContractList.get(Con.Id).Property_Name__c;
				con.Rent_Amount__c = mapAccountIdContractList.get(Con.Id).Rent_Amount__c;
            }
            if(mapAccountIdDelContractList.containsKey(Con.Id)) {
                con.Property_Name__c = '';
				con.Rent_Amount__c = 0;
            }
            
        }
        
        update AccountList;    
    }
}

If this helps, Please mark it as best answer.

Regards,
Ankaiah Bandi​​​​​​​
This was selected as the best answer
Raksha JhaRaksha Jha
Thanks for your response but it is not working. Just thinking, As field will be updated after the contract will become active after approval. so i think trigger should be on after insert.  I am a beginner, so dont have much idea. Thanks again!