+ Start a Discussion
Nevin O'Regan 3Nevin O'Regan 3 

Trigger to update lookup field on Product object

Hi guys,

I have a custom object Target__c that is a parent object of Product2. When a  new Target__c record is created I want to automatically update the Target__c lookup field on the Product2 object with the latest Target__c record. I have tried the below trigger but I'm running into an Error: duplicate value found: <unknown> duplicates value on record with id: <unknown>

trigger MapTargetsToWarehouse on Targets__c (after insert) {
    for(Targets__c target : Trigger.new) {
        Product2[] products = [SELECT Id, Target__c FROM Product2];
        if (products.size() > 0) {
            Product2 product = products[0];
            product.Target__c  = target.Id;

            update product;