+ Start a Discussion
aks001aks001 

Trigger: update child object field

hi,

 

I want to update a child object field when if opportunity line item 's quantity gets updated and that object which gonna be informed that the opportunity is got updated .... is the child to opportunity . so for that I'm writting a trigger and getting confused in fetching the old value of that field..... to match .in the if condition....

 

public void statusUpdate(List<opportunity> newOpps, List<opportunity> oldOpps){
        List<String> oppIds = new List<String>();
        List<opportunityLineItem> opportunityLineItemsNew = new List<opportunityLineItem>();
        List<production__c> productionTickets = new List<production__c>();
        for(opportunity oppNw : newOpps){
            oppids.add(oppNw.id);
        }
        
        opportunityLineItemsNew = [Select id, opportunityId, quantity, StartDate__c, EndDate__c, CostType__c From opportunityLineItem where opportunityID in : oppids];
        productionTickets = [select id, name, Opportunity__c, OpportunityStatus__c from production__c where Opportunity__c in : oppids];
        
        for(Production__c pt : productionTickets){
            for(opportunityLineItem oli : opportunityLIneItemsNew){
                if(oli.opportunityId == pt.opportunity__c){
                    if(oli.quantity != ?????? ){
                   
                    }
                }
            }
        }
    }

 

so plz tell me if you have any of the solutions.... thanks in advance........................... :)

souvik9086souvik9086

Where is your trigger?

 

Create a trigger on OppLineItem and check like the following

 

for( OpportunityLineItem oppty : Trigger.new ) {
if( oppty.Quantity != trigger.oldMap.get(oppty.Id).Quantity) {

//Your code
}
}

aks001aks001

hi,

 

I'm really sorry I forgot to tell that I'm using the trigger framework... and the code that i provided is in my handler class so the newList and oldList that you saw in ... as parameters , is working as trigger.new and trgger.old............... now the only issue is .. when ... an opportunity has multiple line items ... then this code is working happly and fine.... but if it has ... a single oppLineItem

it's not showing the results.... now m assuming and hoping that you got my point..... so plz if you have any answers or solution....then plz reply me....  thanks.. for your .. consideration.... :)

Prashant TiwariPrashant Tiwari
Pass trigger.newmap and trigger.oldmap to the handler class instead of lists and iterate through trigger.newmap for IDs of opportunities and inside this for loop put a condition in IF As :
for(ID Opp_ID : trigger.newmap.keyset()){
IF(trigger.newmap.get(Opp_ID).Quantity !=trigger.OldMap.get(Opp_ID).Quantity){

// your code for update etc...

}
}


Thanks,
Prashant Tiwari
aks001aks001

Yes this approach is best  when my trigger is based on opportunity fields ... but right now m creating it on opportunity but it's depends on opportunity line Item's... fields.... like quantity....

thanks....