+ Start a Discussion
Lavanya Ponniah 3Lavanya Ponniah 3 

How to do the below trigger?

I need assistance with the below trigger, the line which i bold and underlined.When i insert bulk record using dataloader.io for all records v.Pending_Hardware_Installations_Order__c = ordr.Left_Hardware__c; is to reflect the updated ordr.Left_Hardware__c ,but for all records its not reflecting the updated ordr.Left_Hardware__c to the v.Pending_Hardware_Installations_Order__c .
trigger InstalledHardware on Vehicle_Details__c (before insert)
{
    Set<Id> setOrderIds = new Set<Id>();
    Map<Id,Order> mapIdWithOrder = new Map<Id,Order>();
    for(Vehicle_Details__c V:trigger.new){
        if(v.Order__c != null){
           setOrderIds.add(v.Order__c); 
        }
    }
    if(!setOrderIds.isEmpty()){
        for(Order ordr :[select Order_Number__c,Finished_HardWare__c,Left_Hardware__c,Total_Hardware_Type_Ordered_Products__c from Order where id IN :setOrderIds]){
            mapIdWithOrder.put(ordr.Id,ordr);    
        }
    }
    for(Vehicle_Details__c V:trigger.new){
        if(v.Order__c != null && mapIdWithOrder != null && mapIdWithOrder.get(v.Order__c) != null){
           Order ordr = mapIdWithOrder.get(v.Order__c);
           v.Pending_Hardware_Installations_Order__c = ordr.Left_Hardware__c;
           v.Total_Hardware_Quantity_for_Order__c = ordr.Total_Hardware_Type_Ordered_Products__c; 
           ordr.Finished_HardWare__c = ordr.Finished_HardWare__c + V.Total_Hardware_Installed__c;
           mapIdWithOrder.put(v.Order__c,ordr)
        }
    }
    if(mapIdWithOrder.values().size() > 0)
    {
        update mapIdWithOrder.values();
    }
}
Vidya DVidya D
First you can add System.debug to see if all records have value for ordr.Left_Hardware__c.  It may not be send to trigger if data is not present or may have null value.

 
SivaGSivaG
Hi,

In the above trigger you are only updating Order object. Try updating Vehicle_Details__c object.

Thanks
Kumar

If this answers your question, please mark this as Best Answer.
Vidya DVidya D
It's before Insert trigger and it is on 
Vehicle_Details__c, during insert value should be automatically updated.

Also it is incorrect to update related object in insert trigger, related objects should be updated in after trigger
Zuinglio Lopes Ribeiro JúniorZuinglio Lopes Ribeiro Júnior
Hello,

I cannot see any problem with your trigger. I would say that you just don't have the information that you want to reflect it into the vehicle object. I would follow Vidya D suggestion to ensure that the data exists. However do not forget to remove your System.debug lines as it count against Salesforce execution limits.
 
trigger InstalledHardware on Vehicle_Details__c (before insert)
{
    Set<Id> ordersIds = new Set<Id>();
    Map<Id,Order> orders = new Map<Id,Order>();
	
    for (Vehicle_Details__c vehicle : Trigger.new) {
        if (vehicle.Order__c != null) ordersIds.add(vehicle.Order__c);
    }

	for (Order ordr : [SELECT 	Order_Number__c,
								Finished_HardWare__c,
								Left_Hardware__c,
								Total_Hardware_Type_Ordered_Products__c 
					FROM Order WHERE Id =:ordersIds]) {
		orders.put(ordr.Id,ordr);    
	}
	
    for(Vehicle_Details__c vehicle: trigger.new) {
        
		if (orders.containsKey(vehicle.Order__c)) {
           Order ordr = orders.get(vehicle.Order__c);
           vehicle.Pending_Hardware_Installations_Order__c = ordr.Left_Hardware__c;
           vehicle.Total_Hardware_Quantity_for_Order__c = ordr.Total_Hardware_Type_Ordered_Products__c; 
           ordr.Finished_HardWare__c = ordr.Finished_HardWare__c + vehicle.Total_Hardware_Installed__c;
        }
    }
	
    if(!orders.isEmpty()) Database.update(orders.values());
	
}

Regards.

Don't forget to mark your thread as 'SOLVED' with the answer that best helps you.