0

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. How to resolve this?

APex ClasS:

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;               
    }
}

Test Class

@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);

  }



}
  • 2
    I would recommend you push all your test classes first to the production and then push the remain apex classes/controllers. Also remember to fill any data in production which is included as part of your tests, like custom settings, labels etc. – codeinprogress Jun 8 '15 at 16:44
  • @codeinprogress 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. – SFDC_BigDog Jun 8 '15 at 19:00
  • 1
    Query for your ID. Id myGroupId = [SELECT Id from Group WHERE DeveloperName = 'your_group_name' LIMIT 1].Id. Then use :myGroup.Id in your query for GroupMember – frontendloader Jun 8 '15 at 19:31
  • Hey @frontendloader ... Sorry I have not understood. Could you please include that in my code. It will help me a lot in solving this. – SFDC_BigDog Jun 8 '15 at 19:36
  • also - the email templateId is hard-coded - this can create deployment issues as well – cropredy Jun 8 '15 at 19:53
0

This apex class is getting 100% code coverage but you say on deployment, code coverage is at 53%?


Is it possible that the rest of your code in Sandbox accumulates to 53% code coverage when averaged together? On deployment, Salesforce will look at total coverage, not just the code you are deploying.

0

I did used custom labels to resolve this. Here is the following code changes in the Class.

global class memberApproval {

webservice static boolean callApproval(Id localId) { 
            set<Id> Uid = new set<Id>();
            String grpId = Label.GroupId;
     List<GroupMember> Lst =[Select Id, UserOrGroupId From GroupMember Where GroupId =: grpId];
     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  );
        String templateIdVal= Label.TemplateId;
           mail.setTemplateId(templateIdVal);
           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;               
}

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.