You need to sign in to do that
Don't have an account?
stollmeyera
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?
Cheers ! Glad to be of help :D
All Answers
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);
}
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
o just one of them things - try opp.AccountId or opp.Account.Id
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?
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?
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!!
Cheers ! Glad to be of help :D
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!
Are you getting any errors? Or is the insert/update going through but it is not updating your custom field?
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
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.
Thanks, I had tried that before but got
Error: Variable does not exist: opp.Account
Whoops, misread my own trigger. Try this:
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?
Nevermind! I just changed the argument a bit. Here is the final code for anybody looking: