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
Board salesforceBoard salesforce 

after insert and after update in apex class

Hi Guys ,

I have a Requirement please help me out
I have account with 5 checkbox fields   and contact objects with 5 checkbox fields  ,on contacts their is a vf overrided ,when i check the  checkboxes whenever i insert and updates in  contact then it has to be updated in account with according to checkboxes  automatically?


please help me out!!!

Thanks ,
Ram
Best Answer chosen by Board salesforce
Abhishek BansalAbhishek Bansal
Hi,

In order to achieve your requirement you have to create a trigger on your contact object that will fire each time when your contact is inserted and updated.
I have written a trigger as per your requirement.
Please find the code below :
trigger updateCheckboxesOnAccount on Contact(after insert, after update){
	Set<id> accIds = new Set<Id>();
	for(Contact con : trigger.new){
		if(con.AccountId != null){
			accIds.add(con.AccountId);
		}
	}
	
	Map<Id,Account> mapOfAccounts = new Map<Id,Account>([Select checkBox1__c,checkBox2__c,checkBox3__c,checkBox4__c,checkBox5__c from Account where id IN : accIds]);
	
	for(Contact con : trigger.new){
		if(mapOfAccounts.containsKey(con.AccountId)){
			mapOfAccounts.get(con.AccountId).checkBox1__c = con.checkBox1__c;
			mapOfAccounts.get(con.AccountId).checkBox2__c = con.checkBox2__c;
			mapOfAccounts.get(con.AccountId).checkBox3__c = con.checkBox3__c;
			mapOfAccounts.get(con.AccountId).checkBox4__c = con.checkBox4__c;
			mapOfAccounts.get(con.AccountId).checkBox5__c = con.checkBox5__c;
		}
	}
	
	update mapOfAccounts.values();
}
//Please replace following variables with their API names :
//1. checkBox1__c with checkbox API name in contact and Account
//2. checkBox2__c with checkbox API name in contact and Account
//3. checkBox3__c with checkbox API name in contact and Account
//4. checkBox4__c with checkbox API name in contact and Account
//5. checkBox5__c with checkbox API name in contact and Account

Please let me know if you have any issue with the above trigger code or if you need any further help on this.

Thanks,
Abhishek Bansal.

All Answers

ProlayProlay
Your requirement is not clear. Elaborate it
Phillip SouthernPhillip Southern
Hi, you don't need apex for this, you can use Process Builder to do cross object field updates from Contact to Account.
Even if updating your Contacts is done through Visualforce, the insert/update transaction will still trigger Process Builder flows.
Board salesforceBoard salesforce
Thanks For the Reply
Prolay
I think it is  clearly Mentioned that i have 5 checkboxes in account as well contact when i insert a new contact and update the existing contact then the updates has to be affected on to the account checkboxes automatically that is checked or unchecked things hope u understand

Thanks For the Reply Philip Southern

i have some validations over it and error messages , need a vf with class  thanks for the suggestion

 
Abhishek BansalAbhishek Bansal
Hi,

In order to achieve your requirement you have to create a trigger on your contact object that will fire each time when your contact is inserted and updated.
I have written a trigger as per your requirement.
Please find the code below :
trigger updateCheckboxesOnAccount on Contact(after insert, after update){
	Set<id> accIds = new Set<Id>();
	for(Contact con : trigger.new){
		if(con.AccountId != null){
			accIds.add(con.AccountId);
		}
	}
	
	Map<Id,Account> mapOfAccounts = new Map<Id,Account>([Select checkBox1__c,checkBox2__c,checkBox3__c,checkBox4__c,checkBox5__c from Account where id IN : accIds]);
	
	for(Contact con : trigger.new){
		if(mapOfAccounts.containsKey(con.AccountId)){
			mapOfAccounts.get(con.AccountId).checkBox1__c = con.checkBox1__c;
			mapOfAccounts.get(con.AccountId).checkBox2__c = con.checkBox2__c;
			mapOfAccounts.get(con.AccountId).checkBox3__c = con.checkBox3__c;
			mapOfAccounts.get(con.AccountId).checkBox4__c = con.checkBox4__c;
			mapOfAccounts.get(con.AccountId).checkBox5__c = con.checkBox5__c;
		}
	}
	
	update mapOfAccounts.values();
}
//Please replace following variables with their API names :
//1. checkBox1__c with checkbox API name in contact and Account
//2. checkBox2__c with checkbox API name in contact and Account
//3. checkBox3__c with checkbox API name in contact and Account
//4. checkBox4__c with checkbox API name in contact and Account
//5. checkBox5__c with checkbox API name in contact and Account

Please let me know if you have any issue with the above trigger code or if you need any further help on this.

Thanks,
Abhishek Bansal.
This was selected as the best answer