You need to sign in to do that
Don't have an account?
Cola
Cheers
Trigger on Opportunity to check all related Opportunities in Account
I'm currently trying to create a trigger that runs whenever an Opportunity is created or updated. The trigger needs to check all the other Opportunities related to the Account of the Opportunity being updated. It should check to see if any of the Opportunities have a StageName equal to 'Closed Won - One Time' or 'Closed Won - Recurring' and if so, it should update the account Type to 'Customer'. If none of the Opportunities are closed won, the Account Type should be 'Prospect'.
This is the code I have but I'm not sure if I'm querying the right things:
trigger updateAccountIfOppCustomer on Opportunity (after insert, after update) { List<Account> accts = new List<Account>(); List<Opportunity> opps = new List<Opportunity>(); for (Opportunity opp : Trigger.new) { accts = [SELECT Id, Name, Type FROM Account WHERE Id =: opp.AccountId LIMIT 1]; opps = [SELECT Id, AccountId, StageName, Account.Type FROM Opportunity WHERE AccountId =: opp.AccountId]; } for (Account a : accts) { for (Opportunity o : opps) { if (o.StageName == 'Closed Won - One Time' || o.StageName == 'Closed Won - Recurring' || o.StageName == 'Customer Reseller') { if (a == null) { a = new Account(Id = o.AccountId, name='TestingName'); } a.Type = 'Customer'; } else { a.Type = 'Prospect'; } } } update accts; }Any help is much appreciated.
Cheers
Hope it'll help you.
All Answers
Here is how
1. Create a number field on the Opportunity (default = 0)
2. Create a roll up summary on the Account which sums this field
3. Create a WFR on the Opportunity which checks the Roll up summary field is greater than 0
4. Create a Cross-object field update which sets Customer type from Prospect to Customer
Something like that
Hope it'll help you.
If Opportunity won, then Immediate Action:
Update Records
Record*
[Opportunity].Account ID
Criteria for Updating Records*
Updated records meet all conditions
Filter the records you update based on these conditions
Account type = Prospect
Set new field values for the records you update
Account type = Customer
First of all, it would throw a duplicate Id exception if there are multiple opportunities under a single account.
Second, even if duplicate Id exception is taken care, for accounts with multiple opportunities, only the stageName of the last opportunity in iteration is taken into consideration as the for loop in on Opportunity and the check is not from an 'Opportunities under an account' perspective.
Not sure how this resolved Cola's problem. If still going with a trigger, the below would help: