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
shakila Gshakila G 

Apex Code deployment Error

Hi All,

I have written a trigger to update both opportunity and cumulative object.
My logic is working fine for the same I have written a test class. My code coverage is 81 % in Sandbox when I go for deployment in production ...during the validation am getting following error. Kindly let me know How to overcome from the below error.

User-added image
My trigger:

trigger UpdateAmountInWords on Opportunity (Before Update) {
List<Target__C> Tglist= New List<Target__C>();
List<Target__C> Tglist1= New List<Target__C>();
Map<String,ID> Mapfield = New map<String,ID>();
Map<String,ID> MapTele = New map<String,ID>();
List<Incentive__c >lstInc =New list<Incentive__c >();
List<Incentive__c >lstInc1 =New list<Incentive__c >();
List <Cumulative__c> Cumlist= New list <Cumulative__c>();
List <Cumulative__c> Cumlist1= New list <Cumulative__c>();
list<Cumulative__c > lstcum = new list<Cumulative__c >(); 
list<Opportunity > opplist = new list<Opportunity >(); 
 Utils u = new Utils();
       For(Opportunity opp:Trigger.New){ 
       // Update opporunity Amount In Words
        opp.Amount_To_Words_Total_Amount_With_Tax__c = u.convert(opp.Total_Amount_With_IGST__c.longValue())+' Only'; 
        opp.Amount_To_Words_Total_Tax_Amount__c = u.convert(opp.Total_Tax_Amount__c.longValue())+' Only'; 
        //            
       Mapfield .put(opp.Field_Executive__c,opp.ID);MapTele.put(opp.Hidden_Owner_Name__c,opp.ID);}
       
TGlist1=[select ID, name from target__C where Name in:Mapfield.keyset() and Active__c=True];           
TGlist=[select ID, name from target__C where Name in:MapTele.keyset() and  Active__c=True];
            for(Opportunity opp: trigger.new ) { 
                    if(TGlist1.size()>0){if(opp.Field_Executive__c==TGlist1[0].NAme){
                                   opp.Target__C= TGlist1[0].ID; } }
                    IF(TGlist.size()>0){if(opp.Hidden_Owner_Name__c==TGlist[0].NAme){   
                                   opp.Telecaller__c=TGlist[0].ID;}}         
            If(opp.Telecaller__c!=Null && opp.Cumulative_Updated__c==False && opp.Work_Order_Created_Date__c!=Null ){
              Cumlist=[select ID,Target__c,Type1_Customer_Order_value_MPD__c,Type1_Customer_Order_Value_PD__c,Type2_Customer_Order_value_MPD__c,Type2_Customer_Order_value_PD__c,Type3_Customer_Order_value_MPD__c,Type3_Customer_Order_value_PD__c,Type4_Customer_Order_value_MPD__c,Type4_Customer_Order_value_PD__c,From_Date__c,To_Date__c from Cumulative__c where Target__c=:opp.Telecaller__c and  From_Date__c<=:opp.Work_Order_Created_Date__c and To_Date__c>=:opp.Work_Order_Created_Date__c];
                   if(Cumlist.size()>0){For(Cumulative__c Cum:Cumlist){lstInc = [select ID from Incentive__c where ID =:Opp.IncentiveSlab__c and Opp_Discount_To__c>=:opp.Discount__c]; 
                        If(lstInc.size()>0 ){If(Opp.Customer_Slab_Type__c=='Type 4'){           
                                Cum.Type4_Customer_Order_value_PD__c+= opp.Individual_Order_value__c;opp.Cumulative_Updated__c=True;
                             }Else IF(Opp.Customer_Slab_Type__c=='Type 2'){Cum.Type2_Customer_Order_value_PD__c+= opp.Individual_Order_value__c; opp.Cumulative_Updated__c=True;
                             }Else IF(Opp.Customer_Slab_Type__c=='Type 3'){Cum.Type3_Customer_Order_value_PD__c+= opp.Individual_Order_value__c;opp.Cumulative_Updated__c=True;
                             }Else IF(Opp.Customer_Slab_Type__c=='Type 1'){Cum.Type1_Customer_Order_Value_PD__c+= opp.Individual_Order_value__c;opp.Cumulative_Updated__c=True;
                             }} IF(lstInc.size()<=0 ){If(Opp.Customer_Slab_Type__c=='Type 4'){Cum.Type4_Customer_Order_value_MPD__c+= opp.Individual_Order_value__c;opp.Cumulative_Updated__c=True;
                            }Else IF(Opp.Customer_Slab_Type__c=='Type 2'){Cum.Type2_Customer_Order_value_MPD__c+= opp.Individual_Order_value__c;opp.Cumulative_Updated__c=True;
                            }Else IF(Opp.Customer_Slab_Type__c=='Type 3'){Cum.Type3_Customer_Order_value_MPD__c += opp.Individual_Order_value__c;opp.Cumulative_Updated__c=True;
                            } Else IF(Opp.Customer_Slab_Type__c=='Type 1'){Cum.Type1_Customer_Order_value_MPD__c+= opp.Individual_Order_value__c;opp.Cumulative_Updated__c=True;
                            } }lstcum .add(Cum);opplist.add(opp);} } } 
         If(opp.Target__C!=Null && opp.FE_Cumulative_Updated__c==False && opp.Work_Order_Created_Date__c!=Null ){               
              Cumlist1=[select ID,Target__c,Type1_Customer_Order_value_MPD__c,Type1_Customer_Order_Value_PD__c,Type2_Customer_Order_value_MPD__c,Type2_Customer_Order_value_PD__c,Type3_Customer_Order_value_MPD__c,Type3_Customer_Order_value_PD__c ,Type4_Customer_Order_value_MPD__c,Type4_Customer_Order_value_PD__c,From_Date__c,To_Date__c from Cumulative__c where Target__c=:opp.Target__C and From_Date__c<=:opp.Work_Order_Created_Date__c and To_Date__c>=:opp.Work_Order_Created_Date__c];
               if(Cumlist1.size()>0){For(Cumulative__c Cum:Cumlist1){ 
                       lstInc1 = [select ID from Incentive__c where ID =:Opp.IncentiveSlab__c and Opp_Discount_To__c>=:opp.Discount__c]; 
                        If(lstInc1.size()>0 ){If(Opp.Customer_Slab_Type__c=='Type 4'){ Cum.Type4_Customer_Order_value_PD__c+= opp.Individual_Order_value__c;opp.FE_Cumulative_Updated__c=True;
                             }Else IF(Opp.Customer_Slab_Type__c=='Type 2'){Cum.Type2_Customer_Order_value_PD__c+= opp.Individual_Order_value__c;opp.FE_Cumulative_Updated__c=True;
                             }Else IF(Opp.Customer_Slab_Type__c=='Type 3'){Cum.Type3_Customer_Order_value_PD__c += opp.Individual_Order_value__c; opp.FE_Cumulative_Updated__c=True;
                             }Else IF(Opp.Customer_Slab_Type__c=='Type 1'){Cum.Type1_Customer_Order_Value_PD__c+= opp.Individual_Order_value__c;   opp.FE_Cumulative_Updated__c=True;
                             }} IF(lstInc1.size()<=0 ){ If(Opp.Customer_Slab_Type__c=='Type 4'){Cum.Type4_Customer_Order_value_MPD__c+= opp.Individual_Order_value__c; opp.FE_Cumulative_Updated__c=True;
                        }Else IF(Opp.Customer_Slab_Type__c=='Type 2'){ Cum.Type2_Customer_Order_value_MPD__c+= opp.Individual_Order_value__c; opp.FE_Cumulative_Updated__c=True;
                        }Else IF(Opp.Customer_Slab_Type__c=='Type 3'){ Cum.Type3_Customer_Order_value_MPD__c+= opp.Individual_Order_value__c; opp.FE_Cumulative_Updated__c=True;
                        } Else IF(Opp.Customer_Slab_Type__c=='Type 1'){Cum.Type1_Customer_Order_value_MPD__c+= opp.Individual_Order_value__c;opp.FE_Cumulative_Updated__c=True;}        
                        }lstcum .add(Cum); opplist.add(opp);
                      } } } 
                      update lstcum ; }}

 
