0

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 also a field in 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
  • 2
    What is the error message?
    – eaeaoo
    Feb 23 '20 at 10:33
1

Beyond bulkification, I think you are likely to have an issue with this line:

Trigger.oldMap.get(rooms.Id).addError('Cannot add tenants to this room.');

Because it looks to me like you are trying to retrieve a Tenant_details__c record using the id from a room_detail__c record.

Regarding bulkification, your updates are outside of the for loop, you could also pull out the SOQL query -

You can make a list with all of the ids you are going to query on, make a WHERE id IN :list query, put that into a map with the list id as the key, then within your for loop retrieve the relevant record from the map rather than from a SOQL query.

List<Id> tenantRoomIds = new List<Id>();
for(Tenant_Details__c tenant : tenantDetailList){
     tenantRoomIds.add(tenant.Stays_in__c);
} 
Map<Id, Room_Details__c> Room_Details__c rooms= Map<ID, Room_Details__c>([select
 Rent__c, Sharing_Capacity__c, Present_Tenant_Count__c from Room_Details__c where
 id IN :tenantRoomIds]);

for(Tenant_Details__c tenant : tenantDetailList){
    room_detail__c room = rooms.get(tenant.Stays_in__c);
...

You've also got a nested SOQL for loop :

        for(Tenant_Details__c allTenants:[select Contribution__c from Tenant_Details__c where Stays_in__c=:tenant.Stays_in__c])

I think you'd be better off retrieving the child objects from your query on room, and then looping through those child objects so rooms query becomes -

Map<Id, Room_Details__c> Room_Details__c rooms= Map<ID, Room_Details__c>(
[select Rent__c, Sharing_Capacity__c, Present_Tenant_Count__c, 
(SELECT contribution__c FROM Tenant_Details__r) 
from Room_Details__c where
 id IN :tenantRoomIds])

Then you can replace your SOQL for loop with:

for(tenant_details__c tenant :room.tenant_details__r){
    tenant.Contribution__c=contri;
    tenantDetailListToUpdate.add(tenant);

}

You might also have an issue with updating tenants within a tenant trigger - could get into infinite loop, so watch out for that.

1
  • Thank you so much for your help. It worked perfectly.
    – Ayush Roy
    Feb 24 '20 at 12:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.