You need to sign in to do that
Don't have an account?

How to bulkify a trigger.
Hi all,
I have written a trigger on Tenant_Details__c object which has a lookup relationship to Room_Details__c object. Every time a new record is inserted, I want to update a field in Room_Details__c and all the Tenants present in that Room in which that tenant has been added. My trigger is working when I insert an individual record, but when I try to enter multiple records simultaneously, then it throws an error. The reason is that it is not bulkified. Con someone please help me bulkify this code.
trigger addTenant on Tenant_Details__c (after insert) { List<Tenant_Details__c> tenantDetailList = [select Contribution__c, Stays_in__c from Tenant_Details__c where id in :Trigger.new]; List<Tenant_Details__c> tenantDetailListToUpdate = new List<Tenant_Details__c>(); List<Room_Details__c> roomDetailListToUpdate = new List<Room_Details__c>(); for(Tenant_Details__c tenant : tenantDetailList) { Room_Details__c rooms=[select Rent__c, Sharing_Capacity__c, Present_Tenant_Count__c from Room_Details__c where id = :tenant.Stays_in__c]; System.debug('Trigger.isInsert'); System.debug('Previous tenant count:'+rooms.Present_Tenant_Count__c); if(rooms.Present_Tenant_Count__c<rooms.Sharing_Capacity__c) { rooms.Present_Tenant_Count__c=rooms.Present_Tenant_Count__c+1;//3. updating tenant count } else { System.debug('Maximum tenant count reached'); Trigger.oldMap.get(rooms.Id).addError('Cannot add tenants to this room.');//4. add error on max capacity } //5. increase rent of each tenant System.debug('New tenant count: '+rooms.Present_Tenant_Count__c); Double contri = rooms.Rent__c/rooms.Present_Tenant_Count__c; tenant.Contribution__c=contri; for(Tenant_Details__c allTenants:[select Contribution__c from Tenant_Details__c where Stays_in__c=:tenant.Stays_in__c]) { allTenants.Contribution__c=contri; tenantDetailListToUpdate.add(allTenants); } roomDetailListToUpdate.add(rooms); } update roomDetailListToUpdate; update tenantDetailListToUpdate; }
1 - Take the code you written and put it into a separate Apex class. As a best practice, one should never write code within an Apex Trigger. In the Apex Trigger, one should only specify what method to call from the handler class.
2 - Always define SOQL statements outside of for loops. The reason is Salesforce imposes a governor limit of 100 SOQL queries per transcation. I believe this is why you are receiving errors when processing large amounts of records.
I've made the necessary changes to your code and put comments.. If you have any questions, please feel free to reach out to me.
Please view below:
Apex Class:
Apex Trigger:
All Answers
1 - Take the code you written and put it into a separate Apex class. As a best practice, one should never write code within an Apex Trigger. In the Apex Trigger, one should only specify what method to call from the handler class.
2 - Always define SOQL statements outside of for loops. The reason is Salesforce imposes a governor limit of 100 SOQL queries per transcation. I believe this is why you are receiving errors when processing large amounts of records.
I've made the necessary changes to your code and put comments.. If you have any questions, please feel free to reach out to me.
Please view below:
Apex Class:
Apex Trigger:
Thanks for the quick reply. There was a small issue in Line 10. Actually the Parent object is the Room_Details__c and it can have multiple child objects of Room_Details__c. And Tenant_Details__c has a Lookup Relationship to Rooms_Details__c. Since I am new to Salesforce, I am not able to rectify it on my own.
Thanks for your help.
02
03 //This only handles the first record in the Trigger.new collection
04 //But if more than 1 Account initiated this trigger, those additional records
05 //will not be processed
06 Account acct = Trigger.new[0];
07 List<Contact> contacts = [select id, salutation, firstname, lastname, email
08 from Contact where accountId = :acct.Id];
09
10}
The issue is that only one Account record is handled because the code explicitly accesses only the first record in the Trigger.new collection by using the syntax Trigger.new[0]. Instead, the trigger should properly handle the entire collection of Accounts in the Trigger.new collection.
Here is a sample of how to handle all incoming records:
view sourceprint?
01trigger accountTestTrggr on Account (before insert, before update) {
02
03 List<String> accountNames = new List<String>{};
04
05 //Loop through all records in the Trigger.new collection
06 for(Account a: Trigger.new){
07 //Concatenate the Name and billingState into the Description field
08 a.Description = a.Name + ':' + a.BillingState
09 }
10
11}
https://www.walgreenlistens.one/