+ Start a Discussion
stollmeyerastollmeyera 

Trigger to Update Account upon Opportunity insert or update

I have been using SF for a couple of months (Enterprise edition) and am ready to take up Appex classes/triggers. However, I am just now writing my first trigger and, although I have spent countless hours searching through the discussion boards and Google, I can not get this to work.  What I need is a trigger that does the following

 

If Opportunity StageName = '4 - Closed Won' AND Hosting__c is greater than $0

Update Account.Type to Customer

 

Can anyone help guide me in the process?

Best Answer chosen by Admin (Salesforce Developers) 
Ritesh AswaneyRitesh Aswaney

Cheers ! Glad to be of help :D

All Answers

Ritesh AswaneyRitesh Aswaney

trigger OpportunityAfterInsertOrUpdate (after insert, after update)

{

List<Account> accsToUpdate = new List<Account>();

 

for(Opportunity opp : trigger.new)

{

If (opp.StageName = '4 - Closed Won' &&  opp.Hosting__c  > 0)

accsToUpdate.add(new Account(Id = opp.Account, Type = 'Customer'));

}

 

if (accsToUpdate != null && !accsToUpdate.isEmpty())

Database.update(accsToUpdate);

 

}

stollmeyerastollmeyera

Thanks for the reply, Ritesh.  I am glad to see that I was on the right track.

 

However, when imputting this in the Force.com IDE I get the following error message on line 6

line6 = accsToUpdate.add(new Account(Id = opp.Account, Type = 'Customer'));:

error = "Save error: Invalid initial expression type for field Account.Id, expecting: Id"

 

I should add that the error occurs at column 35 when adding directly into my SFDC sandbox

Ritesh AswaneyRitesh Aswaney

o just one of them things - try opp.AccountId or opp.Account.Id

stollmeyerastollmeyera

opp.Account.Id fixed it.  

 

New issue, this error is thrown:

"Compile Error: AND operator can only be applied to Boolean expressions at line 5 column 42"

 

LIne 4 column 42 is the start of the reference to the Hosting__c field.  Would it matter that this field is a Roll-up Summary field?

Ritesh AswaneyRitesh Aswaney

As long as Hosting__c can be compared to a number (0 in this case) - i.e. of the same type - it should be fine. What is the data type of this field?

stollmeyerastollmeyera

Data type is listed as Roll-Up summary with automatic calculation.  The field sum's the total amount of Products assigned to the opp, as long as they are in the "Hosting" product family.  

 

EDIT:

Was able to fix it by adding == instead of = on the opp.StageName part.  Final product:

 

trigger OpportunityUpdateAccount on Opportunity(after insert, after update) {
List<Account> accsToUpdate = new List<Account>();

for(Opportunity opp : trigger.new)
{if (opp.StageName == '4 - Closed Won' && opp.Hosting__c > 0)
accsToUpdate.add(new Account(Id = opp.Account.Id, Type = 'Customer'));
}

if (accsToUpdate != null && !accsToUpdate.isEmpty())
Database.update(accsToUpdate);

}

 

Thanks a lot for the help, Ritesh!!  Couldn't have done it without you!!

Ritesh AswaneyRitesh Aswaney

Cheers ! Glad to be of help :D

This was selected as the best answer
KitagawaSan1337KitagawaSan1337

I am trying to do something very similar... 

 

I would like to 'recreate' the last activity date field and would like to update a custome last activity date field when an opportunity is created or marked as stage == 'closed won' 

 

I have this so far:

 

trigger OpportunityUpdateAccount on Opportunity(after insert, after update) {

List<Account> accsToUpdate = new List<Account>();


for(Opportunity opp : trigger.new)

{if (opp.StageName == 'Closed Won')

accsToUpdate.add(new Account(Id = opp.Account.Id, Last_Activity_Date__c = Date.today()));

}


{if (Trigger.isInsert)

accsToUpdate.add(new Account(Id = opp.Account.Id, Last_Activity_Date__c = Date.today()));}
Update(accsToUpdate);
}

 

But I just can't get it to work. I am admittedly very bad with APEX... Any help / guidance would be greatly appreciated!

stollmeyerastollmeyera

Are you getting any errors?  Or is the insert/update going through but it is not updating your custom field?

KitagawaSan1337KitagawaSan1337

I am getting the following error: 

 

 

Apex trigger OpportunityUpdateAccount caused an unexpected exception, contact your administrator: OpportunityUpdateAccount: execution of AfterInsert caused by: System.DmlException: Update failed. First exception on row 0; first error: MISSING_ARGUMENT, Id not specified in an update call: []: Trigger.OpportunityUpdateAccount: line 12, column 1

 

Error: Invalid Data. 
Review all error messages below to correct your data.

 

Thanks! 

 

I am also getting this: 

Error: Variable does not exist: opp.Account.Id 

stollmeyerastollmeyera

You don't need to reference 'ID' when you call the Account ID from the opp.  Change 'opp.Account.ID' to 'opp.Account' and see what happens.  

KitagawaSan1337KitagawaSan1337

Thanks, I had tried that before but got 

 

Error: Variable does not exist: opp.Account

 

 

stollmeyerastollmeyera

Whoops, misread my own trigger.   Try this:

 

trigger OpportunityUpdateAccountdate on Opportunity(after insert, after update) {
List<Account> accsToUpdate = new List<Account>();

for(Opportunity opp : trigger.new)
{if (opp.StageName == '2 - Demo')
accsToUpdate.add(new Account(Id = opp.AccountID, Last_Activity_Date__c = Date.today()));
}

Update accsToUpdate;
}

 

KitagawaSan1337KitagawaSan1337

Thank you! 

 

I am just having issue with the last part of update either on insert or when updated and Stage = Closed Won. Can I not use if (Trigger.isInsert) to determine this? 

 

 

trigger OpportunityUpdateAccountdate on Opportunity(after insert, after update) {
List<Account> accsToUpdate = new List<Account>();

for(Opportunity opp : trigger.new)
{if (opp.StageName == 'Closed Won')
accsToUpdate.add(new Account(Id = opp.AccountID, Last_Activity_Date__c = Datetime.now()));
}

{if (Trigger.isInsert)
accsToUpdate.add(new Account(Id = opp.AccountID, Last_Activity_Date__c = Datetime.now()));
}

Update accsToUpdate;
}

 

KitagawaSan1337KitagawaSan1337

Nevermind! I just changed the argument a bit. Here is the final code for anybody looking: 

 

trigger OpportunityUpdateAccountdate on Opportunity(after insert, after update) {
List<Account> accsToUpdate = new List<Account>();

for(Opportunity opp : trigger.new)
{if (opp.StageName == 'Closed Won' || Trigger.isInsert)
accsToUpdate.add(new Account(Id = opp.AccountID, Last_Activity_Date__c = Datetime.now()));
}

Update accsToUpdate;
}