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
SFDC_BigDogSFDC_BigDog 

Code Coverage Success in Sandbox but Error in Production

I have 100% code coverage when I tested it in the Sandbox. When I have uploaded the same in the production using changesets, I am getting code coverage error as 53%. What might be the issue. This code for a custom button to send an email for a public group. I have all those deployed in production. Now when I am trying classes, I am getting code coverage error. I have used hardcoded values in my code. Id's of template and group. But when I deployed the email template and Group in production the values change. So is that the problem? How can I do to prevent hard coding values into the code.
 
global class memberApproval {

    webservice static boolean callApproval(Id localId) { 
                set<Id> Uid = new set<Id>();
         List<GroupMember> Lst =[Select Id, UserOrGroupId From GroupMember Where GroupId = '00G19000000YJRC'];
         if(Lst.size()>0){
         for(GroupMember g:Lst){
             Uid.add(g.UserOrGroupId);
         }

         if(Uid.size()>0){
         List<User> Lstuser = [select id,email from user where Id In:Uid];
         if(Lstuser.size()>0){

         List<String> EmailIds = new List<string>();


         for(User u:Lstuser){
         EmailIds.add(u.Email);
         }
           //New instance of a single email message
             Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

            // Who you are sending the email to

               mail.setToAddresses( EmailIds  );

               // The email template ID used for the email
               mail.setTemplateId('00X19000000Dm14');
               mail.setTargetObjectId(userinfo.getuserid());      
               mail.setWhatId(localId);   
               mail.setBccSender(false);
               mail.setUseSignature(false);
               mail.setReplyTo('v.reddy@thegordiangroup.com');
               mail.setSenderDisplayName('Confirmation of Order Quote');
               mail.setSaveAsActivity(false);  

            Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
         }
         }
         }
        return true;               
    }
}
 
@isTest 

public with sharing class Test_Classnow {
  static testMethod void validateHelloWorld() {

    Group testGroup = new Group();
        testGroup.Name = 'testGroup';
        testGroup.DeveloperName = 'ABC';
        INSERT testGroup;

         Profile p = [SELECT Id FROM Profile WHERE Name='Standard User']; 
        User u = new User(Alias = 'standt', Email='standsdgser@testorg.com', 
            EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
            LocaleSidKey='en_US', ProfileId = p.Id, 
            TimeZoneSidKey='America/Los_Angeles', UserName='stangsgduser@testorg.com');

            insert u;


            GroupMember gm= new GroupMember(); 
            gm.GroupId=testGroup.id;
            gm.UserOrGroupId = u.id;
            insert gm;

            // insert opportunity and pass opp id instead of gm.id

      boolean result= memberApproval.callApproval(gm.id);
      system.assertequals(result,true);

  }



}



 
Sumitkumar_ShingaviSumitkumar_Shingavi
At this most, easiest fix I can think of is; Create 2 "Custom Labels" for Group Id and Template Id. Use then in Apex class as Label.MyLabel. Then, deploy these 2 labels and configure them in prod too and finally deploy your code. You will see 100% coverage.

Keep in mind:
1. Salesforce Ids are always instance specific except UAT and prod have same Ids if UAT is very latest refresh of prod.
2. Id's of Dev sandboxes never can be same as prod.

So, you need to keep them flexible by either using custom labels or custom settings so that you can configure environment specific values.

Hope this helps! If yes, then mark it as solution so that it can help others too.
SFDC_BigDogSFDC_BigDog

@sumitKumar_Shingavi
Hey Thanks for the reply. I did understand the point that I need to create labels. But I am clueless where to add. COuld you please add them in my code and paste it in here. That would help me a lot in solving this. Sorry for the trouble.

Thank you.

SFDC_BigDogSFDC_BigDog

hello Sumit,

 

I have created Custom label. But how to call them in apex class. It is showing error. I am call this way.

global class memberApproval {

    webservice static boolean callApproval(Id localId) { 
                set<Id> Uid = new set<Id>();
         List<GroupMember> Lst =[Select Id, UserOrGroupId From GroupMember Where GroupId = System.label.GroupId];
         if(Lst.size()>0){
         for(GroupMember g:Lst){
             Uid.add(g.UserOrGroupId);
         }

         if(Uid.size()>0){
         List<User> Lstuser = [select id,email from user where Id In:Uid];
         if(Lstuser.size()>0){

         List<String> EmailIds = new List<string>();


         for(User u:Lstuser){
         EmailIds.add(u.Email);
         }
           //New instance of a single email message
             Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

            // Who you are sending the email to

               mail.setToAddresses( EmailIds  );

               // The email template ID used for the email
               mail.setTemplateId(System.label.templateId);
               mail.setTargetObjectId(userinfo.getuserid());      
               mail.setWhatId(localId);   
               mail.setBccSender(false);
               mail.setUseSignature(false);
               mail.setReplyTo('v.reddy@thegordiangroup.com');
               mail.setSenderDisplayName('Confirmation of Order Quote');
               mail.setSaveAsActivity(false);  

            Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
         }
         }
         }
        return true;               
    }
}
kaustav goswamikaustav goswami
At Line 5 and line 29.
String grpId = Label.GroupIdLabel;
List<GroupMember> Lst =[Select Id, UserOrGroupId From GroupMember Where GroupId =: grpId];

Line 29

String templateIdVal= Label.ProdTemplateId;
mail.setTemplateId(templateIdVal);

Thanks,
Kaustav
SFDC_BigDogSFDC_BigDog
Thank you Kaustav. That worked.
SFDC_BigDogSFDC_BigDog

@sumit Kumar

I am still getting code coverage error. I have created labels and deployed in production and changed the value according to the production values. I am again getting same error.

Sumitkumar_ShingaviSumitkumar_Shingavi
What error you are getting? Can you send some details or snapshots? There might be a situation where you need to apply same to some other test classes too if they are under coverage.