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
MasieMasie 

Test Class Help on an create after update trigger

Hi

May someone please help, I have written a trigger to create a record in a custom object when a status is changed on my assets. The test class I have passes but the percentage coverage is very low.

Trigger:

trigger createBillableSwappedAsset on Asset (after update) {

List <Billable_Swapped_Asset__c> auditTrailList = new List <Billable_Swapped_Asset__c>();

// or whatever your custom object name put instead of Vehicle__c

for ( Asset ass : Trigger.new) {

              if (Trigger.oldMap.get(ass.Id).Movement_Status__c != 'Swapped') {
 
 
  // here is where you check if asset that is being inserted meets the criteria
              if (ass.Movement_Status__c == 'Swapped' && ass.Product2Id != null && ass.Show_Device__c != true) { 


           
       //instantiate the object to put values for future record 
  Billable_Swapped_Asset__c  b =  new Billable_Swapped_Asset__c ();
        
  // now map asset fields to billable swapped asset that is being created with this asset
 
                      b.Company__c = Trigger.oldMap.get(ass.Id).AccountId;
                      b.Product__c = ass.Product2Id;
                      b.Install_Date__c =  ass.InstallDate;
                      b.End_Date__c = ass.UsageEndDate;
 
  auditTrailList.add(b);
 
 
  }//end if
 
}//end for ass

//once loop is done, you need to insert new records in SF
// dml operations might cause an error, so you need to catch it with try/catch block.
try {
  insert auditTrailList;
} catch (system.Dmlexception e) {
        system.debug (e);
    }
}

}


Test Class:

@isTest

private class TestCreateBillableSwappedAsset

{

static testMethod void TestCreateBillableSwappedAsset()
{
    Date todaysDate = System.today();
        Profile p = [SELECT Id FROM Profile WHERE Name='Standard User'];
        
        //Create  & Insert Test User
  User u = new User(Alias = 'standt', Email='standarduser@sureswipe.com',
                  EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US',
                  LocaleSidKey='en_US', ProfileId = p.Id,
                  TimeZoneSidKey='America/Los_Angeles', UserName='standarduser@sureswipe.com'); 
        System.runAs(u)
           
        {
          //Create and Insert Account
  Account a = new Account(Name = 'Test Company', Status__c = 'Active', Industry = 'Fashion',Type = 'Customer',
                                        Company_Email_Adress__c = 'test@test.com');
            insert a;
           
   //Create and Insert Asset
   Asset ass = new Asset(
   Name = 'Test Asset',
   Status = 'Installed',
   AccountId = a.Id,
   InstallDate = System.Today(),
   Product2Id = '01t200000024GTq',
   UsageEndDate = System.Today()
   );
   insert ass;

//Update Asset status to swapped
           
ass.status = 'Swapped';
ass.show_device__c = false;
update ass; 
           
//Update Asset status to swapped with show device true      
ass.status = 'Swapped';
ass.show_device__c = true;
update ass;
           
//Update Asset status to swapped with no product         
ass.status = 'Swapped';
ass.show_device__c = false;
ass.Product2Id = null;
update ass; 
           

        }
    }
}

Best Answer chosen by Masie
pradeep naredlapradeep naredla
HI,

Try this code and tell me it worked or not
@isTest

private class TestCreateBillableSwappedAsset

{

static testMethod void TestCreateBillableSwappedAsset()
{
    Date todaysDate = System.today();
        Profile p = [SELECT Id FROM Profile WHERE Name='Standard User'];
        
        //Create  & Insert Test User
  User u = new User(Alias = 'standt', Email='standarduser@sureswipe.com',
                  EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US',
                  LocaleSidKey='en_US', ProfileId = p.Id,
                  TimeZoneSidKey='America/Los_Angeles', UserName='standarduser@sureswipe.com'); 
        System.runAs(u)
           
        {
          //Create and Insert Account
  Account a = new Account(Name = 'Test Company', Status__c = 'Active', Industry = 'Fashion',Type = 'Customer',
                                        Company_Email_Adress__c = 'test@test.com');
            insert a;
           
   //Create and Insert Asset
   Asset ass = new Asset(
   Name = 'Test Asset',
   Status = 'Installed',
   AccountId = a.Id,
   InstallDate = System.Today(),
   Product2Id = '01t200000024GTq',
   UsageEndDate = System.Today()
   );
   insert ass;

//Update Asset status to swapped
           
ass.status = 'Swapped';
ass.show_device__c = true;
ass.Product2Id = null;
update ass; 
           

        }
    }
}
Thanks,
pradeep

All Answers

Magdiel HerreraMagdiel Herrera
The requirement for apex triggers is that it needs to be at least 1% or have some coverage,

https://developer.salesforce.com/page/An_Introduction_to_Apex_Code_Test_Methods
https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_qs_test.htm

You could use the developer console and take a look at which lines of code get executed when running your test class,
pradeep naredlapradeep naredla
HI,

Try this code and tell me it worked or not
@isTest

private class TestCreateBillableSwappedAsset

{

static testMethod void TestCreateBillableSwappedAsset()
{
    Date todaysDate = System.today();
        Profile p = [SELECT Id FROM Profile WHERE Name='Standard User'];
        
        //Create  & Insert Test User
  User u = new User(Alias = 'standt', Email='standarduser@sureswipe.com',
                  EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US',
                  LocaleSidKey='en_US', ProfileId = p.Id,
                  TimeZoneSidKey='America/Los_Angeles', UserName='standarduser@sureswipe.com'); 
        System.runAs(u)
           
        {
          //Create and Insert Account
  Account a = new Account(Name = 'Test Company', Status__c = 'Active', Industry = 'Fashion',Type = 'Customer',
                                        Company_Email_Adress__c = 'test@test.com');
            insert a;
           
   //Create and Insert Asset
   Asset ass = new Asset(
   Name = 'Test Asset',
   Status = 'Installed',
   AccountId = a.Id,
   InstallDate = System.Today(),
   Product2Id = '01t200000024GTq',
   UsageEndDate = System.Today()
   );
   insert ass;

//Update Asset status to swapped
           
ass.status = 'Swapped';
ass.show_device__c = true;
ass.Product2Id = null;
update ass; 
           

        }
    }
}
Thanks,
pradeep
This was selected as the best answer
MasieMasie
Thanks Pradeep

This worked :)