function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
aparna d 1aparna d 1 

populates values from parent to child

I have a requirement while inserting and updating of information records ,fetch values from opportunity and populate on Information object . opportunity is a lookup field on Information object.I have tried below code using triggers.  please tell me  where did i mistake.Its very urgent. Thanks in advance.

public static void TMOppCompInfo(List<Competitive_Infomation__c> complst)
    {
        
        List<Infomation__c> lstcompinfo = new List<Infomation__c>();
        set<id> oppid = new set<id>();
        Id TMoppRecord = Schema.SobjectType.Opportunity.getRecordTypeInfosByName().get('machinery').getRecordTypeId();
        Id CloseOut = Schema.SobjectType.Competitive_Infomation__c.getRecordTypeInfosByName().get(' Information - Close Out').getRecordTypeId();
        for(Competitive_Infomation__c comp : complst)
        {
            oppid.add(comp.Sub_Opportunity_Name__c);
         }
       
       List<opportunity> lstoppcomp = [select id,Emissions_Unit__c,Emissions_Guaranteed__c,Reason_for_Guarantee__c,Guaranteed_NOx_15_O2_ppm__c,Liquid_NOx_15_O2_ppm__c,
                                        Guaranteed_CO_15_O2_ppm__c,Liquid_CO_15_O2_ppm__c,CBR_Temperature_Range_Unit__c,Temperature_Range__c,
                                       Load_Range_pct__c,CBR_Engine_Emissions_Controls__c from opportunity where id =: oppid and recordType.Name = 'machinery' ];
      IF(lstoppcomp.size()>0)
     {
        for(Competitive_Infomation__c compinfo : complst)
         {
          
           if(compinfo.Emissions_Same_as_Solar__c == 'same' && compinfo.RecordTypeId == CloseOut)
           {
               for(opportunity opplst : lstoppcomp)
              {
              
                  compinfo.NOx_Emissions_15_O2__c = opplst.Guaranteed_NOx_15_O2_ppm__c;
                  compinfo.CO_Emissions_15_O2__c = opplst.Guaranteed_CO_15_O2_ppm__c;
                  compinfo.Liquid_NOx_Emissions_15_O2__c = opplst.Liquid_NOx_15_O2_ppm__c;
                 
               
           }
         }
       }
Best Answer chosen by aparna d 1
Avinash GangadhareAvinash Gangadhare
Hi @Aparna - If you use map instead of list to store related opportunity detail it will help to fetch its detail easily.. try below code.. if it works mark question solved.

public static void TMOppCompInfo(List<Competitive_Infomation__c> complst)
    {
        
        List<Infomation__c> lstcompinfo = new List<Infomation__c>();
        set<id> oppid = new set<id>();
        Id TMoppRecord = Schema.SobjectType.Opportunity.getRecordTypeInfosByName().get('machinery').getRecordTypeId();
        Id CloseOut = Schema.SobjectType.Competitive_Infomation__c.getRecordTypeInfosByName().get(' Information - Close Out').getRecordTypeId();
        for(Competitive_Infomation__c comp : complst)
        {
            oppid.add(comp.Sub_Opportunity_Name__c);
         }
       
       Map<Id,opportunity> mapOppcomp = new Map<Id, Opportunity> ([select id,Emissions_Unit__c,Emissions_Guaranteed__c,Reason_for_Guarantee__c,Guaranteed_NOx_15_O2_ppm__c,Liquid_NOx_15_O2_ppm__c,
                                        Guaranteed_CO_15_O2_ppm__c,Liquid_CO_15_O2_ppm__c,CBR_Temperature_Range_Unit__c,Temperature_Range__c,
                                       Load_Range_pct__c,CBR_Engine_Emissions_Controls__c from opportunity where id =: oppid and recordType.Name = 'machinery' ]);
      IF(mapOppcomp.size()>0)
     {
        for(Competitive_Infomation__c compinfo : complst)
         {
          
           if(compinfo.Emissions_Same_as_Solar__c == 'same' && compinfo.RecordTypeId == CloseOut)
           {
                  Opportunity temp = mapOppcomp.get(compinfo.Sub_Opportunity_Name__c);
                  compinfo.NOx_Emissions_15_O2__c = temp.Guaranteed_NOx_15_O2_ppm__c;
                  compinfo.CO_Emissions_15_O2__c = temp.Guaranteed_CO_15_O2_ppm__c;
                  compinfo.Liquid_NOx_Emissions_15_O2__c = temp.Liquid_NOx_15_O2_ppm__c;
                 
         }
       }
}

Thanks :)

All Answers

Avinash GangadhareAvinash Gangadhare
Hi @Aparna - If you use map instead of list to store related opportunity detail it will help to fetch its detail easily.. try below code.. if it works mark question solved.

public static void TMOppCompInfo(List<Competitive_Infomation__c> complst)
    {
        
        List<Infomation__c> lstcompinfo = new List<Infomation__c>();
        set<id> oppid = new set<id>();
        Id TMoppRecord = Schema.SobjectType.Opportunity.getRecordTypeInfosByName().get('machinery').getRecordTypeId();
        Id CloseOut = Schema.SobjectType.Competitive_Infomation__c.getRecordTypeInfosByName().get(' Information - Close Out').getRecordTypeId();
        for(Competitive_Infomation__c comp : complst)
        {
            oppid.add(comp.Sub_Opportunity_Name__c);
         }
       
       Map<Id,opportunity> mapOppcomp = new Map<Id, Opportunity> ([select id,Emissions_Unit__c,Emissions_Guaranteed__c,Reason_for_Guarantee__c,Guaranteed_NOx_15_O2_ppm__c,Liquid_NOx_15_O2_ppm__c,
                                        Guaranteed_CO_15_O2_ppm__c,Liquid_CO_15_O2_ppm__c,CBR_Temperature_Range_Unit__c,Temperature_Range__c,
                                       Load_Range_pct__c,CBR_Engine_Emissions_Controls__c from opportunity where id =: oppid and recordType.Name = 'machinery' ]);
      IF(mapOppcomp.size()>0)
     {
        for(Competitive_Infomation__c compinfo : complst)
         {
          
           if(compinfo.Emissions_Same_as_Solar__c == 'same' && compinfo.RecordTypeId == CloseOut)
           {
                  Opportunity temp = mapOppcomp.get(compinfo.Sub_Opportunity_Name__c);
                  compinfo.NOx_Emissions_15_O2__c = temp.Guaranteed_NOx_15_O2_ppm__c;
                  compinfo.CO_Emissions_15_O2__c = temp.Guaranteed_CO_15_O2_ppm__c;
                  compinfo.Liquid_NOx_Emissions_15_O2__c = temp.Liquid_NOx_15_O2_ppm__c;
                 
         }
       }
}

Thanks :)
This was selected as the best answer
aparna d 1aparna d 1
Hi Avinash,
Thanks a lot . Its working fine thank you!