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();
}
}