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
Rahul AlladaRahul Allada 

How to bulkify this code? And avoid Query inside for loop

trigger dummyTrigger on Case (before insert){
    if(trigger.isBefore && trigger.isInsert){
        List<case> ca = new List<case>();
        system.debug('Inside trigger');
        for( case c: trigger.new){
            if(c.Service_Type__c == 'Apply Service' && c.entitlementid==null && c.AssetId!=null)
            {
                 system.debug('Inside If-');
                List<servicecontract> servCon=new List<servicecontract>([select id from servicecontract where AccountId=:c.AccountId and Asset__c=:c.AssetId and sla__c=null and startdate!=null]);
                Entitlement ent;
                if(servCon.size()>0)
                {
                    ent = [select id from entitlement where accountid=:c.AccountId and servicecontractid=:servCon[0].id and AssetId=:c.AssetId];
               }
                c.entitlementid = ent.id;
                c.servicecontractid = servCon[0].id;
            }   
        }
    }

}

How do I bulkify this code and avoid SOQL query inside for loop?
ANUTEJANUTEJ (Salesforce Developers) 
Hi Rahul,

> https://salesforce.stackexchange.com/questions/47469/general-trigger-bulkification-best-practices

The above link has general best practices that you can follow to bulkify your trigger implementation.

Let me know if it helps you and close your query by marking it as the best answer so that it can help others in the future.  

Thanks.
Maharajan CMaharajan C
Hi Rahu,

Please try like below:

If there is syntax error is coming please fix because i have typed in Notepad++ also read the code first to understand.

Put debug logs also wherever you want to test.
 
trigger dummyTrigger on Case (before insert){
    if(trigger.isBefore && trigger.isInsert){
        List<case> ca = new List<case>();
        system.debug('Inside trigger');
		set<Id> accId = new set<Id>();
		set<Id> assetId = new set<Id>();
		set<Id> serviceId = new set<Id>();
		Map<String,List<servicecontract>> serviceIdMap = new Map<String,List<servicecontract>>();
		Map<String,Entitlement> entMap = new Map<String,Entitlement>();
        for( case c: trigger.new){
            if(c.Service_Type__c == 'Apply Service' && c.entitlementid==null && c.AssetId!=null && c.AccountId != null)
            {
				system.debug('Inside If-');
				accId.add(c.AccountId);
				assetId.add(c.AssetId);	
            }   
        }
		
		for(servicecontract  sc :  [select id,AccountId,Asset__c from servicecontract where AccountId IN: accId and Asset__c IN: assetId and sla__c=null and startdate!=null]){
			if(sc.AccountId != null && sc.Asset__c != null){
					serviceId.add(sc.Id);
					String key = sc.AccountId + '&&' + sc.Asset__c;
					if(!serviceIdMap.containsKey(key))
						serviceIdMap.put(key, new List<servicecontract>{sc});
					else
						serviceIdMap.get(key).add(sc);
			}
		}
		
		for(Entitlement ent : [select id,AccountId,servicecontractid,AssetId from entitlement where accountid IN: accId and servicecontractid IN: serviceId  and AssetId IN: assetId]){
			if(ent.AccountId != null && ent.Asset__c != null){
				String key = ent.AccountId + '&&' + ent.AssetId + '&&' + ent.servicecontractid;
				entMap.put(key,ent);
			}
		}
		
		for( case c: trigger.new){
            if(c.Service_Type__c == 'Apply Service' && c.entitlementid==null && c.AssetId!=null && c.AccountId != null)
            {
				String Key1 = c.AccountId + '&&' + c.AssetId;
				if(serviceIdMap.containsKey(Key1)){
					List<servicecontract> scList = serviceIdMap.get(Key1);
					String Key2 = ent.AccountId + '&&' + ent.AssetId + '&&' + scList[0].Id;
					if(entMap.containsKey(Key2))
					{
						c.entitlementid = entMap.get(Key2);
						c.servicecontractid = scList[0].id;
					}
				}
            }   
        }
				
    }
}

Thanks,
Maharajan.C