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
NAVEEN KUMARNAVEEN KUMAR 

Test Class not covering Class line of Contactrole__c

Hi All,

below is my test data for a class. the class doing some rollups based on created date. but in test class test data inserted and it's giving createddate value as null so it's not covering the lines in the class for Contactrole__c object. please give me the solution for this.
 
Contact_Role__c conrole= new Contact_Role__c();
       conrole.Amount__c= 8000;
       conrole.Contact__c=con.Id;
       conrole.Opportunity__c=income.id;
       conrole.Role__c='Soft Credit';
       conrole.Share__c=2;
      // conrole.CreatedDate =System.today();
       insert conrole;
       system.debug('******+conrole' +conrole.CreatedDate); // Retuns Null
       conrole= [select Id, CreatedById ,CreatedDate  from Contact_Role__c where Id =:  conrole.Id limit 1];
        system.debug('******+conrole' +conrole); //Returns Date

 
Mahesh DMahesh D
Hi Naveen,

Yes you have query back to get the CreatedDate, please send me your actual class so that, will try to give you more inputs.

Regards,
Mahesh
NAVEEN KUMARNAVEEN KUMAR
Map<Id,Opportunity> OppMap=new Map<Id,Opportunity> ([Select Id, Primary_Contact__c, AccountId, (SELECT Id, Name,Opportunity__c,Paid__c,Payment_Amount__c,Payment_Date__c from OppPayment__r where Paid__c= true ORDER BY Payment_Date__c ASC NULLS LAST),(SELECT Id, Amount__c,Contact__c,Opportunity__c,Role__c,CreatedDate  from Contact_Roles__r where Role__c= 'Soft Credit' ORDER BY CreatedDate ASC ) from Opportunity where Id IN: oppIds]);

  List<aggregateResult> LifetimeCon=[SELECT Max(Amount__c)PayMax,MAX(CreatedDate)PayMaxDt,Sum(Amount__c)PaySumT,Count(Amount__c)PayCountT,Opportunity__c from Contact_Role__c where (Role__c= 'Soft Credit' AND Opportunity__c IN: OppMap.keySet() ) group by Opportunity__c];
        Map<Id, AggregateResult>LifetimeConMap = new Map<Id, AggregateResult>();
        for(AggregateResult LifetimeConRec: LifetimeCon){
            LifetimeConMap.put((ID)LifetimeConRec.get('Opportunity__c'),LifetimeConRec);
        }

 if(LifetimeConMap.size()>0){
                    accRecord.Largest_Soft_Credit_Amount__c=(Decimal)LifetimeConMap.get(oppRec.Id).get('PayMax');
                    accRecord.Largest_Soft_Credit_Date__c=Date.valueOf(LifetimeConMap.get(oppRec.Id).get('PayMaxDt'));
                    system.debug('*****Largest_Soft_Credit_Date__c'+accRecord.Largest_Soft_Credit_Date__c);
                    accRecord.Lifetime_Soft_Credit_Amount__c=(Decimal)LifetimeConMap.get(oppRec.Id).get('PaySumT');
                    accRecord.Lifetime_Soft_Credit_Count__c=(Decimal)LifetimeConMap.get(oppRec.Id).get('PayCountT');

  acctRecords.add(accRecord);

  if (acctRecords.size() > 0) {
                update acctRecords;
                system.debug('*****acctRecords' + acctRecords);
 Hi Mahesh, i just pasted part of code...  Agreegate values for test data coming as expected but lines are not covered.

 
Mahesh DMahesh D
Hi Naveen,

Still didn't get the clear picture, what exactly the issue is.

If you can paste both the trigger / class, test class and which lines are not executing then will able to help you.

Regards,
Mahesh
NAVEEN KUMARNAVEEN KUMAR
Hi Mahesh,

Problem is line #6
  LifetimeConMap.put((ID)LifetimeConRec.get('Opportunity__c'),LifetimeConRec); 
is not covered in so the rollups related to this is in Red Part.

once this line is covered issue will be fixed.. 
Mahesh DMahesh D
Hi Naveen,

Did you create the Opportunity record in your Test Class, please paste your full test class.

Regards,
Mahesh