• Ayush Roy
  • NEWBIE
  • 10 Points
  • Member since 2020

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 1
    Replies

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;
	
}

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;
	
}