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
BrianJBrianJ 

Need Help To Bulkify a Trigger

I created a trigger that outputs error if checkbox is true and no document is attached. 

It's working great. However, I've read here that I should bulkify it so that I don't hit SOQL query limits. 

I need help bulkifying the below code. Thank you :)
trigger ReportRequestTrigger on Report_Request__c (before update, after update) {
    for(Report_Request__c rr:Trigger.New) {
        if(rr.Will_Receive_Provider_Facility_Claims__c== TRUE) {
            LIST<ContentDocumentLink> attachments = [SELECT ContentDocumentId, LinkedEntityId  FROM ContentDocumentLink where LinkedEntityId = : rr.Id];
            if(attachments.size()<=0){
                rr.adderror('You cant select "Will Receive Provider Facility Claims" without attaching a Zip Census');
            }
        }
    }
}

 
Best Answer chosen by BrianJ
Maharajan CMaharajan C
Hi Brain,

Try the below one:
 
trigger ReportRequestTrigger on Report_Request__c (before update, after update) {
	set<Id> parentIdSet = new set<Id>();
    for(Report_Request__c rr:Trigger.New) {
        if(rr.Will_Receive_Provider_Facility_Claims__c== TRUE) {
            parentIdSet.add(rr.Id);
        }
    }
	
	if(!parentIdSet.IsEmpty()){
		set<Id> linkedEntitySet = new set<Id>();
		for( ContentDocumentLink cdl :  [SELECT Id, ContentDocumentId, LinkedEntityId  FROM ContentDocumentLink where LinkedEntityId IN : parentIdSet ]){
			linkedEntitySet.add(LinkedEntityId);
		}
		
		for(Report_Request__c rr:Trigger.New) {
			if(rr.Will_Receive_Provider_Facility_Claims__c== TRUE && !linkedEntitySet.contains(rr.Id)) {
				rr.adderror('You cant select "Will Receive Provider Facility Claims" without attaching a Zip Census');
			}
		}
	}
}

Thanks,
Maharajan.C

All Answers

Maharajan CMaharajan C
Hi Brain,

Try the below one:
 
trigger ReportRequestTrigger on Report_Request__c (before update, after update) {
	set<Id> parentIdSet = new set<Id>();
    for(Report_Request__c rr:Trigger.New) {
        if(rr.Will_Receive_Provider_Facility_Claims__c== TRUE) {
            parentIdSet.add(rr.Id);
        }
    }
	
	if(!parentIdSet.IsEmpty()){
		set<Id> linkedEntitySet = new set<Id>();
		for( ContentDocumentLink cdl :  [SELECT Id, ContentDocumentId, LinkedEntityId  FROM ContentDocumentLink where LinkedEntityId IN : parentIdSet ]){
			linkedEntitySet.add(LinkedEntityId);
		}
		
		for(Report_Request__c rr:Trigger.New) {
			if(rr.Will_Receive_Provider_Facility_Claims__c== TRUE && !linkedEntitySet.contains(rr.Id)) {
				rr.adderror('You cant select "Will Receive Provider Facility Claims" without attaching a Zip Census');
			}
		}
	}
}

Thanks,
Maharajan.C
This was selected as the best answer
BrianJBrianJ
I'm getting LinkedEntityId variable does not exist error. 
Maharajan CMaharajan C
Hi Brain,

Please use the below code:
 
trigger ReportRequestTrigger on Report_Request__c (before update, after update) {
	set<Id> parentIdSet = new set<Id>();
    for(Report_Request__c rr:Trigger.New) {
        if(rr.Will_Receive_Provider_Facility_Claims__c== TRUE) {
            parentIdSet.add(rr.Id);
        }
    }
	
	if(!parentIdSet.IsEmpty()){
		set<Id> linkedEntitySet = new set<Id>();
		for( ContentDocumentLink cdl :  [SELECT Id, ContentDocumentId, LinkedEntityId  FROM ContentDocumentLink where LinkedEntityId IN : parentIdSet ]){
			linkedEntitySet.add(cdl.LinkedEntityId);
		}
		
		for(Report_Request__c rr:Trigger.New) {
			if(rr.Will_Receive_Provider_Facility_Claims__c== TRUE && !linkedEntitySet.contains(rr.Id)) {
				rr.adderror('You cant select "Will Receive Provider Facility Claims" without attaching a Zip Census');
			}
		}
	}
}



Thanks,
Maharajan.C
BrianJBrianJ
This may be a seperate issue, but I'm getting code coverage error when attempting to validate. It says I must have code ceverage of at least 1%. Is it because I need a test class?