Best Answer chosen by shakila G
Niraj Kr SinghNiraj Kr Singh
Hi Shaklla,

It seems fine your code, But you can check once in Utils this class might be having SOQL which is calling here inside FOR loop as i highlighted here:

Utils u = new Utils();
    For(Opportunity opp: Trigger.New) {
        // Update opporunity Amount In Words
        opp.Amount_To_Words_Total_Amount_With_Tax__c = u.convert(opp.Total_Amount_With_IGST__c.longValue()) + ' Only';
        opp.Amount_To_Words_Total_Tax_Amount__c = u.convert(opp.Total_Tax_Amount__c.longValue()) + ' Only';
        //            
        Mapfield.put(opp.Field_Executive__c, opp.ID);
        MapTele.put(opp.Hidden_Owner_Name__c, opp.ID);
    }

OR You should check with your complete process while trigger is getting fired

And error is getting occured at line 21: you can update your code like that:

//TGlist1 = [select ID, name from target__C where Name in: Mapfield.keyset() and Active__c = True];
//TGlist = [select ID, name from target__C where Name in: MapTele.keyset() and Active__c = True];
   
Replace these two lines whith this below code make make two soql to one soql
    
Set<String> setAllNames = new Set<String>();
    setAllNames.addAll(Mapfield.keyset());
    setAllNames.addAll(MapTele.keyset());
    for(target__C objTG : [SELECT ID, name FROM target__C WHERE Name in: setAllNames AND Active__c = True]) {
        if(Mapfield.containskey(objTG.Name)){
            TGlist1.add(objTG);
        }
        if(MapTele.containskey(objTG.Name)){
            TGlist.add(objTG);
        }
    }

