function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Anoop RbAnoop Rb 

How to update Account related Opportunities.

Hi please any one help me on this " I am trying to update Account related Opportunities ". Here I have a field on Account object named as "Account Status" and also i have field on Opportunity Object named as "Opportunity Status". if changed Account status as "Active " then the Account related Opportunities "Opportunity Status" also "Active", if the Account status is changed to "In Active " then the Account related Opportunities "Opportunity Status" also changed to " In Active". this is my requirement. below is my code. when i use this code am getting error like "Error:Apex trigger updateAccRelatedOpptys caused an unexpected exception, contact your administrator: updateAccRelatedOpptys: execution of AfterUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id 0069000000cAZjEAAW; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, UpdateAccCon: execution of AfterUpdate caused by: System.ListException: Duplicate id in list: 0019000001Eu0chAAB Trigger.UpdateAccCon: line 16, column 1: []: Trigger.updateAccRelatedOpptys: line 15, column 1"

Below is my code. please let me explain how to avoid this error. when i use same code for account related contacts and cases update its working fine.


trigger updateAccRelatedOpptys on Account (After insert,After update) {
//set<>
list<Account> accRelConts=[select id,name,Account_Status__c,(select id,name,Oppty_Status__c from Opportunities) from Account where id IN : trigger.newMap.keyset()];
list<Account> accrelOppty=[select id,name,Account_Status__c,(select id,name,Oppty_Status__c from Opportunities ) from Account where Id IN : trigger.newMap.keyset()];

System.debug('++++++++++key set+++++++++++'+trigger.newMap.keyset());
list<Opportunity> opplst=new list<Opportunity>();
for(Account a:accRelConts){
for(Opportunity op:a.Opportunities){
op.Oppty_Status__c='hjg';
opplst.add(op);
}
}
if(opplst.size() > 0){
update opplst;
}
}
Ritesh Gupta 27Ritesh Gupta 27
Hi Anoop,

Above error occured from "UpdateAccCon" Trigger, please can you share code of this trigger.
Arunkumar RArunkumar R
Hi anoop,

    Change  Update List to Set or Map in UpdateAccCon trigger. The cause of the problem same account id list added more than one time in a same list.
Anoop RbAnoop Rb
Hi Ritesh this is my code

trigger updateAccRelatedOpptys on Account (After insert,After update) {
//set<>
list<Account> accRelConts=[select id,name,Account_Status__c,(select id,name,Oppty_Status__c from Opportunities) from Account where id IN : trigger.newMap.keyset()];
list<Account> accrelOppty=[select id,name,Account_Status__c,(select id,name,Oppty_Status__c from Opportunities ) from Account where Id IN : trigger.newMap.keyset()];

System.debug('++++++++++key set+++++++++++'+trigger.newMap.keyset());
list<Opportunity> opplst=new list<Opportunity>();
for(Account a:accRelConts){
for(Opportunity op:a.Opportunities){
op.Oppty_Status__c='hjg';
opplst.add(op);
}
}
if(opplst.size() > 0){
update opplst;
}
}
Anoop RbAnoop Rb
Hi Arunkumar,

   if i am using set then am gettin error like "Error: Compile Error: Incompatible element type Set<Opportunity> for collection of Opportunity at line 15 column 1"
Arunkumar RArunkumar R
Anoop,

  Can you share your trigger code called "UpdateAccCon"
Ritesh Gupta 27Ritesh Gupta 27
Hi Anoop,

This "updateAccRelatedOpptys" Trigger was already mentioned by you but I have asked for "UpdateAccCon" Trigger which must be there. Please send the code of this triiger as acutally error occur from "UpdateAccCon" Trigger.