You need to sign in to do that
Don't have an account?
SFDC_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); } }
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.
@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.
hello Sumit,
I have created Custom label. But how to call them in apex class. It is showing error. I am call this way.
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
@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.