You need to sign in to do that
Don't have an account?

How to update related entity details using a trigger
Dear All,
I am new to Salesforce Apex coding, please provide me sample code for updating related entity details. While updating an Oppurtunity, I need to update the Account details of that Oppurtunity.
Thanks In Advance
Suresh N
I am new to Salesforce Apex coding, please provide me sample code for updating related entity details. While updating an Oppurtunity, I need to update the Account details of that Oppurtunity.
Thanks In Advance
Suresh N
<Account > a = new <account>();
For (oppurtunity o :Trigger.new)
{
a.account name=o.account name;
a.account number=o.account number;
\\other fields you need
}
database.insert(a);
}
this is how the code will look like for your requirement
Thanks for your quick replies..... Here is my final code, which is also working.
trigger Opportunity_AccountUpdateBeforeUpdate on Opportunity (before update) {
Account acc = new Account();
for(Opportunity objOpp:Trigger.new) {
acc.Id = objOpp.AccountId;
acc.Website = 'www.gmail.com';
}
database.update(acc);
}
Your code will work for one account, but if accounts are updated in bulk, the trigger will fail. Please make your trigger bulk compatible.
Thanks for your reply, I will follow ur code and modify according to my needs, can u please reply how I can test / check bulk update of accounts.
Here is a sample test class to check Bulk Account and Opportunity Load. You can modify this to add more opportunity to test larger batches as well. You might also want to run a loop to create and add opportunities to a list and then insert.
Vidhyasagaran Muralidharan code doesn't work your need since he has given the approach which work on insert.
Hargobind approach is correct and work on bulk update either.
the best part is hargobinds code keep the SOQL Queries out of the loop which is a standard guideline
Well done Hargobind [https://developer.salesforce.com/forums/ForumsProfile?communityId=09aF00000004HMG&userId=005F0000003Fd8p&showHeader=false] .
Keep it up. You rocks.
Please find the below code which describes a trigger on the contact object to update the fields on the account object.
I have done way back.
you can do the same for opportunity also.
Code:
************
trigger ConTrgToUpdateParent on Contact (after update) {
Map<Id,Contact> Mp = new Map<Id,Contact>();
List<Account> aLstToUpdate = new List<Account>();
//indexed for loop
for(integer i=0; i<trigger.new.size(); i++){
//condition to check if the phone value is changed
if(trigger.new[i].Phone != trigger.old[i].phone){
//Adding the Account Id's to a set
//aIds.add(trigger.new[i].AccountId);
// System.debug('Mp'+Mp.size()+'----'+Mp);
System.debug('trigger.old[i].AccountId : '+trigger.old[i].AccountId);
//System.debug('trigger.new[i] : '+trigger.new[i]);
Mp.put(trigger.old[i].AccountId,trigger.new[i]);
}
//Querying the Account records where the contact phone is changed..
for(Account acc :[Select Id,phone FROM Account WHERE Id =: Mp.keySet()]){
//updating the account phone with the contact changed phone value..
system.debug('Mp.get(acc.Id) :'+Mp.get(acc.Id));
acc.phone = Mp.get(acc.Id).phone;
//adding the account with changed phone to a list..
//update acc;//
aLstToUpdate.add(acc);
}
//updating the account list with new phone values
update aLstToUpdate;
}
}
trigger Opportunity_AccountUpdateBeforeUpdate on Opportunity (before update) {
List<Account> accountsToUpdate = new List<Account>();
for(Opportunity objOpp:Trigger.new) {
Account acc = new Account();
acc.Id = objOpp.AccountId;
acc.Website = 'www.gmail.com';
accountsToUpdate.add(acc);
}
update accountsToUpdate;
}