You need to sign in to do that
Don't have an account?
kali charan
Trigger on Update parent account based on child contact
Hi everyone,
I am newbie to salesforce and i want help to update the parent account of amount field when ever the contact of amount field is inserted or updated.and i have the scenario like when ever the contact is created or updated the amount field of account should be auto populated this is the first scenario. The second scenario is when the 2nd contact is created if the amount field is greater the first amount the greater amount field should be populated on the account amount field. The 3rd scenario is there are two check boxes greater amount and lesser amount in the account object this should be populated with the latest contact whether it should be greater or lesser than the previous account.
The fields are
Account object:
Parent_amount__c = Text field
Greater_amount__c = checkbox
Lesser_Amount__c = checkbox
Contact object:
Amount__c
I have written some code only for update thing and please help me for second and third scenarios.
The 1st scenario is also giving error for me.
trigger UpdateAmount on Contact (after insert, after update) { //You want it on update too, right?
Map<ID, Account> parentOpps = new Map<ID, Account>(); //Making it a map instead of list for easier lookup
List<Id> listIds = new List<Id>();
for (contact childObj : Trigger.new) {
listIds.add(childObj.Account.id);
}
//Populate the map. Also make sure you select the field you want to update, amount
//The child relationship is more likely called Quotes__r (not Quote__r) but check
//You only need to select the child quotes if you are going to do something for example checking whether the quote in the trigger is the latest
parentOpps = new Map<Id, account>([SELECT id, Parent_amount__c,Greater_amount__c,Lesser_Amount__c,Name,(SELECT ID, Amount__c FROM contacts) FROM Account WHERE ID IN :listIds]);
for (contact con: Trigger.new){
Account myParentOpp = parentOpps.get(con.Account.id);
myParentOpp.Parent_amount__c = con.Amount__c;
}
update parentOpps.values();
}
The error is
Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger UpdateAmount caused an unexpected exception, contact your administrator: UpdateAmount: execution of AfterInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.UpdateAmount: line 16, column 1
Please help in fixing the bug and please suggest me for 2nd and 3rd scenarios
Thanks.
I am newbie to salesforce and i want help to update the parent account of amount field when ever the contact of amount field is inserted or updated.and i have the scenario like when ever the contact is created or updated the amount field of account should be auto populated this is the first scenario. The second scenario is when the 2nd contact is created if the amount field is greater the first amount the greater amount field should be populated on the account amount field. The 3rd scenario is there are two check boxes greater amount and lesser amount in the account object this should be populated with the latest contact whether it should be greater or lesser than the previous account.
The fields are
Account object:
Parent_amount__c = Text field
Greater_amount__c = checkbox
Lesser_Amount__c = checkbox
Contact object:
Amount__c
I have written some code only for update thing and please help me for second and third scenarios.
The 1st scenario is also giving error for me.
trigger UpdateAmount on Contact (after insert, after update) { //You want it on update too, right?
Map<ID, Account> parentOpps = new Map<ID, Account>(); //Making it a map instead of list for easier lookup
List<Id> listIds = new List<Id>();
for (contact childObj : Trigger.new) {
listIds.add(childObj.Account.id);
}
//Populate the map. Also make sure you select the field you want to update, amount
//The child relationship is more likely called Quotes__r (not Quote__r) but check
//You only need to select the child quotes if you are going to do something for example checking whether the quote in the trigger is the latest
parentOpps = new Map<Id, account>([SELECT id, Parent_amount__c,Greater_amount__c,Lesser_Amount__c,Name,(SELECT ID, Amount__c FROM contacts) FROM Account WHERE ID IN :listIds]);
for (contact con: Trigger.new){
Account myParentOpp = parentOpps.get(con.Account.id);
myParentOpp.Parent_amount__c = con.Amount__c;
}
update parentOpps.values();
}
The error is
Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger UpdateAmount caused an unexpected exception, contact your administrator: UpdateAmount: execution of AfterInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.UpdateAmount: line 16, column 1
Please help in fixing the bug and please suggest me for 2nd and 3rd scenarios
Thanks.
Looks like I added too many brackets and used .contains instead of .containsKey.
All Answers
Hey Kali,
Just FYI, I do not think triggers come with relationships passed the immediate relationship. Notice in this line ::
This will give you the id of the account. I'm willing to bet that Account myParentOpp = parentOpps.get(con.Account.id); is your null object. Check this out by putting this in front of it :: This will ensure that you actually have it in your map. Throw some debugs in there to help you figure out what is going on.You are trying to grab the id of the account. Instead of that, directly reference the name of the relationship. For instance, if the name of the relationship was just called 'Account' you would do something like this ::
For instance
Then take a look at the debug log, see if anything is there for parentOpps.
Good luck!
-Sean
Ahh, my bad Kali!
For this scenerio, you would want this on update / insert of contact trigger.
For the update, you'd want to check to see if the amount has changed. Compare the old value and the new value. If it has changed, you'll end up doing the same thing you do on the insert trigger.
I didn't notice a scenerio for if the amounts were equal to one another.
Good luck!
-Sean
trigger UpdateAmount on Contact (after insert, after update) { //You want it on update too, right?
Map<ID, Account> parentOpps = new Map<ID, Account>(); //Making it a map instead of list for easier lookup
List<Id> listIds = new List<Id>();
for (contact childObj : Trigger.new) {
listIds.add(childObj.AccountID);
}
parentOpps = new Map<Id, account>([SELECT id, Parent_amount__c,Greater_amount__c,Lesser_Amount__c,Name,(SELECT ID, Amount__c FROM contacts) FROM Account WHERE ID IN :listIds]);
for (contact con: Trigger.new){
Account myParentOpp = parentOpps.get(con.AccountID);
myParentOpp.Parent_amount__c = con.Amount__c;
}
if(parentOpps.size()>0){
update parentOpps.values();
}
}
Please reply me back.
When I was first learning to program in Apex, I would put debug statements everywhere to help me figure out what was going on. I'd advise the same for you as well!
Good luck in your coding endeavors!
-Sean
Looks like I added too many brackets and used .contains instead of .containsKey.
You need to set it to false when there is a lesser amount then.