Thanks
Niraj
 

All Answers

Ganesh Hembram 21Ganesh Hembram 21
Hi Shakila,

As per the error your tirgger crossing the SOQL limit on line no 21
TGlist=[select ID, name from target__C where Name in:MapTele.keyset() and  Active__c=True];
Thanks & Regards,
Ganesh Hembram
shakila Gshakila G
Hi Ganesh, 
Thanks for the reply,
Can you suggest me to overcome from this issue?

 
Niraj Kr SinghNiraj Kr Singh
Hi Shaklla,

It seems fine your code, But you can check once in Utils this class might be having SOQL which is calling here inside FOR loop as i highlighted here:

Utils u = new Utils();
    For(Opportunity opp: Trigger.New) {
        // Update opporunity Amount In Words
        opp.Amount_To_Words_Total_Amount_With_Tax__c = u.convert(opp.Total_Amount_With_IGST__c.longValue()) + ' Only';
        opp.Amount_To_Words_Total_Tax_Amount__c = u.convert(opp.Total_Tax_Amount__c.longValue()) + ' Only';
        //            
        Mapfield.put(opp.Field_Executive__c, opp.ID);
        MapTele.put(opp.Hidden_Owner_Name__c, opp.ID);
    }

OR You should check with your complete process while trigger is getting fired

And error is getting occured at line 21: you can update your code like that:

//TGlist1 = [select ID, name from target__C where Name in: Mapfield.keyset() and Active__c = True];
//TGlist = [select ID, name from target__C where Name in: MapTele.keyset() and Active__c = True];
   
Replace these two lines whith this below code make make two soql to one soql
    
Set<String> setAllNames = new Set<String>();
    setAllNames.addAll(Mapfield.keyset());
    setAllNames.addAll(MapTele.keyset());
    for(target__C objTG : [SELECT ID, name FROM target__C WHERE Name in: setAllNames AND Active__c = True]) {
        if(Mapfield.containskey(objTG.Name)){
            TGlist1.add(objTG);
        }
        if(MapTele.containskey(objTG.Name)){
            TGlist.add(objTG);
        }
    }

Thanks
Niraj
 
This was selected as the best answer