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
Sree_NaniSree_Nani 

can any one help me out from testcoverage of email template

Hi Developers,

 

System.DmlException: Insert failed. First exception on row 0; first error: MIXED_DML_OPERATION, DML operation on setup object is not permitted after you have updated a non-setup object (or vice versa): EmailTemplate, original object: Account: []

 

Not allowing any insertion statement, now code coverage is 55% with only emailtemplate insertion. 

 

If I am doing any mistake please help me out with modified code.

 

public class TestClass{
public Static TestMethod void taskemail(){

/* Blob b1 = Blob.valueOf('Test Data');
Attachment attachment = new Attachment();
attachment.ParentId = '02sE00000005KTDIA2';
attachment.Name = 'Test Attachment for Parent';
attachment.Body = b1;

insert(attachment); */

//public blob body{set;get;}
Account a = new Account(name='abc');
insert a;
//Account a1 = [select id,name from account limit 1];
//contact c1 = [select id,name from contact limit 1];
//opportunity o1 = [select id, name from opportunity limit 1];
Contact c = new Contact(lastname='test',email='test@advanz.com');
insert c;
//BrandTemplate b = new BrandTemplate(name='sampletemplate');
//insert b;
BrandTemplate b=[select id,name from BrandTemplate limit 1];
emailtemplate etemplate=new emailtemplate(BrandTemplateId=b.id,body='body',name='sample',TemplateStyle='formalLetter',DeveloperName='taeqst123w',folderid='00DE0000000IzueMAC',TemplateType='HTML');

//attachment a=new attachment(body=b1,name='attachfilename');
//insert a;
document doc = new document(name='sample',folderid='005E0000000HyVQ');
insert doc;
taskemail t = new taskemail();
Test.startTest();
insert etemplate;
Test.stopTest();

t.send();
t.getett();
//t.testsend();
t.cancel();
t.cancel1();
t.selectedtemplate();
t.selecttemplate();
t.viewfile();
t.done();
t.del();
t.getdoclist();
t.files();
t.getet();
t.getetlist();
t.go();
t.getattachlist();
t.getattlist();
t.fileattach();
t.getItems();

}
}

 

 

 

class code:

