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

Cross Object Update Trigger
I am very new to salesforce, and even newer to Apex. I'm trying to update a field on my account object, after an opportunity has been closed - won. Here is my first attempt;
trigger AccountTypeUpdate on Opportunity (after update) {
Account myAccount=trigger.update[0];
if (opportunity.StageName = 'A - Closed Won') {
myAccount.type = 'Customer';
update myAccount;
} //else nothing
}
Evidently it doesn't work, and I realize I would have to select the account associated with that opportunity, I'm just not sure how to go about doing that. Any help would be greatly appreciated.
Modified your code-
trigger AccountTypeUpdate on Opportunity (after update) {
list<Account> accounts = new List<Account>();
Set<Account> accSet = new Set<Account>();
Map<ID,Account> accMap = new Map<ID,Account>();
for(Opportunity opp : Trigger.new)
{
if (opp.StageName = 'A - Closed Won')
{
accSet.add(opp.AccountId);
}
// myAccount.type = 'Customer';
// update myAccount;
//}
}
accounts = [Select ID, type from Account where ID in :accSet];
for(Opportunity opp : Trigger.new)
{
for(Account acc:accounts)
{
if(opp.AccountId == acc.Id && opp.StageName = 'A - Closed Won')
acc..type = 'Customer';
}
}
if(accounts.size() > 0)
update accounts;
}
Cool_D
Opportunity oppty = trigger.new[0]; Account acct = new Account(Id = oppty.AccountId); acct.name = 'my_acct'; update acct;
Hey Cool-D..,
I modified your code to fit my case - I'm trying to update the StageName of the opportunity to "Won" when the Contract is activated. Below is what I've taken and modified from your previously provided code - but I'm getting an error when I'm trying to save it. I too and a novice and new to this...can you, or someone, review if possible and point out the error of my ways?
Bernie in Tucson
trigger OpportunityStageUpdate on Contract (after update) {
list<Opportunity> opportunities = new List<Opportunity>();
Set<Opportunity> oppSet = new Set<Opportunity>();
Map<ID,Opportunity> oppMap = new Map<ID,Opportunity>();
for(Contract cont : Trigger.new)
{
if (cont.Status = 'Activated')
{
oppSet.add(con.OpportunityId);
}
// myOpportunity.StageName = 'Won';
// update myOpportunity;
//}
}
opportunities = [Select ID, type from Opportunity where ID in :oppSet];
for(Contract cont : Trigger.new)
{
for(Opportunity opp:opportunities)
{
if(cont.OpportunityId == opp.Id && Cont.Status = 'Activated')
opp..StageName = 'Won';
}
}
if(opportunities.size() > 0)
update opportunities;
}