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

Update account field when opportunity lookup is update
Hello,
I'm trying to create a trigger or a process builder to update an account status field when an opportunity populated on lookup field in account changed.
In fact I have a lookup field to opportunity on my account name's "Main_opportunity__c" and I want to update the account status when this "Main opportunity" is closed won.
Thanks to your answer
Thomas
I'm trying to create a trigger or a process builder to update an account status field when an opportunity populated on lookup field in account changed.
In fact I have a lookup field to opportunity on my account name's "Main_opportunity__c" and I want to update the account status when this "Main opportunity" is closed won.
Thanks to your answer
Thomas
All Answers
Thomas
I tried to create a test for this trigger :
@isTest
private class TestUpdateMainOpp {
@isTest static void TestUpdateMainOpp () {
account a1 = new account(
Name ='Test',
Account_Type_MAIN__c ='Prospect',
Availpro_Status__c = 'In progress',
Salesperson__c = '005w0000003eWsO',
Main_Account_Contact__c = '0032500000YS4TK',
Account_Type__c = 'Reseller');
insert a1;
Opportunity opp = new Opportunity(Name='Opportunity TEST',
StageName='Prospecting',
AccountId=a1.id,
CloseDate=System.today().addMonths(1)
);
insert opp;
// Perform test
Test.startTest();
a1.Main_Opportunity__c = opp.id;
update a1;
opp.StageName='Closed Won';
opp.SyncedQuoteId=QUOT.Id;
update opp;
Test.stopTest();
}
}
Bu I have an error caused by the after update and after insert.
Do you have any idea to fixe that please ?
Thanks
Thomas
But just 50% on my trigger :
Do you have an idea ?
Thanks
Thomas
Sagar Lakhani (Skype : Sagar.Lakhani3)
Sorry just one last question : if I want to update a checkbox on opportunity object, what can I do ?
Thanks for your time.
Thomas
Please change trigger code to below otherwise you get error
if(Trigger.isBefore && (Trigger.isInsert || Trigger.isUpdate)){
OpportunityTriggerHandler.updateRelatedAccStatusOnOpp(Trigger.New);
}
and for checkbox update, In my triggerhandlerclass code
Add
objOpp.checkbox_api_Name = True or False;
below
objAcc.Status = 'AccStatus';
Not Opportunity object
Here is the trigger :
trigger OpportunityTrigger on Opportunity (After Insert,After Update,Before Insert,Before Update) {
if(Trigger.isBefore && (Trigger.isInsert || Trigger.isUpdate)){
OpportunityTriggerHandler.updateRelatedAccStatusOnOpp(Trigger.New);
}
}
And public class :
public class OpportunityTriggerHandler{
public static void updateRelatedAccStatusOnOpp(List<Opportunity> listOpportunity){
List<Account> listAccToUpdate = new List<Account>();
for(Opportunity objOpp : [SELECT Id,StageName,First_Closed_Opp__c from Opportunity]){
for(Account objAcc : [SELECT Id, Name, Account_Ownerchip__c, Account_Type_MAIN__c, AVP_Status__c, Availpro_Status__c, Invoicing_Status__c, Account_Support__c from Account Where Main_Opportunity__c =: objOpp.Id]){
if((objAcc != null) && (objOpp.StageName == 'Closed won') && (objOpp.First_Closed_Opp__c == false) && (objAcc.Account_Ownerchip__c != 'Partner')){
objAcc.Account_Type_MAIN__c = 'To be activated'; <!-- Custom Account Status -->
objAcc.AVP_Status__c = 'To be set-up'; <!-- Custom Account Status -->
objAcc.Availpro_Status__c = 'Client'; <!-- Custom Account Status -->
objAcc.Invoicing_Status__c = 'To be set-up'; <!-- Custom Account Status -->
objAcc.Account_Support__c = 'AVAILPRO - NEW HOTEL'; <!-- Custom Account Status -->
objOpp.First_Closed_Opp__c = true; <!-- This is the checkbox -->
listAccToUpdate.add(objAcc);
}
}
update listAccToUpdate;
}
}
}
Thanks Sagar
for(Opportunity objOpp : [SELECT Id,StageName,First_Closed_Opp__c from Opportunity])
I think in above line you have to Use 'listOpportunity' in place of [SELECT Id,StageName,First_Closed_Opp__c from Opportunity]
Have a nice day
Thomas
I don't know if you will see my post but I try :)
Thanks for the trigger that's work perfectly.
But I have one problem. Yesterday, I tried to mass update stage field of 17000 opportunity and I got this error : OpportunityTrigger: System.LimitException: Too many SOQL queries: 101.
I think that the trigger is trigger for any edit on opportunity. Could we change the code to execute this trigger just when the opportunity (linked to the main opportunity field on account) is closed won ?
Thanks
Thomas
Thanks,
Sagar Lakhani (Skype : Sagar.Lakhani3)
Error:Apex trigger OpportunityTrigger caused an unexpected exception, contact your administrator: OpportunityTrigger: execution of BeforeUpdate caused by: System.SObjectException: DML statement cannot operate on trigger.new or trigger.old: Class.OpportunityTriggerHandler.updateRelatedAccStatusOnOpp: line 20, column 1
Thomas
if any one knows pls share me.....