public class taskemail
{

public taskemail()
{
//ett=[select Body,HtmlValue,Markup,TemplateType,Description,Name from emailtemplate where DeveloperName=:'ActeionContact'];
//body=ett.body;
oid=apexpages.currentpage().getparameters().get('oid');
system.debug('+++++++++++++++++++++');
for(OpportunityContactRole ocr:[select id,contactid from OpportunityContactRole where opportunityid=:oid])
{
setid.add(ocr.contactid);
contactids.add(ocr.contactid);
}
for(contact c:[select id,name,email from contact where id in:setid])
{
mapval.put(c.id,c.name);

if(c.email!=null)
{
s=s+c.Name+',';

}
}
email=s;

}


public boolean plaintextbody{set;get;}
public boolean htmlbody{set;get;}
public boolean displayPopUp{set;get;}
public boolean attachpopup{set;get;}
public string email{set;get;}
public string subject{set;get;}
public string body{set;get;}
public string additionalemail{set;get;}
public string bcc{set;get;}
public string cc{set;get;}
set<id> setid=new set<id>();
map<id,string> mapval=new map<id,string>();
public List<Id> contactids=new List<Id>();
map<id,OpportunityContactRole> mapopp;

string s='';
public id oid;


public string errmessage{set;get;}
emailtemplate ett=new emailtemplate();
public void testsend()
{

BrandTemplate b=[select id,name from BrandTemplate limit 1];
emailtemplate etemplate=new emailtemplate();
etemplate.name='sample2';
etemplate.DeveloperName='test123w';
etemplate.folderid='00DE0000000IzueMAC';
etemplate.TemplateType='HTML';
etemplate.BrandTemplateId=b.id;
etemplate.TemplateStyle='formalLetter';
//etemplate.HtmlValue=body;

etemplate.body=body;
insert etemplate;
//send();

}
public emailtemplate getett()
{
return ett;
}

public pagereference send()
{
if(body==''||subject==''||body==null||subject==null)
{
errmessage='Required fields Subject or Body missing ';
return null;
}
else
{
set<string> strset=new set<string>();
if(email!='')
{
string[] emailaddress=email.split(',');
for(string s:emailaddress)
{
strset.add(s);
}
}
List<Messaging.Emailfileattachment> fileAttachments = new List<Messaging.Emailfileattachment>();
for (Attachment a :attlist)
{ // Add to attachment file list
Messaging.Emailfileattachment efa = new Messaging.Emailfileattachment();
efa.setFileName(a.Name);
efa.setBody(a.Body);
fileAttachments.add(efa);
}


for(id i:mapval.keyset())
{
if(strset.contains(mapval.get(i)))
{
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {mapval.get(i)};
//String[] toAddresses=s.split(',');
//system.debug('********'+toAddresses);
mail.setTargetobjectid(i);
if(additionalemail!='')
{
string[] addaddresses=additionalemail.split(',');
mail.setToAddresses(addaddresses);
}
if(cc!='')
{
string[] ccAddresses=cc.split(',');
mail.setCcAddresses(ccAddresses);
}
if(bcc!='')
{
string[] bccAddresses=bcc.split(',');
mail.setbccAddresses(bccAddresses);
}
mail.setSenderDisplayName('Daisy Cake Sales Team');
system.debug('*****'+et.id);

/*if(et.id!=null)
{
mail.setTemplateId(et.id);
//mail.setHtmlBody(body);
}
else
{*/
mail.setSubject(subject);
mail.setPlainTextBody(body);
mail.setHtmlBody(body);
//}
mail.setFileAttachments(fileAttachments);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
}
pagereference page=new pagereference('/'+oid);
return page;
}
return null;
}
public pagereference cancel()
{
system.debug('****************');
pagereference page=new pagereference('/'+oid);

return page;
}
list<EmailTemplate> etlist=new list<EmailTemplate>();
public void selecttemplate()
{
displayPopUp=true;
etlist=[select Body,HtmlValue,Markup,TemplateType,Description,Name from emailtemplate];

}
public list<emailtemplate> getetlist()
{
return etlist;
}
public void cancel1()
{
displayPopUp=false;
attachpopup=false;

}
emailtemplate et=new emailtemplate();
public void selectedtemplate()
{
string name=apexpages.currentpage().getparameters().get('ename');
if(name!=null)
{
et=[select id,Subject,Markup,HtmlValue,name,body from emailtemplate where name=:name];
body=et.body;
subject=et.subject;
}
displayPopUp=false;
}
public emailtemplate getet()
{
return et;
}
list<document> doclist=new list<document>();
public void files()
{
system.debug('HHHHffffffffffffffHHHHHHHHHHHHHHHHHHH');
if(attachlist.size()>0)
attachlist.clear();
attachpopup=true;
doclist=[select id,ContentType,description,name from document];
val='My Personal Documents';
if(val=='My Personal Documents')
{
documents=true;
mycomputer=false;
}
if(val=='My Computer')
{
documents=false;
mycomputer=true;
}
}
public list<document> getdoclist()
{
return doclist;
}
list<attachment> attlist=new list<attachment>();
public void fileattach()
{
string a=apexpages.currentpage().getparameters().get('dname');
if(a!=null){
document doc=[select id,name,BodyLength,body,ContentType,description from document where name=:a];
attachment att=new attachment();
att.name=doc.name;
att.contenttype=doc.contenttype;
att.description=doc.description;

att.body=doc.body;
attlist.add(att);
attachpopup=false;
}}
public list<attachment> getattlist()
{
return attlist;
}
public void del()
{
string aa=apexpages.currentpage().getparameters().get('delname');

integer i=0;
for(attachment a:attlist)
{
if(a.name==aa)
{

attlist.remove(i);
break;
}
i++;
}


}
public string val{set;get;}
public List<SelectOption> getItems() {
List<SelectOption> options = new List<SelectOption>();
options.add(new SelectOption('My Personal Documents','My Personal Documents'));
options.add(new SelectOption('My Computer','My Computer'));

return options;
}
public pagereference viewfile()
{
string a=apexpages.currentpage().getparameters().get('fname');
pagereference page;
document doc;
if(a!=null){
doc=[select id,name,BodyLength,body,ContentType,description from document where name=:a];
page=new pagereference('/servlet/servlet.FileDownload?file='+doc.id);}
return page;
}
public boolean documents{set;get;}
public boolean mycomputer{set;get;}
public blob attfile{set;get;}
public void go()
{
if(val=='My Personal Documents')
{
documents=true;
mycomputer=false;

}
if(val=='My Computer')
{
documents=false;
mycomputer=true;
}

}
public string attachfilename{set;get;}
public integer attachfileSize{set;get;}
List<attachment> attachlist=new list<attachment>();
public void attach()
{
attachment a=new attachment();
a.body=attfile;
//a.BodyLength=attachfileSize;
a.name=attachfilename;
attachlist.add(a);

}
public void done()
{
if(attachlist.size()>0)
attlist=attachlist;
attachpopup=false;

}
public list<attachment> getattachlist()
{
return attachlist;
}
public void testval()
{
system.debug('**************');

}
}


Sree_NaniSree_Nani

No it's not commented. I am querying that value. Can you please tell me the line where you find it as commented

Sree_NaniSree_Nani

HI Kiran,

 

               If am inserting only email template, I didn't get DML Exception like above. 

If any insert take place other than blue marked insertion, it will throw this error & code coverage doesn't change from 55%