You need to sign in to do that
Don't have an account?
JosherPack
SandboxPostScript - Help with Test
Use Case: I have users that need to be admins in the sandbox, but not in production. I have written a SandboxPostCopy apex class to:
My class and test are below. The test passes, but shows 0% code coverage. As I am new to apex development, any help with this will be greatly appreciated.
global class AfterSandboxRefresh implements SandboxPostCopy {
global void runApexClass(SandboxContext context) {
//Pull System Administrator Profile
Profile p = [SELECT Id FROM Profile WHERE Name='System Administrator'];
//Pull Users to Make Sandbox Admin
List<User> listOfUsers = new List<User>();
List<User> UsersToUpdate = [SELECT Id,Email, FederationIdentifier
FROM User
WHERE Make_Sandbox_Admin__c = True];
For (User u : UsersToUpdate) {
u.ProfileId = p.Id;
u.isActive = True;
String e1 = u.email;
String e2 = e1.remove('@example.com');
String target = '=';
String replacement = '@';
String e3 = e2.replace(target, replacement);
u.Email = e3;
listOfUsers.add(u);
}
Update(listOfUsers);
}
}
@isTest
class AfterSandboxRefreshTest
{
@isTest
static void testSandboxPostCopyScript()
{
//Pull System Administrator Profile
Profile p = [SELECT Id FROM Profile WHERE Name='System Administrator'];
//Pull Users to Make Sandbox Admin
List<User> listOfUsers = new List<User>();
List<User> UsersToUpdate = [SELECT Id,Email, FederationIdentifier
FROM User
WHERE Make_Sandbox_Admin__c = True];
For (User u : UsersToUpdate) {
u.ProfileId = p.Id;
u.isActive = True;
String e1 = u.email;
String e2 = e1.remove('@example.com');
String target = '=';
String replacement = '@';
String e3 = e2.replace(target, replacement);
u.Email = e3;
listOfUsers.add(u);
}
Update(listOfUsers);
Test.startTest();
// Using any Ids as orgId and sandboxId for test, e.g. Account Ids
// Id possible pass valid id
Test.testSandboxPostCopyScript(
new SandboxRefresh(),
'00Dj0000001srun',
'00Dm00000008k1D',
'MySandboxName'
);
Test.stopTest();
}
}
- Make them active in the sandbox
- Update their Profile to System Administrator
- Change their Sandbox email to their real email
My class and test are below. The test passes, but shows 0% code coverage. As I am new to apex development, any help with this will be greatly appreciated.
global class AfterSandboxRefresh implements SandboxPostCopy {
global void runApexClass(SandboxContext context) {
//Pull System Administrator Profile
Profile p = [SELECT Id FROM Profile WHERE Name='System Administrator'];
//Pull Users to Make Sandbox Admin
List<User> listOfUsers = new List<User>();
List<User> UsersToUpdate = [SELECT Id,Email, FederationIdentifier
FROM User
WHERE Make_Sandbox_Admin__c = True];
For (User u : UsersToUpdate) {
u.ProfileId = p.Id;
u.isActive = True;
String e1 = u.email;
String e2 = e1.remove('@example.com');
String target = '=';
String replacement = '@';
String e3 = e2.replace(target, replacement);
u.Email = e3;
listOfUsers.add(u);
}
Update(listOfUsers);
}
}
@isTest
class AfterSandboxRefreshTest
{
@isTest
static void testSandboxPostCopyScript()
{
//Pull System Administrator Profile
Profile p = [SELECT Id FROM Profile WHERE Name='System Administrator'];
//Pull Users to Make Sandbox Admin
List<User> listOfUsers = new List<User>();
List<User> UsersToUpdate = [SELECT Id,Email, FederationIdentifier
FROM User
WHERE Make_Sandbox_Admin__c = True];
For (User u : UsersToUpdate) {
u.ProfileId = p.Id;
u.isActive = True;
String e1 = u.email;
String e2 = e1.remove('@example.com');
String target = '=';
String replacement = '@';
String e3 = e2.replace(target, replacement);
u.Email = e3;
listOfUsers.add(u);
}
Update(listOfUsers);
Test.startTest();
// Using any Ids as orgId and sandboxId for test, e.g. Account Ids
// Id possible pass valid id
Test.testSandboxPostCopyScript(
new SandboxRefresh(),
'00Dj0000001srun',
'00Dm00000008k1D',
'MySandboxName'
);
Test.stopTest();
}
}
1) https://developer.salesforce.com/forums/?id=906F0000000kAQfIAM
Please try below code
Let us know if this will help you
Thanks
Amit Chadhary
All Answers
1) https://developer.salesforce.com/forums/?id=906F0000000kAQfIAM
Please try below code
Let us know if this will help you
Thanks
Amit Chadhary