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
emuelasemuelas 

Trigger to update lookup field after insert not working

Hi,

I have a custom object Product_Serial__c which has  a lookup field to another object Product_inventory__c.

The product_inventory__c has a lookup to Product.
The product serial record also has a lookup to product


Now when a user creates a new Product Serial record ,
i created a trigger on the Product serial that updates the product value from the product_inventory which the user enters to create a record.

But this update is not happening.Please help.


Trigger updprod on Product_serial__c(after insert)

{

Set<Id> ids = trigger.newmap.keySet();

List<Product_serial__c> pser=[select product_inventory__c,product__c,Product_Inventory__r.Product__r.Id from  product_serial__c where id IN :ids];


For(Product_serial__c psers:pser)

{

 psers.product__c=psers.Product_Inventory__r.Product__r.Id;

update psers;

}

}

ABRAR-UL-HAQABRAR-UL-HAQ

its really works......

bob_buzzardbob_buzzard

Do you get an error, or does the data not update as you would expect?

 

Have you tried adding debug statements to check that the field that you retrieve from the related Product_Inventory__c is correctly populated etc?

 

 

emuelasemuelas

Hi Bob,

 

There is no error,but the value is not updating..

bob_buzzardbob_buzzard

I can't see anything obviously wrong with  your trigger.  Try adding some debug and turning on debug logs via the monitoring menu.  Something like:

 

 

Trigger updprod on Product_serial__c(after insert)
{
   Set<Id> ids = trigger.newmap.keySet();
   List<Product_serial__c> pser=[select    
        product_inventory__c,product__c,
        Product_Inventory__r.Product__r.Id from  product_serial__c where id IN :ids];

   System.debug('#### Product serials are ' + pser);
   For(Product_serial__c psers:pser)
   {
      System.debug('#### Overwriting ' + psers.product__c + 
           ' with ' + psers.Product_Inventory__r.Product__r.Id);
      psers.product__c=psers.Product_Inventory__r.Product__r.Id;
      update psers;
   }
}