• Kimball Robinson
  • NEWBIE
  • 0 Points
  • Member since 2018

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 2
    Replies
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:
  1. Make them active in the sandbox
  2. Update their Profile to System Administrator
  3. Change their Sandbox email to their real email
I got the class, and the test to work, when I hard coded individuals names and emails.  I am writing this new class to do the same thing by simply checking a custom field I created on their user account called Make Sandbox Admin.

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

I am writing a chunk of code to take information from a custom object, and place it into an email when a partner portal user selects a custom button.

 

When it comes to building the body of the email, I am trying to find a way to make it look nice.

In .NET I could use stringbuilder to assemble the line breaks and feed in the variables where needed then convert to a string.

 

Is there an equivalent to that in Salesforce? 

Does anyone have an example of syntax for it?

 

Thanks,

Temple Sutfin