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
Rohit KumawatRohit Kumawat 

trigger to delete related contact according to account field value changed

Hi All,

I want to create  contacts as per field value no of location in account and also if i update the no of location field value .. if new value is more than previous then it create new contacts as per difference between new and old value of account feld. 

and if the new value is less than old value then it delete the related contact based on difference between new and old value.

 

 

AbhishekAbhishek (Salesforce Developers) 
Rohit,

You need to write the Trigger on Contact, because if a new Contact is created or deleted, the change in count must be reflected in the Parent Account.


You can try this,

trigger CountContactOnAccount on Contact (after INSERT, after UPDATE, after DELETE ) {
Set <Id> accountIds = new Set <Id>();
List <Account> lstAccountsToUpdate = new List <Account>();
 if(Trigger.isInsert){
    for(Contact con:trigger.new){
        accountIds.add(con.accountID);
    }
}
if(Trigger.isUpdate|| Trigger.isDelete){
    for(Contact con:trigger.old){
        accountIds.add(con.accountID);
    }
}

for(Account acc:[SELECT Id,Name,Count_Contact__c,(Select Id from Contacts) from Account where Id IN: accountIds]){
    Account accObj = new Account ();
    accObj.Id = acc.Id;
    accObj.Count_Contact__c = acc.Contacts.size();
    lstAccountsToUpdate.add(accObj);
}

UPDATE lstAccountsToUpdate;


Maybe you need to make some minor changes based on your requirement.

Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.

Thanks.
Rohit KumawatRohit Kumawat
@Abhishek,

I think you have not understood my requirment. Your trigger is to count no of contacts associated with account. But my scenerio is: 
example : when i create account and i enter the value in custom field no of location = 3 then it will create three contacts as well
but when i update the value from 3 to 5 it will create two more contacts 
and if i update the value from 5 to 2 it will delete three contacts
Malika Pathak 9Malika Pathak 9

Hi Rohit,

Please Find The Solution of your questions trigger to delete related contact according to account field value changed.

trigger FirstQuesTrigger on Account (after insert,after update) {
    
    if(trigger.isAfter){
        if(trigger.isInsert ){
            FirstQuesTriggerHandler.addContactRecords(Trigger.new);
            
        }
         else if(trigger.isUpdate){
            FirstQuesTriggerHandlerUpdate.updateAccount(Trigger.new);
        }
    }

}


//After Insert

public class FirstQuesTriggerHandler {
    
    public static void addContactRecords(List<Account> accList){
        list<Contact> conList=new List<Contact>();
        integer s=0;
    	for(Account ac:accList){
            integer i=0;
      	    s=ac.Number_of_location__c;
            system.debug(s);
            if(s>0){
                while(i<s){
                    contact con = new contact();
                	con.AccountId=ac.Id;
                	con.LastName = 'Indigo'+i;

                	conList.add(con);
                	i++;
                }
            }
        }
       if(conList.size()>0)
            insert conList;
        system.debug(conList+ 'updated');
    	
    } 

}


//After Update

public class FirstQuesTriggerHandlerUpdate {
    public static void updateAccount(List <Account> upAccList){
        List <Account> accList = [select id,Number_of_location__c,(select id from contacts) from Account  where id in: upAccList ];
        list<Contact> conList=new List<Contact>();
        list <Contact> conList11 =new List <Contact>();
        integer s=0;
        for(Account ac:accList){
            if(ac.Number_of_location__c>=ac.contacts.size()){
                s=ac.Number_of_location__c- ac.contacts.size();
                for(integer i=0;i<s;i++){
                    contact con = new contact();
                	con.AccountId=ac.Id;
                	con.LastName = 'Indigo'+i;
                	conList.add(con);

                }
            
            }
            else{
                s=ac.contacts.size()-ac.Number_of_location__c;
                for(integer i=0;i<s;i++){
                    conList11.add(ac.contacts[i]);
                    
                }
            }
        }
        
        if(conList.size() !=null)
            insert conList;
        
        if(conList11.size()!=null)
            Delete conList11;
            
        
        }
    }

If It will help you then Please Mark it Best Answer so that other people would take reference from it.

Thanks