You need to sign in to do that
Don't have an account?

Need help in update trigger
Hi,
I am trying to write an update trigger for the following requirement:
I have a custom object purchase order lines which has the fields:Received Quantity,Purchase order Receipt(lookup) ,Product(lookup).
I have another custom object Product_Serial which has the following fields:Purchase order Receipt(lookup) ,Product(lookup).
Now whenever the received quantity on the purchase order line is updated,I want an after update trigger to be fired that does:
*Check the count of product serial records having the product and purchase order receipt combination .
*For example, the received quantity was initially 4 and there are 4 product serial records.When the received quantity is updated to 6, the update trigger is fired
and it first checks that i have 4 product serials for this product and purchase_order_receipt combination.
*So i have to create 6-4 =2 product serial records with this product and purchase_order_receipt combination.
I have tried to create the trigger for this as follows.but iam unable to get this to work.please help me!
trigger createserial on Purchase_Order_Line__c (after update)
//Trigger to get fired when a purchase order line record is updated.
{
public Purchase_order_line__c[] pol = Trigger.new;
//Create a variable reference to the object fired by the trigger.
Integer serialcount =[select count() from product_serial__c where product__c in
(select product__c from purchase_order_line__c where id=:pol.id) and purchase_order_receipt__c in (select purchase_order_receipt_c from purchase_order_line__c where id=:pol.id )];
//Get the count of product serial records for the selected purchase_order_line
Integer diff=pol.received_quantity__c-serialcount;
//Get the difference of records to determine if any new product serial records are to be created.
for (integer i=0 ;i<diff;i++){
Product_serial__c obj = new Product_serial__c(
product__c=pol.product__c,
purchase_order_receipt__c=pol.purchase_order_receipt__c);
//create new product serial records
insert obj;
//insert is placed in loop so that for each iteration the record is inserted.
}
}
This might help you.....
trigger MyFirstObjectTrigger on purchase_order_lines__c (after update) {
Product_serial__c obj;
for(Purchase_order_lines__c pp:Trigger.new)
{
for(Integer i=0;i<pp.Received_Quantity__c;i++)
{
Integer serialcount =[select count() from product_serial__c where product__c in
(select product__c from purchase_order_lines__c where id=:pp.id) and Purchase_order_Receipt__c in (select Purchase_order_Receipt__c from purchase_order_lines__c where id=:pp.id )];
for(Integer z=0;z<pp.Received_Quantity__c-serialcount;z++)
{
obj = new Product_serial__c(name='Hari',product__c=pp.product__c,Purchase_order_Receipt__c=pp.Purchase_order_Receipt__c);
insert obj;
}
}
}
}
All Answers
What is the specific problem that you are running into. Are you getting compilation errors, or does the trigger does not behave as expected?
A few ideas --
a) Do you get the correct value for pol.received_quantity__c and serialcount? That may be something to check.
b) Since pol is an array, you will need to iterate through the elements of pol, and access its fields by something like pol[i].id rather than pol.id.
c) The statement insert obj; should be moved outside the for loop. In the loop you should create an array of Product_serial__c objects, and then insert them together outside the loop with one insert statement.
This might help you.....
trigger MyFirstObjectTrigger on purchase_order_lines__c (after update) {
Product_serial__c obj;
for(Purchase_order_lines__c pp:Trigger.new)
{
for(Integer i=0;i<pp.Received_Quantity__c;i++)
{
Integer serialcount =[select count() from product_serial__c where product__c in
(select product__c from purchase_order_lines__c where id=:pp.id) and Purchase_order_Receipt__c in (select Purchase_order_Receipt__c from purchase_order_lines__c where id=:pp.id )];
for(Integer z=0;z<pp.Received_Quantity__c-serialcount;z++)
{
obj = new Product_serial__c(name='Hari',product__c=pp.product__c,Purchase_order_Receipt__c=pp.Purchase_order_Receipt__c);
insert obj;
}
}
}
}
Hi guys,
The trigger is giving me compilation errors...
please help me to iterate through an array and use an insert statement outside the loop as suggested.
The compilation error I am getting is:
"Error: Compile Error: semi join sub selects can only query id fields, cannot use: 'Purchase_order_Receipt__c' at line 10 column 22"
My code is :
trigger MyFirstObjectTrigger on purchase_order_line__c (after update) {
Product_serial__c obj;
for(Purchase_order_line__c pp:Trigger.new)
{
for(Integer i=0;i<pp.Received_Quantity__c;i++)
{
Integer serialcount =[select count() from product_serial__c where product__c in
(select product__c from purchase_order_line__c where id=:pp.id) and Purchase_order_Receipt__c in (select Purchase_order_Receipt__c from purchase_order_line__c where id=:pp.id )];
for(Integer z=0;z<pp.Received_Quantity__c-serialcount;z++)
{
obj = new Product_serial__c
(product__c=pp.product__c,Purchase_order_Receipt__c=pp.Purchase_order_Receipt__c);
insert obj;
}
}
}
}
It works fine for me.....
Hi Giri,
Ok i guess the purchase_order_receipt__c must be a lookup field(i had a text field) and i changed that....no longer getting that error...
Now iam getting this error:
Error: Compile Error: Invalid field product__c for SObject Purchase_Order_Line__c at line 18 column 13
This is at this part:
obj = new Product_serial__c
(product__c=pp.product__c,Purchase_order_Receipt__c=pp.Purchase_order_Receipt__c);
insert obj;
product__c is a valid field name for purchase_order_line__c object!!!
Please help!!!!!!!
okays...finally got this one!
Thanks guys!