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
forceappforceapp 

Test class covered 42% need more coverage from ur help.

trigger emailquotetrig on Opportunity (after insert, after update) {
	List<quote> quotelist=new List<quote>();
	for(opportunity opp:trigger.new){
		quote q=new quote();
		if(opp.StageName=='Closed Won'){
			try{
			q=[select status,name,id,opportunityid from quote where Status__c=true and opportunityid=:opp.id];
			Messaging.reserveSingleEmailCapacity(1);
			Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
			String[] toAddresses=new String[]{'arvind.kot@gmail.com'};
			mail.setToAddresses(toAddresses);
			mail.setReplyTo('arvind_kot@rediffmail.com');
			mail.setSenderDisplayName('Salesforce Support');
			mail.setSubject('Plz click on the link to view the quote record');
			mail.setBccSender(false);
			mail.setUseSignature(false);
			mail.setPlainTextBody('https://ap1.salesforce.com/'+ q.id);
			Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
			}catch(system.QueryException e){
				opp.adderror('Need to select a primary quote among the related quotes'); 
			
			}
			
		}

	}
}


 Test class covered only 42 %:


@isTest
private class Test_emailquotetrig {

    static testMethod void myUnitTest() {
        // TO DO: implement unit test
         	opportunity opp=new opportunity(name='Monitors',closedate=system.today(),StageName='closed won');
        	insert opp;
        	Quote obj=new Quote(name='keyboard',primary__c=true,opportunityid=opp.id);
        	insert obj;
      
    	

    }
}
 

 

Best Answer chosen by Admin (Salesforce Developers) 
Rahul SharmaRahul Sharma
@isTest
private class Test_emailquotetrig {

    static testMethod void myUnitTest() {
        // TO DO: implement unit test
         	opportunity opp=new opportunity(name='Monitors',closedate=system.today(),StageName='closed won');
        	insert opp;
        	Quote obj=new Quote(Status__c=true, name='keyboard',primary__c=true,opportunityid=opp.id);
        	insert obj;
		update opp;
    }
}

 

All Answers

Rahul SharmaRahul Sharma
@isTest
private class Test_emailquotetrig {

    static testMethod void myUnitTest() {
        // TO DO: implement unit test
         	opportunity opp=new opportunity(name='Monitors',closedate=system.today(),StageName='closed won');
        	insert opp;
        	Quote obj=new Quote(Status__c=true, name='keyboard',primary__c=true,opportunityid=opp.id);
        	insert obj;
		update opp;
    }
}

 

This was selected as the best answer
forceappforceapp

@isTestprivate class Test_emailquotetrig {
    static testMethod void myUnitTest() {

 

   opportunity opp = new opportunity(name='forceapp',Closeddate=system.today(),StageName='Prospecting');

    insert opp;

   

   Quote qu = new  Quote(name='contributor',primary__c=true,opportunityid=opp.id);

 insert quo;

  opportunity o =[select id from opportunity where id=:opp.id]

   o.StageName='Closed Won';

  update o;

 

 

but the code coverage is 89%,   how 2 cover the catch block need a reply.

 



   }catch(system.QueryException e){
    opp.adderror('Need to select a primary quote among the related quotes');
Rahul SharmaRahul Sharma

Try this:

@isTest
private class Test_emailquotetrig {

    static testMethod void myUnitTest() {
        // TO DO: implement unit test
         	opportunity opp=new opportunity(name='Monitors',closedate=system.today(),StageName='closed won');
        	insert opp;
		opportunity opp1=new opportunity(name='Monitors 1',closedate=system.today(),StageName='closed won');
        	insert opp1;
        	Quote obj=new Quote(Status__c=true, name='keyboard',primary__c=true,opportunityid=opp.id);
        	insert obj;
		update opp;
		update opp1;
    }
}