+ Start a Discussion
carmilyn.c martincarmilyn.c martin 

execution of BeforeInsert caused by: System.SObjectException: SObject row was retrieved via SOQL without querying the requested field:

I am trying to edit an existing trigger to get values of Coaching Report Object from Monthly PC Territory IC Object but i encountered an error when i tried to use it:

Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger Eisai_UpdateMonthlyWeeklyTransValuesOnFCR_Trg caused an unexpected exception, contact your administrator: Eisai_UpdateMonthlyWeeklyTransValuesOnFCR_Trg: execution of BeforeInsert caused by: System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: Monthly_PC_Territory_IC_esi__c.Aricept_Tgt_QTD_Ter_Pct_Atnment__c: Trigger.Eisai_UpdateMonthlyWeeklyTransValuesOnFCR_Trg: line 157, column 1

Here is my trigger:

trigger Eisai_UpdateMonthlyWeeklyTransValuesOnFCR_Trg on Coaching_Report_vod__c(before Insert) {
    Set<Id> userIds = New Set<Id>();
    Map<Id,Id> userTerrMap = New Map<Id,Id>();
    Map<Id,Territory> terrMap = New Map<Id,Territory>();
    Map<String,Monthly_PC_Territory_IC_esi__c> terrMonPcTerr = New Map<String,Monthly_PC_Territory_IC_esi__c>();
    Map<String,Monthly_ONC_Territory_IC_esi__c> terrMonOnTerr = New Map<String,Monthly_ONC_Territory_IC_esi__c>();
    Map<String,Monthly_PC_Territory_TRx_esi__c> terrMonPcTrxTerr = New Map<String,Monthly_PC_Territory_TRx_esi__c>();
    Map<String,Weekly_ONC_Territory_Sales_esi__c> weekONCTerrSales = New Map<String,Weekly_ONC_Territory_Sales_esi__c>();
    Map<String,Weekly_PC_Call_Averages_esi__c> weekPCCalAvg = New Map<String,Weekly_PC_Call_Averages_esi__c>();
    Map<String,Weekly_ONC_Call_Averages_esi__c> weekONCCalAvg = New Map<String,Weekly_ONC_Call_Averages_esi__c>();
    set<String> terrNames = New Set<String>();
    //Added by Cognizant on Jun 24th 2013
    Map<String,Weekly_PC_Territory_TRx_NBRx__c> terrWkPcTerr = New Map<String,Weekly_PC_Territory_TRx_NBRx__c>();
    Map<String,Weekly_PC_Territory_Banzel_TRx_NBRx__c> terrWkPcBanzlTerr = New Map<String,Weekly_PC_Territory_Banzel_TRx_NBRx__c>();
    Map<String,Monthly_MB_Territory_IC_esi__c> terrMonMbBelvqTerr = New Map<String,Monthly_MB_Territory_IC_esi__c>();
    //added by cognizant on 7/7/2014 PD
    Map<String,Weekly_EP_Territory_TRx_NBRx__c> terrWkEPTerr = New Map<String,Weekly_EP_Territory_TRx_NBRx__c>();
    
    /********************Code to update Rep Name**********************/
    if(Eisai_CaseMedicalInqGlobalVariable_Cls.isUpdateRepName)
    {
        List<Id> empIds=new List<Id>();
        Set<Id> terrIds = new Set<Id>();
        List<UserTerritory> repuserterr = New List<UserTerritory>();
        Territory terrName = New Territory();
        for(Coaching_Report_vod__c c:Trigger.New)
        {
            if(c.Employee_vod__c !=null)
                empIds.add(c.Employee_vod__c);
        } 
        empIds.add(userinfo.getUserId());  
        repuserterr = [select UserId,TerritoryId from UserTerritory where UserId in: empIds];
        Map<id,id> territoryMap = New Map<Id,Id>();
        Map<id,String> territoryIndicatorMap = New Map<Id,String>();
        for(UserTerritory ut : repuserterr)
        {
            territoryMap.put(ut.userId,ut.TerritoryId);
            terrIds.add(ut.TerritoryId);
        }
        List<Territory> terrIndicatorList = New List<Territory>();
        if(terrIds.size()>0)
        {
            terrIndicatorList = [select Id,Territory_Indicator_esi__c from Territory where id in : terrIds];
        }
        for(Territory t : terrIndicatorList)
        {
            territoryIndicatorMap.put(t.Id,t.Territory_Indicator_esi__c);
        }
        for(Coaching_Report_vod__c c:Trigger.New)
        {
            if(c.Employee_vod__c != null)
            {
                if(territoryMap.containsKey(c.Employee_vod__c))
                {
                    if(territoryIndicatorMap.containsKey(territoryMap.get(c.Employee_vod__c)))
                    {
                        if('T' != territoryIndicatorMap.get(territoryMap.get(c.Employee_vod__c)))
                            c.addError('Select valid Representative Name');
                    }
                    else
                        c.addError('Select valid Representative Name');   
                } 
                else
                    c.addError('Select valid Representative Name');        
            }
            if(c.Employee_vod__c == null)
            {
                if(territoryMap.containsKey(userinfo.getUserId()))
                {
                    if('T' == territoryIndicatorMap.get(territoryMap.get(userinfo.getUserId())))
                    {
                        c.Employee_vod__c = userinfo.getUserId();
                    }
                    else
                        c.addError('Select valid Representative Name'); 
                }
                else
                    c.addError('Territory is not defined for Current User'); 
            }
            /*if(c.Manager_vod__c == null)
            {
                if(territoryMap.containsKey(userinfo.getUserId()))
                {
                    if('D' == territoryIndicatorMap.get(territoryMap.get(c.Manager_vod__c)))
                    {
                        c.Employee_vod__c = userinfo.getUserId();
                    }
                }
            }*/
    
        }
        Eisai_CaseMedicalInqGlobalVariable_Cls.isUpdateRepName = false;
        if(terrIds.size()>0)
            terrIds.clear();
        if(empIds.size()>0)
            empIds.clear();
     }
    /*****End for updating Rep name***************/
    for(Coaching_Report_vod__c cp:Trigger.New)
    {
        system.debug('***************Collecting Employee Ids*************');
        if(cp.Employee_vod__c !=null)
            userIds.add(cp.Employee_vod__c);
    }
    if(userIds.size()>0)
        constructTerrmap(userIds);
    for(Coaching_Report_vod__c cp:Trigger.New)
    {
        if(cp.Employee_vod__c != null && userTerrMap.ContainsKey(cp.Employee_vod__c ))
        {
            system.debug('***************Updating Terriotry Number and Division fields*************');
            cp.Territory_Number_esi__c = terrMap.get(userTerrMap.get(cp.Employee_vod__c)).Name;
            cp.Division_esi__c = terrMap.get(userTerrMap.get(cp.Employee_vod__c)).Division_esi__c;
        }
    } 
    for(Coaching_Report_vod__c cp:Trigger.New)
    {           
        if(cp.Territory_Number_esi__c != null)
                terrNames.add(cp.Territory_Number_esi__c);
    }        
    if(terrNames.size()>0)
        constructMaps(terrNames);
    for(Coaching_Report_vod__c cp:Trigger.New)
    {
        system.debug('***************Mapping for Monthly PC territory values*************');
        if(cp.Territory_Number_esi__c != null && terrMonPcTerr.ContainsKey(cp.Territory_Number_esi__c))
        {
            system.debug('*Aricept_Total_QTD_Territory_Goal__c*' + terrMonPcTerr.get(cp.Territory_Number_esi__c).Aricept_Total_QTD_Territory_Goal__c);        
            /*OldCode cp.Aciphex_QTD_Territory_Goal_esi__c = terrMonPcTerr.get(cp.Territory_Number_esi__c).Aciphex_QTD_Territory_Goal__c;*/
            cp.Aricept_Total_QTD_Territory_Goal_esi__c = terrMonPcTerr.get(cp.Territory_Number_esi__c).Aricept_Total_QTD_Territory_Goal__c;
            
            /*OldCode cp.Aciphex_QTD_Territory_Sales_esi__c = terrMonPcTerr.get(cp.Territory_Number_esi__c).Aciphex_QTD_Territory_TRx__c;*/
            cp.Aricept_Total_QTD_Territory_Volume_esi__c = terrMonPcTerr.get(cp.Territory_Number_esi__c).Aricept_Total_QTD_Territory_Volume__c;
            
            /*OldCode cp.Aricept_QTD_Territory_Goal_esi__c = terrMonPcTerr.get(cp.Territory_Number_esi__c).Aricept_QTD_Territory_Goal__c;*/
            cp.Aricept_Target_QTD_Territory_Goal_esi__c = terrMonPcTerr.get(cp.Territory_Number_esi__c).Aricept_Target_QTD_Territory_Goal__c;
            
            
            /*OldCode cp.Aricept_QTD_Territory_Sales_esi__c = terrMonPcTerr.get(cp.Territory_Number_esi__c).Aricept_QTD_Territory_TRx__c;*/
            cp.Aricept_Target_QTD_Territory_Volume_esi__c = terrMonPcTerr.get(cp.Territory_Number_esi__c).Aricept_Target_QTD_Territory_Volume__c ;
            
            
            /*OldCode cp.Banzel_QTD_Territory_Goal_esi__c = terrMonPcTerr.get(cp.Territory_Number_esi__c).Banzel_QTD_Territory_Goal__c;*/
            cp.Aciphex_QTD_Territory_Goal_esi__c = terrMonPcTerr.get(cp.Territory_Number_esi__c).Aciphex_QTD_Territory_Goal__c;
            
            /*OldCode cp.Banzel_QTD_Territory_Sales_esi__c = terrMonPcTerr.get(cp.Territory_Number_esi__c).Banzel_QTD_Territory_TRx__c;*/
            cp.Aciphex_QTD_Territory_Volume_esi__c = terrMonPcTerr.get(cp.Territory_Number_esi__c).Aciphex_QTD_Territory_Volume__c;
            
            /*OldCode cp.Pct_IC_Attainment_esi__c = terrMonPcTerr.get(cp.Territory_Number_esi__c).IC_Percent_Attainment_QTD__c;*/
            /*OldCode cp.IC_Rank_esi__c = terrMonPcTerr.get(cp.Territory_Number_esi__c).IC_Rank_QTD__c;*/
            
            cp.Banzel_QTD_Territory_Attainment__c = terrMonPcTerr.get(cp.Territory_Number_esi__c).Aricept_Tgt_QTD_Ter_Pct_Atnment__c;
            cp.Banzel_QTD_Territory_Goal__c = terrMonPcTerr.get(cp.Territory_Number_esi__c).Aricept_Target_QTD_Territory_Goal__c;
            cp.Banzel_QTD_Territory_Volume__c = terrMonPcTerr.get(cp.Territory_Number_esi__c).Aricept_Target_QTD_Territory_Volume__c;
            cp.Fycompa_Prev_Qtr_Avg_Monthly_TRx__c = terrMonPcTerr.get(cp.Territory_Number_esi__c).Aricept_Total_QTD_Territory_Goal__c;
            cp.Fycompa_QTD_Avg_Monthly_TRx__c = terrMonPcTerr.get(cp.Territory_Number_esi__c).Aricept_Total_QTD_Territory_Volume__c;
            cp.Fycompa_QTD_TRx_to_Baseline__c = terrMonPcTerr.get(cp.Territory_Number_esi__c).Aricept_Tot_QTD_Ter_Pct_Attainment__c;
            cp.As_Of_Banzel_esi__c = terrMonPcTerr.get(cp.Territory_Number_esi__c).Data_As_Of__c;


NOTE: Line 157 starts here:  cp.Banzel_QTD_Territory_Attainment__c = terrMonPcTerr.get(cp.Territory_Number_esi__c).Aricept_Tgt_QTD_Ter_Pct_Atnment__c;
            cp.Banzel_QTD_Territory_Goal__c = terrMonPcTerr.get(cp.Territory_Number_esi__c).Aricept_Target_QTD_Territory_Goal__c;
            cp.Banzel_QTD_Territory_Volume__c = terrMonPcTerr.get(cp.Territory_Number_esi__c).Aricept_Target_QTD_Territory_Volume__c;
            cp.Fycompa_Prev_Qtr_Avg_Monthly_TRx__c = terrMonPcTerr.get(cp.Territory_Number_esi__c).Aricept_Total_QTD_Territory_Goal__c;
            cp.Fycompa_QTD_Avg_Monthly_TRx__c = terrMonPcTerr.get(cp.Territory_Number_esi__c).Aricept_Total_QTD_Territory_Volume__c;
            cp.Fycompa_QTD_TRx_to_Baseline__c = terrMonPcTerr.get(cp.Territory_Number_esi__c).Aricept_Tot_QTD_Ter_Pct_Attainment__c;
            cp.As_Of_Banzel_esi__c = terrMonPcTerr.get(cp.Territory_Number_esi__c).Data_As_Of__c;
Vishal_GuptaVishal_Gupta
Hi Carmilyn,

You have  a map terrMonPcTerr in which you are putting String as key and Monthly_PC_Territory_IC_esi__c as Value and then you are geting values back from that map from Monthly_PC_Territory_IC_esi__c records but when you are putting records of it in Map, you are missing to query Aricept_Tgt_QTD_Ter_Pct_Atnment__c field from object Monthly_PC_Territory_IC_esi__c  and thats why it is giving error when you are trying to get that field value.

Please add that field in your query before putting Monthly_PC_Territory_IC_esi__c  in your map terrMonPcTerr. It will resolve your issue, if still face the issue please provide the complete code of your trigger.

Thanks,
Vishal