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
DEV-SFDCDEV-SFDC 

Help on Trigger for update in relatedlist

Hi,

 

I have a account say TEST :-

 TEST account has several opportunities in its related list i.e both DIRECT and INDIRECT.

and opportunity are of 2 types direct and indirect based on checkbox i.e if the checkbox indirect_c is checked then its an indirect opportunities.

 

So,

I want ..If the account owner
has an existing opportunity  of his own and it’s marked closed/won then all the Indirect
opportunity stage  related to account TEST should change to as closed/lost with a reason of “won by current
channel”

bob_buzzardbob_buzzard

Have you got some code you are having problems with, or are you just posting your requirements?

DEV-SFDCDEV-SFDC

Thnaks BOB for your quick Reply

 

I have tried to write a trigger and my code is :-

trigger Updatestageoppty on Opportunity (before insert,before update) {

list<string>accountids=new list<string>();   List<Account> parentAccounts = new List<Account>(); for(Opportunity opp:trigger.new){ accountids.add(opp.Accountid); }      Map<String,Account> accountMap = new Map<String,Account>([Select Id, OwnerId from Account where Id in :accountIds]);

for(Opportunity opp1:trigger.new){

        if(opp1.StageName=='Closed Won'){                 system.debug('TEStJaya'+accountMap);                 if(accountMap.containskey(opp1.Accountid) && opp1.Indirect_opportunity__c == true)                 {                    


bob_buzzard wrote:

Have you got some code you are having problems with, or are you just posting your requirements?



                   opp1.StageName ='Closed Lost';                     }                         } }

}

bob_buzzardbob_buzzard

Can you repost your code using the code format - the clipboard with a 'c' in the toolbar?

 

Also, what problems are you hitting with your code?

DEV-SFDCDEV-SFDC
trigger UpdateAccownerTOoppOwner on Opportunity (before insert,before update) {

list<string>accountids=new list<string>();
  List<Account> parentAccounts = new List<Account>();
for(Opportunity opp:trigger.new){
accountids.add(opp.Accountid);
}
     
Map<String,Account> accountMap = new Map<String,Account>([Select Id, OwnerId from Account where Id in :accountIds]);

for(Opportunity opp1:trigger.new){

        if(opp1.StageName=='Closed Won'){
        
        system.debug('TEStJaya'+accountMap);
                if(accountMap.containskey(opp1.Accountid) && opp1.Indirect_opportunity__c == true)
                {
                    //opp1.OwnerId =accountMap.get(opp1.Accountid).OwnerId ;
                   opp1.StageName ='Closed Lost'; 
  
                  }
        
        
        }
}

}

 I can update only 1 opportunity of which i'm the owner, where as i also need to change the status of all indirect opportunity related to a that account once the account owner changes the status of his opportunity as closed won

harsha__charsha__c

Step 1: For this Change the Event to after i.e. after insert and after update

 

Step 2 : Instead of Iterating trigger.new list in the for loop, Iterate accountMap .values()

 

Because, here you not only needed to update the newly inserted records but also the existing records

 

Step 3 : Update the records

 

DEV-SFDCDEV-SFDC

How do I do this?

 

Been struggling from 2 days, Can you please help me writting a code as i'm new to Apex

 

Thanks in advance for help.

Yoganand GadekarYoganand Gadekar

Hi u can try following trigger.

 

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

  list<string>accountids=new list<string>();
  List<Opportunity> Updateopty = new List<Opportunity>(); 
  
 for(Opportunity opp:trigger.new){
     accountids.add(opp.Accountid);
    }
    
 for(Account acc:[select id,name,(select name,id from opportunities) from account where id IN:accountids]){
   Integer iCount =0;
   for(Opportunity opty:acc.opportunities){
       if(opty.StageName=='Closed Won')
         iCount++;
   }

   if(ICount > 0){
     for(Opportunity opty:acc.opportunities){
       if(opty.indirectcheckbox)//insert your exact field name for your checkbox

       {

       opty.StageName ='Closed Lost';
       opty.reason= 'won by current channel'//check for your field reason
       Updateopty.add(opty);
     }

  }

   }
 }
   update Updateopty;

}

 

 

mark this as answer if this helps :-)

__Yoganand gadekar.

DEV-SFDCDEV-SFDC

Thanks yoganand for your help.

 

I'm Getting some error while saving the trigger.

 

Error: Compile Error: expecting a semi-colon, found 'Updateopty.add' at line 19 column 7

 

I.e

Updateopty.add(opty);

  

