+ Start a Discussion
LloydSLloydS 

Simple opportunity trigger updating account fields

I'm creating a simple trigger to update a few fields of the related account object when an opportunity is updated. I've tried a few variations but none validate correctly. Here's the current version of the trigger. Thanks in advance for the lesson.

 

 

trigger AccountBecameClient on Opportunity (after update) { Opportunity myOpportunity = trigger.new[0]; Account myAccount = myOpportunity.AccountId; if (myOpportunity.StageName == 'Closed Won' && myAccount.First_Became_Client__c == Null) { myAccount.First_Became_Client__c = date.today(); myAccount.Next_Annual_Review__c = date.today() + 365; date thisDate = date.today(); date quarterlyReview = thisDate.addMonths(3); myAccount.Next_Quarterly_Review__c = quarterlyReview; } }

 

 

 

LloydSLloydS

I've been trying to read up on relationship queries thinking that was the more correct direction. So I changed the Apex code to what is below. Still doesn't work resulting in an error on line 3 of "Save error: illegal assignment from LIST:SOBJECT:Opportunity to SOBJECT:Account".

 

 

trigger AccountBecameClient on Opportunity (after update) { Opportunity myOpportunity = trigger.new[0]; Account myAccount = [select accountid from Opportunity where ID = :myOpportunity.Id]; if (myOpportunity.StageName == 'Closed Won' && myAccount.First_Became_Client__c == Null) { myAccount.First_Became_Client__c = date.today(); myAccount.Next_Annual_Review__c = date.today() + 365; date thisDate = date.today(); date quarterlyReview = thisDate.addMonths(3); myAccount.Next_Quarterly_Review__c = quarterlyReview; } }

 

 

 

sunil316sunil316

yes, in line 3 your qury is wrong.

Here, your making an object of Account(myAccount) and your querying on opportunity , it doesnt work.

LloydSLloydS
Yes. I understand there's an issue. I just can't figure out how to fix it with my limited knowledge. I tried:

Opportunity oppAcct = [select accountid from Opportunity where ID = :myOpportunity.Id]; Account myAccount = oppAcct;

 

but that didn't work either. Also tried Account myAccount = myOpportunity.accountId.
sunil316sunil316

You can write your trigger like this,

 

 

trigger AccountBecameClient on Opportunity (before update) {

   

Set<id> objSet = Set<id>();  

for(Opprtunity objOpp:trigger.new)

{

objSet.add(objOpp.AccountId);

}


List<Account> myAccount = [select Id, First_Became_Client__c, Next_Annual_Review__c, Next_Quarterly_Review__c from Account where Id in: objSet];

 

 

for(Opportunity objOpp:Trigger.new)

{

for(Account objAcc: myAccount)

{

if (objOpp.StageName == 'Closed Won' && objAcc.First_Became_Client__c == Null) {

{

objAcc.First_Became_Client__c = date.today(); objAcc.Next_Annual_Review__c = date.today() + 365; date thisDate = date.today(); date quarterlyReview = thisDate.addMonths(3); objAcc.Next_Quarterly_Review__c = quarterlyReview; }

}}

}

 

I think this should work.

 

 

-Sunil 

 

LloydSLloydS

Thanks for the suggestion. Unfortunately it results in an unexpected token error '('.

 

Also, did you write it that way so it could work as a bulk trigger or is that the only way of getting the account class working on the opportunity trigger?

sunil316sunil316

yes, its a bulk enabled code.

There is just a bug, forgot to write new when, i'm creating an object of Set. 

 

 

trigger AccountBecameClient on Opportunity (before update) {   

Set<id> objSet = new Set<id>();  

for(Opprtunity objOpp:trigger.new)

{

objSet.add(objOpp.AccountId);

}

List<Account> myAccount = [select Id, First_Became_Client__c, Next_Annual_Review__c, Next_Quarterly_Review__c from Account where Id in: objSet];

for(Opportunity objOpp:Trigger.new)

{

for(Account objAcc: myAccount)

{

if (objOpp.StageName == 'Closed Won' && objAcc.First_Became_Client__c == Null) {

{

objAcc.First_Became_Client__c = date.today(); objAcc.Next_Annual_Review__c = date.today() + 365; date thisDate = date.today(); date quarterlyReview = thisDate.addMonths(3); objAcc.Next_Quarterly_Review__c = quarterlyReview; }

}}

} 

 

hope this should work.

 

 

-Sunil 

LloydSLloydS

Unfortunately, I got the error:  Problem: expecting right curly bracket, found 'EOF'.

 

It looked like there was an extra '{' after the if statement but if I remove it I still get an error.

 

Sorry this is such a frig and I appreciate your help.

sunil316sunil316
Can i see your final code?
LloydSLloydS

Here you go. Thanks.

 

 

trigger AccountBecameClient on Opportunity (before update) { Set<id> objSet = new Set<id>(); for(Opprtunity objOpp:trigger.new) { objSet.add(objOpp.AccountId); } List<Account> myAccount = [select Id, First_Became_Client__c, Next_Annual_Review__c, Next_Quarterly_Review__c from Account where Id in: objSet]; for(Opportunity objOpp:Trigger.new) { for(Account objAcc: myAccount) { if (objOpp.StageName == 'Closed Won' && objAcc.First_Became_Client__c == Null) { { objAcc.First_Became_Client__c = date.today(); objAcc.Next_Annual_Review__c = date.today() + 365; date thisDate = date.today(); date quarterlyReview = thisDate.addMonths(3); objAcc.Next_Quarterly_Review__c = quarterlyReview; } }} }