0
Sample1__c (Id,Name,chkbox__c,Sum__c) 
Sample2__c (Id,Name,Sample1__c,Num1__c)
Sample3__c (Id,Name,Sample1__c,Sample2__c,chkbox__c,Num1__c,Sum__c,Other_Field__c)

Sample1__c is the master of both Sample2__c and Sample3__c.

Meanwhile, Sample3__c has a lookup relationship with Sample2__c.

I have a requirement that updates a Sample3__c record after updating a Sample2__c record. Since Sample3__c is a child of Sample1__c, a field from Sample1__c record must be updated too. I am not encountering any problem with this requirement. In fact, it's working perfectly when I test it with data.

So in order to deploy it, I created a test class. Every time I run it, both the apex test execution ui and developer console are giving me passed result. But there is no code coverage.

I tried researching about this scenario, and there's this link: https://success.salesforce.com/issues_view?id=a1p30000000eMoyAAE

After following the instructions, there is still no code coverage. So, I am certain that the problem is in my codes.

I am kind of stuck here. Meanwhile, below are the codes.

Apex Class

public class HelperClass {

 public static void UpdateSample3AfterSample2Update(List<Sample2__c> s2List) {

        s2List = [SELECT Id, Name, Sample1__c, Sample1__r.chkbox__c, Num1__c FROM Sample2__c WHERE Id IN: s2List];

        List<Sample3__c> s3Qry = new List<Sample3__c>();
        List<Sample3__c> s3ListToUpdate = new List<Sample3__c>();

        for(Sample2__c s2 : s2List) {

            s3Qry = [SELECT Id,Name,Sample1__c,Sample2__c,chkbox__c,Num1__c,Sum__c FROM Sample3__c WHERE Sample2__c =: s2.Id];

            for(Sample3__c s3 : s3Qry) {

                if(s2.Id != null) {

                    if(s2.Sample1__r.chkbox__c == true) {

                        s3.Sample2__c = s2.Id;
                        s3.Num1__c = s2.Num1__c;
                        s3.Sum__c = s2.Sample1__r.Sum__c;
                        s3ListToUpdate.add(s3);

                    }

                }

            } 

        }   

        update s3ListToUpdate;

    }

}

Trigger1

trigger updateSum on Sample2__c (after update) {

   if(Trigger.isAfter && Trigger.isUpdate) {
       HelperClass.UpdateSample3AfterSample2Update(Trigger.New);
   }

}

Trigger2

trigger updateSample1 on Sample3__c (after update) {

    List<Sample1__c> s1List = new List<Sample1__c>();
    List<Id> ids = new List<Id>();

    if(Trigger.isAfter && Trigger.isUpdate) {

        for(Sample3__c s3Rec : Trigger.New) {
            ids.add(s3Rec.Sample1__c);
        }

        Map<Id,Sample1__c> mapSample1 = new Map<Id,Sample1__c>([SELECT Sum__c FROM Sample1__c WHERE Id IN: ids]);

        for(Sample3__c s3Rec2 : Trigger.New) {

            Sample1__c s1 = mapSample1.get(s3Rec2.Sample1__c);

            if(s1!=null) {
                s1.Sum__c = s3Rec2.Other_Field__c + s3Rec2.Num1__c; 
                s1List.add(s1);    
            }

            if(!s1List.isEmpty()) {
                update s1List;
            }

        }

    } 

}

Test Class

@isTest  

public class SampleTestClass {

    static List<Sample1__c> sample1List = new List<Sample1__c>();
    static List<Sample2__c> sample2List = new List<Sample2__c>();
    static List<Sample3__c> sample3List = new List<Sample3__c>();
    static List<Sample3__c> sample3ListToUpdate = new List<Sample3__c>();

    private static testMethod void UpdateS3AfterS2Update() {

        Sample1__c sample1 = new Sample1__c();
        sample1.Name = 'This is just a sample test name';
        sample1.chkbox__c = true;
        sample1List.add(sample1);
        insert sample1List;

        Sample2__c sample2 = new Sample2__c();
        sample2.Sample1__c = sample1.Id;
        sample2.Num1__c = 1000;   
        sample2List.add(sample2);
        insert sample2List;

        Sample3__c sample3 = new Sample3__c();
        sample3.Sample1__c = sample1.Id;
        sample3.chkbox__c = true;
        sample3.Sample2__c = sample2.Id;
        sample3.Num1__c = 1000;
        sample3.Sum__c = 1000;
        sample3.Other_Field__c = 1000;
        sample3List.add(sample3);
        insert sample3List;

        Test.startTest();

        for(Sample3__c s3 : sample3List) {
            s3.Num1__c = 500;
            sample3ListToUpdate.add(s3);
        }

        update sample3ListToUpdate;

        HelperClass hc = new HelperClass();

        Test.stopTest();


    }

}
2

Your test class is inserting Sample1, Sample2 and Sample3 records and updating only Sample3 records.

You have a trigger on Sample2__c object on AfterUpdate which is calling your helper class

Your trigger on sample3 is updating sample1 but its not touching sample2.

unless you update Sample2 in your test class, the sample2 trigger and the helper class won't get covered.

but with your current test class you should see the trigger on sample3 being covered already.

Also, in your helper class I noticed you doing SOQL inside for loops which is generally not recommended (try moving it out of the loop or change it to SOQL for loop).

3
  • I added this line for(Sample2__c s2 : sample2List) { s2.Quantity__c = 500; sample2ListToUpdate.add(s2); } update sample2ListToUpdate; before Test.startTest();, the code is now covered, but the result now displays fail result – Gaynerys Jan 11 '17 at 13:26
  • Also, I checked the errors, and there's System.DmlException: Update failed. First exception on row 0 with id [some id]; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, updateSum: execution of AfterUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id [some id]; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, updateSample1: execution of AfterUpdate caused by: System.ListException: Duplicate id in list: [some id] – Gaynerys Jan 11 '17 at 13:29
  • I think that @Vami Krishna answered your question successfully. Possibly, your tests are highlighting that you have errors in your code (that's what they're there for!). Time to check your code and, possibly, ask another question! – mkorman Jan 11 '17 at 15:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.