function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Chelsea LukowskiChelsea Lukowski 

Custom Cross Object field update

I need to create a trigger that updates the Inventory_On_Hand__c field on  custom object Oreder_Product__c, with the value of the Available__c field from the object call Inventory__c. The matching point between the two is the product number, which is on both objects as a field called Product__c. I do not know where to begin. Any help would be appriciated. 
Abhishek BansalAbhishek Bansal
Hi Chelsea,

If your Product__c is a lookup field than please try the below code :
trigger updateInventoryOnHand on Inventory__c(after insert, after update){
	Set<Id> productIds = new Set<Id>();
	Boolean updateValues = false;
	for(Inventory__c newInvent : trigger.new){
		productIds.add(newInvent.Product__c);
	}
	
	Map<Id,Oreder_Product__c> mapOfProductWithOrder = new Map<Id,Oreder_Product__c>();
	
	for(Oreder_Product__c order : [Select Inventory_On_Hand__c, Product__c from Oreder_Product__c where Product__c in :productIds]){
		mapOfProductWithOrder.put(order.Product__c, order);
	}
	
	for(Inventory__c newInvent : trigger.new){
		if(mapOfProductWithOrder.containsKey(newInvent.Product__c)){
			mapOfProductWithOrder.get(newInvent.Product__c).Inventory_On_Hand__c = newInvent.Available__c;
			updateValues = true;
		}
	}
	
	if(updateValues){
		update mapOfProductWithOrder.values();
	}
}

If Product__c is not a lookup field than we have to make changes in trigger.
So please let me know the Data type of Product__c field so that i can modify the trigger code accordingly.
Please also verify the API name of all objects and fields used in the above trigger.

Let me know if you have any issues.

Thanks,
Abhishek
Chelsea LukowskiChelsea Lukowski
Product__c is a lookup field.  
Abhishek BansalAbhishek Bansal
Fine Chelsea,

Please move ahead with the code which i have provided you.
Please verify the API names of objects and fields in the code.

Let me know if you have any issues in the code.

Regards,
Abhishek
Chelsea LukowskiChelsea Lukowski
I just realized I spelled order wrong for Order_Product__c. I fixed it in the code and tested, but I got the error


Error: Compile Error: Illegal assignment from Decimal to String at line 16 column 61
 
Abhishek BansalAbhishek Bansal
Hi Chelsea,

It would be good if you can contact me on gmail or skype.
Gmail : abhishek.bansal@metacube.com 
Skype : abhishek.bansal2790

If you want a solution here than please tell me the data type of following fields :
  1. Inventory_On_Hand__
  2. Available__c
Please tell me the data types of above fields so that i can help you out.

Thanks,
Abhishek
Chelsea LukowskiChelsea Lukowski
I will email you.