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
anuj pathak 2anuj pathak 2 

Test Classs with Last modified Date

Hi Team,

I need to create a account that's have lastmodified date in prevous months, but LastModified field is non writable. so how i can cover my test class.

 
Best Answer chosen by anuj pathak 2
mukesh guptamukesh gupta
Hi Anuj,

You need to use static resources. please follow below example

Test.loadData Example
The following are steps for creating a sample .csv file and a static resource, and calling Test.loadData to insert the test records.
Create a .csv file that has the data for the test records. This sample .csv file has three account records. You can use this sample content to create your .csv file.
 
Name,Website,Phone,BillingStreet,BillingCity,BillingState,BillingPostalCode,BillingCountry
sForceTest1,http://www.sforcetest1.com,(415) 901-7000,The Landmark @ One Market,San Francisco,CA,94105,US
sForceTest2,http://www.sforcetest2.com,(415) 901-7000,The Landmark @ One Market Suite 300,San Francisco,CA,94105,US
sForceTest3,http://www.sforcetest3.com,(415) 901-7000,1 Market St,San Francisco,CA,94105,US


Create a static resource for the .csv file:
From Setup, enter Static Resources in the Quick Find box, then select Static Resources.
Click New.
Name your static resource testAccounts.
Choose the file you created.
Click Save.
Call Test.loadData in a test method to populate the test accounts.
@isTest 
private class DataUtil {
    static testmethod void testLoadData() {
        // Load the test accounts from the static resource
        List<sObject> ls = Test.loadData(Account.sObjectType, 'testAccounts');
        // Verify that all 3 test accounts were created
        System.assert(ls.size() == 3);

        // Get first test account
        Account a1 = (Account)ls[0];
        String acctName = a1.Name;
        System.debug(acctName);

        // Perform some testing using the test records
    }
}

    if you need any assistanse, Please let me know!!


    Kindly mark my solution as the best answer if it helps you.

    Thanks
    Mukesh