I'm trying to develop my first test class in our sandbox instance. When running the below test, I am getting no records return from the first query to build accountsList. The test succeeds, but I have 0% code coverage. The trigger works as expected when ran.
My Test Class:
@isTest
private class test_deleteAccountSharesOnUpdate {
static testMethod void updateAccounts()
{
Set<String> accountIds = new Set<String>();
List<Account> accountsList =
[
SELECT Id, Is_Script_Update__c, Description FROM Account
];
Integer i = 0;
for (Account acct : accountsList)
{
if(i > 199)
{
break;
}
acct.Is_Script_Update__c = True;
acct.Description = 'Testing at ' + System.now();
accountIds.add(acct.Id);
i++;
}
test.startTest();
update accountsList;
test.stopTest();
List<Account> updatedAccts =
[
SELECT Id, Is_Script_Update__c, Description
FROM Account
WHERE Id IN : accountIds
];
for(Account acct : updatedAccts)
{
System.assertEquals
(
'Testing at',
acct.Description.substring(0,10)
);
}
}
}
Trigger I'm trying to test:
trigger deleteAccountSharesOnUpdate on Account (after update)
{
Set<String> accountIds = new Set<String>();
// Gather the account Ids being updated
for (Account acct : trigger.new)
{
// Only remove shares for accounts being updated by data loader.
// Loader script will insert new shares after this job.
if(acct.Is_Script_Update__c)
{
String terrCode = acct.Territory_Code_String__c;
if(terrCode != null && terrCode.length() == 5)
{
accountIds.add(acct.id);
}
}
}
if(accountIds.size() > 0)
{
// Remove the shares associated with these accounts
delete
[
select id
from AccountShare
where RowCause = 'TerritoryManual' AND
AccountId IN : accountIds
];
// Reset flag that indicates this update is from a data loader.
// Data loader will set this checkbox as true on update.
// This prevents the trigger on UI update.
//
List<Account> accountsList =
[
SELECT Id, Is_Script_Update__c
FROM Account
WHERE Id IN: accountIds
];
for (Account acct : accountsList)
{
acct.Is_Script_Update__c = False;
}
update accountsList;
}
}
Degub log shows no rows returned for the first query:
14:47:14.461 (3461430000)|SOQL_EXECUTE_BEGIN|[8]|Aggregations:0|select Id, Is_Script_Update__c, Description from Account 14:47:14.483 (3483545000)|SOQL_EXECUTE_END|[8]|Rows:0
Any help is greatly appreciated!