1

I am new to Apex. The scope of the trigger are 2 objects. Product__c and Specific_Product__c.

There is an after insert trigger on Product__c to create a corresponding record on Specific_Product__c (only selected fields are being copied over). I have also added the Id of the Product__c to be passed onto the Specific_Product__c so the record on Specific_Product__c could potentially be easily referenced against Product__c.

I am struggling with writing a trigger that would update the corresponding record on Specific_Product__c when an existing record on Product__c gets updated. There are no lookup or master-detail relationships between objects.

Is the above scenario possible?

The code is:

trigger UpdateSpecificProduct on Product__c (after update) {

    List<Id> idProduct = new List<Id>{};

    for(Product__c p: Trigger.new){
        idProduct.add(p.id);
    }


    Specific_Product__c[] prodToUpdate = [select id 
                                            from Specific_Product__c 
                                            where Specific_Product__c.Product_ID__c IN :idProduct];

    {
        Description__c = p.Description__c,
        Availability__c = p.Availability__c,
    }

    update prodToUpdate;

}
  • Im struggling to understand the architecture here. You say 'update the corresponding record on Specific_Product__c when an existing record on Product__c gets updated. There are no lookup or master-detail relationships between objects.' If there is no relationships between them, then what do you mean by corresponding? – Chris Duncombe Aug 28 '14 at 19:16
  • Sorry for a confusion. By corresponding I mean the record on Specific_Product__c that contains the Id of a record on Product__c (auto-populated by other trigger). – Jerald Aug 28 '14 at 19:32
  • I don't follow. You are carrying the Id of a product on your specific_Product__c record, but its not in a lookup field? – Chris Duncombe Aug 28 '14 at 19:39
  • Is it possible for you to create a lookup on Specific_Product__c to Product__c? (it could simplify your implementation) – Mark Pond Aug 28 '14 at 19:40
1

You are quering Specific_Product__c Object with Parent id of Product__c object. But there are no any Relationship between those objects, then how it will query his Parent. First create any M/s or Look-up relationship. Then your trigger will run Properly.

|improve this answer|||||
0

My guess is that description and availability are fields on the specific product object. You will have to add these field references in the query. Also, run a loop on the prodtoUpdate list and crosscheck with the correct product.

I would suggest the below approach.

trigger UpdateSpecificProduct on Product__c (after update) {

List<Id> idProduct = new List<Id>{};
Map<Id, Product__c> productDetails = new Map<Id,Product__c>();

for(Product__c p: Trigger.new){
    idProduct.add(p.id);
    productDetails.put(p.id,p);}


Specific_Product__c[] prodToUpdate = [select id,description__c, availability__c, Product_ID__c from Specific_Product__c where Specific_Product__c.Product_ID__c IN :idProduct];
for(Specific_Product__c specific : prodToUpdate){

    specific.Description__c = productDetails.get(specific.product_id__c).Description__c;
    specific.availability__c = productDetails.get(specific.product_id__c).availability__c

}

update prodToUpdate;

}

The purpose of the Map here is to avoid a second loop where you would search for the product in the first list.

However, according to me, a better design would be if you store the product as a lookup field (best practice :)).

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.