+ Start a Discussion
T-S.comT-S.com 

How to bulkify trigger

Hi all,

 

I'm pretty new to SFDC development and I need to bulkify following trigger. I've checked multiple resources, but couldn't move forward. :(  Every help will be highly appreciated!

 

Regs

 

---------------------------------

 

trigger BI_Rental_CheckAssetStatus on Rental__c (before insert) {

// Disables to rent assets with other status than Available.

    for(Rental__c myRental : trigger.new){

        Asset__c myAsset = [SELECT status__c FROM Asset__c WHERE Id = :myRental.Asset__c];
    
        if(myAsset.status__c != 'Available'){
            myRental.addError('You cannot borrow asset, which has a different status than Available. Current asset status is ' + myAsset.status__c + '.');
        }

    }

}

 

---------------------------------

Best Answer chosen by Admin (Salesforce Developers) 
VKrishVKrish

Try this:

 

trigger BI_Rental_CheckAssetStatus on Rental__c (beforeinsert) {

   

Set<id> assetId = newSet<id>();

Map<id, Asset__c> assetMap =new Map<id, Asset__c>();

for(Rental__c myRental : trigger.new)

  assetId.add(myRental.Asset__c);

assetMap.putAll([SELECT status__c FROM Asset__c WHERE Id IN: assetId]);

for(Rental__c myRental : trigger.new){

  Asset__c asset = assetMap.get(myRental.Asset__c);       

if(asset.status__c != 'Available')

{

            myRental.addError('You cannot borrow asset, which has a different status than Available. Current asset status is ' + asset.status__c + '.');

        }

    }

}

 

All Answers

VKrishVKrish

One major point to follow in bulking a trigger is: there should not be any query inside any loop.

Use List, Set or Maps.

EG:

List<Id> conId = new List<Id>();

for(Contact c: trigger.new)

  conId.add(c.Id)

List<Account> acc = new List<Account>([Select id, name from account where id IN: conId]);

 

Go to salesforce tutorial for best practise. There you find all the list of points you to consider while bulking a trigger & writting test class for bulk testing

T-S.comT-S.com

Hi Vkrish,

 

thank for your proposal. I've already checked your proposed example before, unfortunatelly I am not able to convert it on my scenario, where the "roles" of objects are quite different (there's no 1:n relationship between Rental and Asset, which however exists between Account and Contact in your scenario.)

 

Thanks

VKrishVKrish

Try this:

 

trigger BI_Rental_CheckAssetStatus on Rental__c (beforeinsert) {

   

Set<id> assetId = newSet<id>();

Map<id, Asset__c> assetMap =new Map<id, Asset__c>();

for(Rental__c myRental : trigger.new)

  assetId.add(myRental.Asset__c);

assetMap.putAll([SELECT status__c FROM Asset__c WHERE Id IN: assetId]);

for(Rental__c myRental : trigger.new){

  Asset__c asset = assetMap.get(myRental.Asset__c);       

if(asset.status__c != 'Available')

{

            myRental.addError('You cannot borrow asset, which has a different status than Available. Current asset status is ' + asset.status__c + '.');

        }

    }

}

 

This was selected as the best answer
T-S.comT-S.com

Thank you for your quick and precise help Vkrish,

 

works perfectly!