Yoganand GadekarYoganand Gadekar

need to put semicolon as below

 

opty.StageName ='Closed Lost';
       opty.reason= 'won by current channel'  ;    //check for your field reason
       Updateopty.add(opty);
     }

 

mark this as answer if it works now

DEV-SFDCDEV-SFDC

Error:Apex trigger UpdateOpptIndirect caused an unexpected exception, contact your administrator: UpdateOpptIndirect: execution of AfterUpdate caused by: System.EmailException: SendEmail failed. First exception on row 0; first error: INVALID_FIELD_WHEN_USING_TEMPLATE, When a template is specified the plain text body, html body, subject and charset may not be specified : []: Class.AP_OpportunityUpdate.NotifyOpportunityOwner: line 74, column 1

 

CLASS :-

 

public class AP_OpportunityUpdate
{
public static boolean blnflag=false;  
public static void upDateOpt(list<Opportunity> lstOpt,Map<id,Opportunity> mpoldvalues)
{
map<id,id> mapOpportunityOwnerId=new map<id,id>();
Set<id> stAccountId=new set<id>();

list<UserRole> userRoleMap=new list<UserRole>([select Id from UserRole where name='indirect']);
blnflag=false;
for(Opportunity opt: lstOpt)
{
    if(opt.ownerid==userinfo.getuserid() && userinfo.getUserRoleId()!=userRoleMap[0].id && opt.StageName!=mpoldvalues.get(opt.id).stageName && opt.StageName=='Closed Won'  )
    {
        mapOpportunityOwnerId.put(opt.id,opt.ownerid);
        stAccountId.add(opt.accountid);
    }
}


set<id> stEmail=new set<id>();
list<Opportunity> lstOpprtunityToUpdate=new list<Opportunity>();
for(Opportunity varOpts:[SELECT AccountId,Owner.name,Owner.email,owner.userRole.Name FROM Opportunity  where AccountId In :stAccountId AND owner.userRole.Name='Indirect'])
{
//stEmail.add(varOpts.Owner.Email);
stEmail.add(varOpts.Ownerid);
  varOpts.StageName='Closed Lost';
    varOpts.Won_Lost_Reason__c='Won by Current Channel';
    lstOpprtunityToUpdate.add(varOpts);

}
update lstOpprtunityToUpdate;
NotifyOpportunityOwner(lstOpprtunityToUpdate);
//NotifyOpportunityOwner(stid);
blnflag=true;
}
public static void NotifyOpportunityOwner(list<opportunity> lstOpprtunityToUpdate)
{
/*list<id> lstmail=new list<id>();
lstmail.addall(stmail);*/

list<Messaging.singleEmailMessage> lstMailmail = new list<Messaging.singleEmailMessage>();

for(opportunity varOpt: lstOpprtunityToUpdate)

{
Messaging.singleEmailMessage mail = new Messaging.singleEmailMessage();
 
//list<string> lstmails =new list<string>();
//lstmail.addall(stmail);
//string strusr1='';
/*for(integer i=0; i<lstmail.size(); i++ )
{
if(i==0)
{
strusr1=lstmail[i];
}
lstmail.remove(strusr1);*/
//mail.setToAddresses(lstmail);

 // The email template ID used for the email
   mail.setTemplateId('00XK0000000DjTU');
 mail.setTargetObjectId(varOpt.ownerid);

  mail.whatid=varOpt.id;
  mail.saveAsActivity = false;
    //mail.whatid='001K000000MqbEp';
 
 lstMailmail.add(mail);  
system.debug('---------------->'+lstMailmail.size()); 
system.debug('---------------->'+lstMailmail); 
if(!lstMailmail.isEmpty())
{   
Messaging.sendEmail(lstMailmail);
}
}
}
}

Trigger :-

 

trigger UpdateOpptIndirect on Opportunity (After update) 
{
if(AP_OpportunityUpdate.blnflag==false)
{
AP_OpportunityUpdate.upDateOpt(trigger.new,Trigger.oldmap);

}




}

i have written a class which i'm calling through trigger .

 

I have a account say TEST :-

TEST account has several opportunities in its related list i.e both DIRECT and INDIRECT.

and opportunity are of 2 types direct and indirect based on checkbox i.e if the checkbox indirect_c is checked then its an indirect opportunities.

 

So,

I want ..If the account owner has an existing opportunity  of his own and it’s marked closed/won then all the Indirect opportunity stage  related to account TEST should change to as closed/lost with a reason of “won by current channel”