+ Start a Discussion
HelloSanHelloSan 

i need a test class for code coverage for below trigger when parent updated child is updated

trigger Update  on Opportunity(afterupdate){

     List<Service__c lstservice=new List<Service__c>();

     Map<id,Opportunity> oppMap=new Map<Id,Opportunity>();

     for(Opportunity  opp:Trigger.new){

       oppMap.put(opp.Id,opp);

    }

     if(Trigger.isUpdate){

       for(Opportunity opp :Trigger.new){

          oppMap.put(opp.Id,opp);
        }

        List<Service__c> serList=[SELECTService_Type__c,Service_location__c FROM Service__c WHEREopppID  IN:oppMap.keySet()];

         List<Service__c> serviceListToUpdate=newList<Service__c>();

        for(Service__c ser:serList){

             ser.Service_Type__c=oppMap.get(ser.Id).Service_Type__c;

           ser.Service_location__c=oppMap.get(ser.Id).Service_location__c

             serviceListToUpdate.add(ser);

         }

        if(serviceListToUpdate.size()>0){

             try{

               update serviceListToUpdate;

             }catch(DmlException de){

                System.debug(de);

             }

        }

     


 
Best Answer chosen by HelloSan
David ZhuDavid Zhu
You may use this as a reference. You need to put valid value in service_type__c and service_location__ c field.

 
public static testmethod void positivetest1()
{
    Account a = new Account(name='test');
    insert a;

    Opportunity o = new Opportunity ();
    o.name='test op';
    o.stagename = 'Prospecting';
    o.closedate = date.today();
    o.accountid= a.id;
    o.service_type__c = 'service aaaa';    
    o.service_location__c  = 'location aaaa';
    insert o;
    
    Service__c s = new Service__c();
    s.name = 'test service';
    s.Service_Type__c = 'service bbbb';
    s.Service_location__c = 'location bbbb';
    s.oppid = o.id;
    insert s;

    o.stagename = 'Close Won';
    update o;    // this will trigger the after update trigger

    Service__c s1 = [SELECT Service_Type__c,Service_location__c FROM Service__c WHERE oppID = :o.id]

   //assert if the value is chagned to the value from opportunity
    system.assertequals('service aaaa',s1.Service_Type__c); 
    system.assertequals('location aaaa',s1.Service_location__c);
    
}

 

All Answers

PratikPratik (Salesforce Developers) 
Hi,

To cover the above mentioned trigger code, please insert an opportunity record and update the same.

Here is the smaple  code:
https://developer.salesforce.com/forums/ForumsMain?id=906F0000000AZWKIA4

Thanks,
Pratik
sandeep sankhlasandeep sankhla
Hi,

To cover this test class you need to simply create one Opportunity and then create Service__c  record where opptId will be inserted oppt ID.

After this you can update the oppt and cover the code.

 
David ZhuDavid Zhu
You may use this as a reference. You need to put valid value in service_type__c and service_location__ c field.

 
public static testmethod void positivetest1()
{
    Account a = new Account(name='test');
    insert a;

    Opportunity o = new Opportunity ();
    o.name='test op';
    o.stagename = 'Prospecting';
    o.closedate = date.today();
    o.accountid= a.id;
    o.service_type__c = 'service aaaa';    
    o.service_location__c  = 'location aaaa';
    insert o;
    
    Service__c s = new Service__c();
    s.name = 'test service';
    s.Service_Type__c = 'service bbbb';
    s.Service_location__c = 'location bbbb';
    s.oppid = o.id;
    insert s;

    o.stagename = 'Close Won';
    update o;    // this will trigger the after update trigger

    Service__c s1 = [SELECT Service_Type__c,Service_location__c FROM Service__c WHERE oppID = :o.id]

   //assert if the value is chagned to the value from opportunity
    system.assertequals('service aaaa',s1.Service_Type__c); 
    system.assertequals('location aaaa',s1.Service_location__c);
    
}

 
This was selected as the best answer