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 

Trigger error : INVALID_FIELD_WHEN_USING_TEMPLATE, When a template is specified the plain text body

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 user's role and record type i.e if user role is indirect 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”

DEV-SFDCDEV-SFDC

this error is coming when an account owner is trying to change the stage to CLOSED WON  of his own opportunity.

@taani.ax1426@taani.ax1426

Hi,

 

Could you please elaborate your issue. 

 

Regards

DEV-SFDCDEV-SFDC

Thanks for your reply!!

 

I have an account say TEST ..

Account has opportunities(i.e direct and indirect) related to it.

Opportunities can be direct or indirect.

Indirect opportunities are those which have their owner role as INDIRECT

So when account owner(has role Direct) changes the status of his opportunity as CLOSED won then all the INDIRECT opportunities related to TEST Acount should be CLOSED LOST and the mail should go to all the respective INDIRECT opportunity owner with a template and the template should contain the opportunity owner name,Account name,

 

So i have written a Class for this which i'm calling through trigger.

So,

i'm getting the following error  while saving the record when changing the direct opportunity status

Raja Bipin ChandraRaja Bipin Chandra

 Hello ,

 

Did you get the resolution for this error? Please post it if you found anything as i am also facing the same problem.

 

Thanks,

Raj