You need to sign in to do that
Don't have an account?
Trigger to create records on unrelated objects
Hi,
I have a custom object product_inventory__c which has a lookup field to product.
I have another custom object order_line__c which has a lookup field to product.
The requirement is,every time an order line is created,i need to check in the product inventory if any record exists for that product.
If record in product inventory does not exist ,I need to create a new product_inventory_record for the product.
To achieve this ,I tried writing a trigger on order_line__c object,that would create a new product_inventory record if it is not present.
But since product inventory and order line do not share a master detail relationship,Iam not sure how this goes.i tried writing the following sample code:
trigger inventory on Order_Line__c(after insert) {
Order_Line__c ol=trigger.new;
for (Order_Line__c neworder: Trigger.New) {
Integer i=[select count() from Product_Inventory__c where Product__c :=ol.Product__c];
if(i=0)
{
Product_Inventory__c.add(new Product_Inventory__c(
product__c=neworder.product));
}
}
}
This is erroring out of course because i have not mapped any id's....but iam not sure how to do that since i just want to compare if any record with the product is existing
Please help...
You have a query running inside a for loop, which is a bad idea. This will hit the governors pretty quickly. Let's fix that while we're looking at your real issue.
Basically, you want to go through the order lines, and for each one, check if there's already a product inventory for that order line. If not, then create one. We have to split out the querying and the inserting from the loops, which means every query and insert should be in bulk. Maybe we can do it with one of each.
There are other ways to do it, but see if that basically works for you. Check for typos, etc.
Jeremy
All Answers
You have a query running inside a for loop, which is a bad idea. This will hit the governors pretty quickly. Let's fix that while we're looking at your real issue.
Basically, you want to go through the order lines, and for each one, check if there's already a product inventory for that order line. If not, then create one. We have to split out the querying and the inserting from the loops, which means every query and insert should be in bulk. Maybe we can do it with one of each.
There are other ways to do it, but see if that basically works for you. Check for typos, etc.
Jeremy
Thank you Jeremy,
This was exactly what was i was looking for.The explanation was also very clear and concise for a beginner like me
Can U please help me with this one .
I have two objects parent__c and Teacher__c . There is no relationship between them .no materdetail no lookup .
only name fields in both parent__c and teacher__c will be treated as common , because we will manual enter the name field .
Parent fields: name(text )
, teacher_expertise (text)
Teacher Name (text)
Teacher fields :
- name (text)
expertise (picklist)
Teacher_name( Text Fields)
I would like to update teacher_expertise and Teacher_name field on parent objects :
Once we update fields on Teacher__c it should update Parent__c.
how can we use maps in this .
Please help . Urgent
Thanks