2

can anybody tell me why this trigger only works when the record is created but not when is updated. For example, if I create a new record the trigger will set the buyer name correctly but if I changed the NRProduct__c it will not get the new buyer's name.

I will appreciate any help.

trigger buyerlookupTriggerMWSItem on MWSOrderItem__c (before update, before insert) {
try{

    MWSOrderItem__c[] results =
           [SELECT id , NRProduct__r.Buyer_Lookup__c  
              FROM MWSOrderItem__c
              WHERE id IN :Trigger.new
           ];

    Map<id,id> Map1 = new Map<id,id>();
    for(MWSOrderItem__c rod : results)
        Map1.put(rod.NRProduct__r.Buyer_Lookup__c,rod.id);


    User s = [SELECT Id FROM User WHERE id IN :Map1.keySet()];
    if(s != null){
        for(MWSOrderItem__c r : Trigger.new){
            if(String.isEmpty(r.Buyer_Lookup__c)){
                r.Buyer_Lookup__c = s.Id;
            }
        }
    }
}
catch(Exception ex){}
}
5
  • 2
    Are you positive that the Buyer_Lookup__c on the MWSOrderItem__c objects being updated are already null? And do you have any workflow rules with field update actions on MWSOrderItem__c?
    – greenstork
    Jul 7, 2014 at 22:54
  • Hi greenstork, we do have some workflows for this Object, do u think that can be the reason the trigger is not updating?
    – Carlos
    Jul 7, 2014 at 23:42
  • 1
    No, I think the reason was answered by @brovasi. There are a number of issues with your code addressed with the answer given.
    – greenstork
    Jul 7, 2014 at 23:44
  • 1
    I think your code was working for you as a fluke, as a result of the workflow rules causing the trigger to fire twice but the solution is as brovasi mentioned.
    – greenstork
    Jul 7, 2014 at 23:45
  • good to know that, but i am getting an error with that solution, perhaps because NRProduct__c is a master-relation field
    – Carlos
    Jul 7, 2014 at 23:49

1 Answer 1

5

A few things with your code :

  • try and catch is rarely used in a trigger
  • buyer_lookup is a lookup to user, so why do you want to query the User if you already have the id
  • On before insert, usually you don't query the records themselves, they don't exist yet

UPDATED: Modified the code to include the name of the buyer if you need

Check if the following code is working :

trigger buyerlookupTriggerMWSItem on MWSOrderItem__c (before update, before insert) {
    Map<id,NRProducts__c> nrProducts = new Map<id,NRProducts__c>();
    for(MWSOrderItem__c rod : Trigger.new){
        nrProducts.put(rod.NRProduct__c,null);
    }
    nrProducts = new Map<ID,NRProduct__c>([select id, Buyer_Lookup__c, Buyer_Lookup__r.Name from NRProducts__c where Id IN :nrProducts.keySet()]);

    for(MWSOrderItem__c rod : Trigger.new)
      if(nrProducts.get(rod.NRProduct__c) != null){
          rod.Buyer_Lookup__c = nrProducts.get(rod.NRProduct__c).Buyer_Lookup__c;
          //rod.Buyer_Lookup__c = nrProducts.get(rod.NRProduct__c).Buyer_Lookup__r.Name;
      }
    }
}
6
  • i am gonna try ur solution now
    – Carlos
    Jul 7, 2014 at 23:10
  • Brovasi, I am getting an error: "Invalid type: NRProduct__c at line 3 column 50." Just you know NRProduct__c field is a master-relation field to the Object NRProducts__c
    – Carlos
    Jul 7, 2014 at 23:16
  • Sorry I don't know your data model, so you might need to make a few changes, but the idea is there.
    – brovasi
    Jul 7, 2014 at 23:20
  • I appreciate it, and how about the test class for this approach?
    – Carlos
    Jul 7, 2014 at 23:21
  • probably insert a NRProduct with a buyer, then insert a MWSOrderItem. Have a try and if you have any question, create a new post. Cheers
    – brovasi
    Jul 7, 2014 at 23:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .