0

I have created trigger on parent object (Vendor__c) to update child records(Transaction__c). Logic : when the Vendor Manager field is updated on a Vendor record, look for Transaction records associated to that Vendor record and update the Vendor Manager lookup field for those Transactions, but ONLY IF the Vendor Manager on each Transaction record is blank. If the Vendor Manager for a Transaction is already populated, skip that record.

Trigger :

trigger VendorManager on Vendor__c (before insert, before update,after insert,after update) 
{
            List<id> Vid = new List<id>();
            for(Vendor__c v:Trigger.new)
                  {
                    Vid.add(v.id);
                  }

            List<Vendor__c> vlst = [select id,name,Manager__c,(select id,Name,Manager1__c,Vendor__c from Transactions__r where Vendor__c=:Vid) from Vendor__c ];
            List<Transaction__c> Tlst = new List<Transaction__c>();

                    for(Vendor__c vv:vlst)
                     {
                        for(Transaction__c t:vv.Transactions__r)
                        {
                          if(t.Manager1__c =='')
                          {
                            t.Manager1__c = vv.Manager__c;
                          }
                        Tlst.add(t);
                        }
                      }
                      update Tlst;
}

I have checked all the things but not sure why it is not updating any transaction record.Any help appreciated.

1 Answer 1

0

I think the problem is how you are validating the Manager1__c field. If it is a relationship the condition

if(t.Manager1__c=='')

wont work. try using

if(t.Manager1__c==null)

and also you can bring the transactions that Manager1__c field is null with the query, try this one.

List<Vendor__c> vlst = [select id,name,Manager__c,(select id,Name,Manager1__c,Vendor__c from Transactions__r where Vendor__c=:Vid and Manager1__c=null ) from Vendor__c ];

You must log in to answer this question.

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