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
Abhishek AS 3Abhishek AS 3 

write a testclass for apex trigger

 trigger EUCOBBrochureCount on Opportunity_Brochure__c (after insert, after delete, after undelete) {
    Set<Id> quoteIDs = new Set<Id>();
    if(Trigger.isInsert || Trigger.isUndelete){
        for(Opportunity_Brochure__c OB : Trigger.New){
            quoteIDs.add(OB.Quote__c);
        }
        EUCOBBrochureCountHandler.updateQuote(quoteIDs);
    }
    else if(Trigger.isDelete){
        for(Opportunity_Brochure__c opp : Trigger.old){
            quoteIDs.add(opp.Quote__c);
        }
        EUCOBBrochureCountHandler.updateQuote(quoteIDs);
    }
}



public with sharing class EUCOBBrochureCountHandler {
    public static List<Quote> updateQuote(Set<Id> quoteIds){
            Integer TotalCount = [select count() from Opportunity_Brochure_c where Quote_c in :quoteIds];
            List<Quote> quoteList = [select id, Brochure_Count__c from Quote where id in :quoteIds];
        
            for(Quote qt : quoteList){
                qt.Brochure_Count__c= TotalCount;
            }
        try{
            update quoteList;
        }
        catch(Exception e){ 
            ApexException.LogException(e);
        }
        return quoteList;
    }  
    
}
Best Answer chosen by Abhishek AS 3
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Abhishek,
Can you use the below test class. Make sure to replace the API names of the objects and fields where ever necessary.

It is giving 100% coverage for trigger and 87% for Class.
 
@istest
public class EucoBrochureTest {
    @istest static void testm()
    {
      Account a = new Account ();
        a.name='sample account';
        insert a;
        Contact c= new Contact();
        c.AccountId=a.id;
        c.LastName='sample';
        insert c;
        Opportunity o = new Opportunity();
        o.Name='sample oppy';
        o.AccountId=a.id;
        o.ContactId=c.id;
        o.StageName='Booking';
        o.CloseDate=Date.today();
        insert o;
		Quote__c q= new Quote__c();
		q.opportunity__c= o.id;
		insert q;
Opportunity_Brochure__c b = new Opportunity_Brochure__c();
	b.Quote__c =q.id;
        b.name='sample bro';
        insert b;
        delete b;
    }
}

Let me know if you face any issues.

If this solution helps, please mark it as best answer.

Thanks,

 

All Answers

Sai PraveenSai Praveen (Salesforce Developers) 
Hi Abhishek,

Can you list down what are the mandatory fields for creation of Quote and Opportunity_Brochure__c records and what is the relation with opportunity? 

The simple test class would be creation of Quote and 2 Opportunity_Brochure__c records and deletion of 1 Opportunity_Brochure__c record. This should cover 100%.

Thanks,
K. Sai Praveen Kumar.
Abhishek AS 3Abhishek AS 3
There is look up relation between quote and opportunity_brouchure__c.Name field is mandatory. Can u please provide the code
Sai PraveenSai Praveen (Salesforce Developers) 
Can you also confirm if Opportunity record is required for creating a Quote?

Thanks,
 
Abhishek AS 3Abhishek AS 3
Yes its required
Abhishek AS 3Abhishek AS 3
Also want to include test for try catch block.
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Abhishek,
Can you use the below test class. Make sure to replace the API names of the objects and fields where ever necessary.

It is giving 100% coverage for trigger and 87% for Class.
 
@istest
public class EucoBrochureTest {
    @istest static void testm()
    {
      Account a = new Account ();
        a.name='sample account';
        insert a;
        Contact c= new Contact();
        c.AccountId=a.id;
        c.LastName='sample';
        insert c;
        Opportunity o = new Opportunity();
        o.Name='sample oppy';
        o.AccountId=a.id;
        o.ContactId=c.id;
        o.StageName='Booking';
        o.CloseDate=Date.today();
        insert o;
		Quote__c q= new Quote__c();
		q.opportunity__c= o.id;
		insert q;
Opportunity_Brochure__c b = new Opportunity_Brochure__c();
	b.Quote__c =q.id;
        b.name='sample bro';
        insert b;
        delete b;
    }
}

Let me know if you face any issues.

If this solution helps, please mark it as best answer.

Thanks,

 
This was selected as the best answer
Abhishek AS 3Abhishek AS 3
Thank u very much Sai.
Can u help to cover the exception in this code. This test class is not covering it.
Abhishek AS 3Abhishek AS 3
Can any vody help me to write test class for above cide exception handling part also Sent from Android device