You need to sign in to do that
Don't have an account?
GMASJ
How to update account from contacts
Hi,
How to update account and parent account when contact is created or updated.
Account A has custom field NSE Count
========
Contact 1 has custom field NSE1 which is boolean If NSE1 = true
Contact 2 has custom field NSE1 which is boolean if NSE1 = true
When any contact is updated it should update the Account A NSE Count = 2
Also if Account A has parent and grand parent it must rollup adding NSE Count
Please suggest me the logic how to obtain this in apex.
Thanks
Sudhir
How to update account and parent account when contact is created or updated.
Account A has custom field NSE Count
========
Contact 1 has custom field NSE1 which is boolean If NSE1 = true
Contact 2 has custom field NSE1 which is boolean if NSE1 = true
When any contact is updated it should update the Account A NSE Count = 2
Also if Account A has parent and grand parent it must rollup adding NSE Count
Please suggest me the logic how to obtain this in apex.
Thanks
Sudhir
For first part you can write trigger on Contact. I have tried one, let me know if it works for you
trigger UpdateAccountField on Contact (before insert, before Update) {
HelperClassUpdateAccountField.updateAccountField(trigger.new, trigger.newMap, trigger.oldMap);
}
public class HelperClassUpdateAccountField {
public static void updateAccountField(List<Contact> conList, Map<ID, Contact> newMap, Map<ID, Contact> OldMap){
List<Contact> ContactWithFieldChanged = new List<Contact>();
for(Contact con : conList){
if(con.NSE1__c != OldMap.get(con.id).NSE1__c){
ContactWithFieldChanged.add(con);
}
}
if(ContactWithFieldChanged.size() == 0) return;
List<Account> UpdateContact = new List<Account>();
for(Contact con : ContactWithFieldChanged){
if(con.NSE1__c == true){
con.Account.NSE_Count__c += 1;
}
else{
con.Account.NSE_Count__c -= 1;
}
UpdateContact.add(con.Account);
}
update UpdateContact;
}
}
For Second part I think rollup summary should work.
Rahul
Thank you very much for your reply. I tried your code there seem to be some issue. I am getting below error
Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger ContactTrigger caused an unexpected exception, contact your administrator: ContactTrigger: execution of BeforeUpdate caused by: System.NullPointerException: Attempt to de-reference a null object: ()
In below helper class I just modified the field names to actual fields
Trigger
Please suggest me even the roll up part how to add the logic it will great help from your side.
Thanks
Sudhir
I wrote some code which is working need your suggestion on this code for rollup to parent and grand parent
Below code is working only for one contact it update to parent account
If account have two contacts it is not working
Example.
Account A NSE_1_Status__c shold be = 2
Contact A NSE_1__c = true
Contact B c.NSE_1__c = true
Please suggest
Thanks
Sudhir
Sorry i did not test the code before positing it. I have modified it. Still I have not considered all the scenarios and Bulk test. If you need help with test Class, do let me know. May I also ask what is the business functionality here, May be I will be able to help more.
Also in below code change NSE1 to NSE2.
Best Regards,
Rahul Singh Rana
You code is working now but it is not rolling up the value to parent and grand parent Please suggest me how to achive this soloution.
Also your code is not rolling up the multiple contacts to same account as I gave example
Example.
Account A NSE_1_Status__c shold be = 2
if Contact A NSE_1__c = true
if Contact B c.NSE_1__c = true
Both the contact value should be added to account same logic it should travers to Account A parent and its grand top parent.
Thank you very much for your help.
Thanks
Sudhir
Parent to Grand Parent functionality I have not included(as I did not understand your requirment), but the code I have checked it before posting. it is rolling up in my Org, my be you have some trigger or functionity that prevents this trigger from adding, thats why I asked the Business Requirement and flow to understand your situation better.
Best Regards,
Rahul Singh Rana
Thank you very much for you reply and help here is the complete requirement please see below image.
In Account we have a custom field NSE_1_Status__c (Integer data type)
In Contact we have a custom field NSE_1__c ( Boolean data type)
In salesforce we have account hierarchy that is how below image account are hierarchy displayed.
When contact is created or updated and NSE_1__c boolean value is changed it must roolup the count to account and parent accounts
Your code is working only thing it is not rolling up Please let me know if your still not clear I can explain
Thanks
Sudhir
You can add 1 function like below to
and pass values
or
But this code will only cover if parent is alreay present. For senario in which parent is created, you may want to write similar trigger on Account Object. Also, I would suggest you to put default value on NSE_Count__c, may be 0, so that it does not run into nullPointerException.
In contact trigger I also suggest that you add a validate for account or add some funtionality to skip it may be something in trigger that checks it and then only calls the helper class, as the above prevents contact from saving, if no account is present.
You can also consider process builder for your requirment. Do you really have to use trigger?
Let me know if you need futher assitance.
Best Regrds,
Rahul
Thanks for your reply I executed you code it is updating only the first parent its not updating the grand parent and top parent. Please see the logic its only till parent
Thanks
Sudhir
Thanks for your reply Sorru I am bit not clear on the logic can you please give me a complete code with just a logic how your to update parenet hierachy
Thanks
Sudhir