I am receiving
System.LimitException: Too many SOQL queries: 101
error because of my Apex trigger.
Issue is clear but I am thinking of best possible way to handle it during bulk data loads. We will have at max of 500 tasks to load usually, so is batch apex needed or can handle it somehow only in trigger? Here Contact(WhoId) is not child object, so for each row, we need to query on Contacts and if validation fails, just add error to file and proceed with next row when dealing with Dataloader, else just add an error before save.
trigger ValidateContStatus_Task on Task (before insert, before update)
{
for(Task t : trigger.new)
{
//If condition checks if it's not Lead
if(t.whoId != NULL && String.valueOf(t.whoId).startsWith('00Q')== FALSE)
{
Contact c = [SELECT Id, Contact_Status__c from Contact where Id =: t.WhoId Limit 100];
if(C.Contact_Status__c == 'Inactive')
{
t.whoId.addError('This Contact is Inactive, please pick an Active Contact');
}
}
}
}