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
Tejas Wadke 5Tejas Wadke 5 

Need test class for following code

Hello,

---------TRIGGER----------

trigger UpdateReportName on ActivityPlanningAndReport__c (before insert) {
     Map<Id,User> ownerMap;
     Set<Id> oppOwnerIds = new Set<Id>();
     String nameOfOwner;
     for(ActivityPlanningAndReport__c p : trigger.new)
    {
        oppOwnerIds.add(p.OwnerId);
    }
     ownerMap = new Map<Id,User>([SELECT Id, Name FROM User WHERE Id IN :oppOwnerIds]);
     for(ActivityPlanningAndReport__c p : trigger.new)
     {
          nameOfOwner = ownerMap.get(p.OwnerId).Name;
        if(p.Name!=null)
        {
            p.Name = p.Country_txt__c+'_'+ p.Display_Engagement_Date__c+'_'+nameOfOwner;
     }
     }   
   }
Nagendra ChinchinadaNagendra Chinchinada
Hi Tejas,
Here is the test class. I coded it in note pad, so there might be small typos in code, plese correct it
public class ApexTestClass {
static testmethod void testUpdateReportName(){
ActivityPlanningAndReport__c apr = new ActivityPlanningAndReport__c(Name='Test',Country_txt__c='Test txt',OwnerId=Userinfo.getUserId());
insert apr;
System.assertEquals(apr.Name, 'Test txt__'+Userinfo.getName());
}
}


Thanks,
Nagendra
sandeep reddy 37sandeep reddy 37
public class ApexTestClass {
static testmethod void testUpdateReportName(){
ActivityPlanningAndReport__c apr = new ActivityPlanningAndReport__c(Name='' Country_txt__c+'_'+ Display_Engagement_Date__c+'_''+userinfo.getname(),Country_txt__c='Test txt',OwnerId=Userinfo.getUserId()); insert apr; System.assertEquals(apr.Name, 'Country_txt__c+'_'+ Display_Engagement_Date__c+Userinfo.getName());
}
}
buggs sfdcbuggs sfdc
HI 

Try this you will get your 100% coverege

mark it as correct answer,if it helps you out.
@isTest
public class ApexTestClass {
static testmethod void testUpdateReportName(){
ActivityPlanningAndReport__c apr = new ActivityPlanningAndReport__c(Name='TestName',Country_txt__c='Test country',OwnerId=Userinfo.getUserId());
insert apr;
}


Thanks
 
Tanuja JTanuja J
Hi,
Shouldnt we create Account,Opportunity instances and then create the custom object instance ?
Also "OwnerId=Userinfo.getUserId());" how does this work?
Thanks