+ Start a Discussion
Amit Kr SinghAmit Kr Singh 

How to get child object record in parent object using trigger

In my current scenario there is three object

1 Quote
2 Sales_order  (here in Quote is looup)
3 Sales_order_line_item (here Quote is lookup and Salesorder is master)

in Sales_order_line_item object there is one field name  Import_Lending_Price__c that i want to display on Quote Object .in Quote Object  Filed i have created Lending_Price__c  this price should Sales_order_line_item object Import_Lending_Price__c 


Please check my trigger and tell me is it correct or not


trigger ParentQuoteupdate on Sales_Order_Line_Item__c (after insert, after update,before insert) {

  

Map<ID, Quote> parentQuote = new Map<ID, Quote>(); 

List<Id> listIds = new List<Id>();

  for (Sales_Order_Line_Item__c childObj : Trigger.new) 
  {
    listIds.add(childObj.Quote);
  }
  
  parentQuote = new Map<Id, Quote> ([SELECT ID,(SELECT ID,Quote__c,Import_Landing_Price__c FROM Sales_Order_Line_Item__r) FROM Quote WHERE ID IN :listIds]);
  
  for (Sales_Order_Line_Item__c  SOLI: Trigger:new){
     Quote myParentQuote = parentQuote.get(SOLI.Quote__c);
     myParentQuote.Landing_Price__c = SOLI.Import_Landing_Price__c ;
  }
  
  
  update parentQuote.values(); 


}


Error:- I am getting
Error: Compile Error: Didn't understand relationship 'Sales_Order_Line_Item__r' in FROM part of query call. If you are attempting to use a custom relationship, be sure to append the '__r' after the custom relationship name. Please reference your WSDL or the describe call for the appropriate names. at line 14 column 37

Please help me 

Thanks in advance
Mahesh DMahesh D
Hi Amit,

Make this (listIds) as a set of ids. and change the query like below:

parentQuote = new Map<Id, Quote> ([SELECT ID, Name,  Landing_Price__c FROM Quote WHERE ID IN :listIds]);

Regards,
Mahesh
Omveer kundu 8Omveer kundu 8
           Sales_Order_Line_Item__r  replace r with c
Amit Kr SinghAmit Kr Singh
Hi Mahesh D

after replacing my code with your codethen getting 
Error: Compile Error: unexpected token: 'Trigger' at line 14 column 39
Mahesh DMahesh D
hi Amit,

Please use below code:

Trigger ParentQuoteupdate on Sales_Order_Line_Item__c (after insert, after update) {

    Map<ID, Quote> parentQuoteMap = new Map<ID, Quote>(); 
    Set<Id> quoteIdSet = new Set<Id>();

    for (Sales_Order_Line_Item__c soli : Trigger.new) {
        quoteIdSet.add(sol.Quote__c);
    }
  
    parentQuoteMap = new Map<Id, Quote> ([SELECT ID, Landing_Price__c FROM Quote WHERE ID IN :quoteIdSet]);
  
    for (Sales_Order_Line_Item__c  soli: Trigger:new){
        Quote myParentQuote = parentQuote.get(soli.Quote__c);
        myParentQuote.Landing_Price__c = soli.Import_Landing_Price__c ;
    }
    update parentQuoteMap.values(); 
}
 
Amit Kr SinghAmit Kr Singh
Hi Mahesh D
Again i am getting Error
Error: Compile Error: unexpected token: 'Trigger' at line 12 column 40
Please help me out
Mahesh DMahesh D
Hi Amit,

Please try the below code:
 
Trigger ParentQuoteupdate on Sales_Order_Line_Item__c (after insert, after update) {

    Map<ID, Quote> parentQuoteMap = new Map<ID, Quote>(); 
    Set<Id> quoteIdSet = new Set<Id>();

    for (Sales_Order_Line_Item__c soli : Trigger.new) {
        quoteIdSet.add(sol.Quote__c);
    }
  
    parentQuoteMap = new Map<Id, Quote> ([SELECT ID, Landing_Price__c FROM Quote WHERE ID IN :quoteIdSet]);
  
    for (Sales_Order_Line_Item__c  soli: Trigger.new){
        Quote myParentQuote = parentQuote.get(soli.Quote__c);
        myParentQuote.Landing_Price__c = soli.Import_Landing_Price__c ;
    }
    update parentQuoteMap.values(); 
}

Regards,
Mahesh