You need to sign in to do that
Don't have an account?
erikvm
Writing Test Class with SOQL in Trigger?
Hi,
I'm trying to write a test class for my trigger. I tried the trigger in my environment and it works fine, but I can't get the code coverage on the test class, and not really sure how. Here's what the trigger looks like:
I'm trying to write a test class for my trigger. I tried the trigger in my environment and it works fine, but I can't get the code coverage on the test class, and not really sure how. Here's what the trigger looks like:
trigger AccountTrigger on Account (before insert) { list<Account> Acc = [SELECT Id, Name, Generated_ID__c FROM Account WHERE Generated_ID__c != null ORDER BY Generated_ID__c DESC LIMIT 1]; String Record_ID = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Billing').getRecordTypeId(); if(Acc.size() > 0){ decimal maxLead = Acc[0].Generated_ID__c; for (Account acco : Trigger.new) { if (acco.RecordTypeId == Record_ID) { acco.Generated_ID__c = Integer.valueOf(maxLead)+1; maxLead++; } } } }
And my test class:
@IsTest private class GenerateExternalIdTest { private static String Record_ID = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Billing').getRecordTypeId(); @isTest static void createNewAccount() { Account newAccount = (Account) TestDataGenerator.createSObject(new Account(RecordTypeId = Record_ID),'TestDataGenerator.AccountDefaults'); insert newAccount; } }
My issue is that I don't know where to put the query. Right now, the test class code coverage is 37,5% - it skips the actual loop.
Thanks in advance!
IF(!Acc.isEmpty()){
}