+ Start a Discussion
Developer99Developer99 

Update the Child Fild when ever i changed the parent field

HI,

        I am working with quotes and quote-line items  here quote-line items is the child object and quote is parent both having master-detail relation ship,
  
         when i update the tax field in quote.. in quote line item i have a field update that must be changed to false can any one suggest me how to write the trigger for this task.
Phillip SouthernPhillip Southern
Hi, just to confirm....since you are trying to reach up into the parent for data, have you considered using forumla fields?
Ramu_SFDCRamu_SFDC
You may try the below code. Here abc__c is the checkbox field in quote line item object.

trigger quotetax on Quote (after update) {   
    set<id> ids=new set<id>();
    for(quote q:trigger.new){
         ids.add(q.id);    
    }
    map<id,quote> oldquote=new map<id,quote>();
    for(quote qold:trigger.old){
      oldquote.put(qold.id,qold);
    }
    map<id,list<quotelineitem>> qlimap=new map<id,list<quotelineitem>>();
    list<quotelineitem> qli=[select id,abc__c,quoteid from quotelineitem where quoteid=:ids];
    for(quotelineitem qli2:qli){
        qlimap.put(qli2.quoteid,qli);      
    }
    system.debug('qlimap size = '+qlimap.size());
      system.debug('qlimap CONTENTS = '+qlimap);
    list<quotelineitem> qli4=new list<quotelineitem>();
    for(quote q2:trigger.new){
        quote b=oldquote.get(q2.id);
        decimal c=b.tax;
       if(q2.Tax!=c){
           system.debug('Tax value Changed');
         list<quotelineitem> qli3=qlimap.get(q2.Id);          
           for(quotelineitem qli5:qli3){
              qli5.abc__c=true;
               qli4.add(qli5);
           }
       } 
        else{
            system.debug('Tax value NOT changed');
        }
    }
    system.debug('size of quote line items = '+qli4.size());
    update qli4;
   }

Mark this answer as the best answer if it resolved your requirement.
Developer99Developer99
@Ramu,

                 IN Parent I have the fields city__c,Street__c,tax__c may i know where can i include city-_c,Street__c..could you update your trigger.
Ramu_SFDCRamu_SFDC
Are you checking for changes on these fields? if yes, then you need to add the condition on the line 'if(q2.Tax!=c)' . Change this line as
if(q2.Tax!=c || q2.city__c!=b.city__c || q2.street__c!=b.street__c). Hope this helps