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
Nani SircillaNani Sircilla 

i need to test class for this trigger

Hi all,
i have a trigger and i need test class for this trigger. please check it once and give me appropriate solutions.

trigger OppMailTrigger on Opportunity (after insert) {
Opportunity i=[select ownerid, id,,Name from opportunity where id=:trigger.newMap.keySet()];
user s= [select name from User where id=:i.ownerid];
String userEmail =; 
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); 
String[] toAddresses = new String[] {userEmail}; 
mail.setSubject('Automated email: Opportunity created'); 
//String body = 'Hello,'+' ''\n\n'+'  '+'On: '+'''\n'+'New Opportunity : '+' '+i.Name+' '+'is Created..'+'\n'+'Click Here..'+''+''; 
String body = '<html><body>Hello,&nbsp;&nbsp;&nbsp;'+'<br>On:&nbsp;&nbsp;'+'<br><href>'+ +'</href></body>  </html>';
Messaging.sendEmail(new Messaging.SingleEMailMessage[]{mail});
opportunity opp =[SELECT id FROM opportunity Where id in :];
update opp;  

Thank you.
sandeep sankhlasandeep sankhla

You can simply insert a opportunity with all required fields in your test class

test class:

Opportunity objOppt = new Opportunity(Name = 'test');

insert objOppt;

Please perovide all required fields for opportunity and it will work for you..

sandeep sankhlasandeep sankhla
Hi Nani,

Please try above code to cover the code...But your code is not bulkified as a best practise we should not use soql inside loop...but as per your business need if there will not be bulk insert then it will work...but in bulkinsert thei will fail as it will exceed the limit..