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
saurabh kumar 11saurabh kumar 11 

SanboxPostCopy Script not working on Sandbox Refresh/new Creation

Hello,
 I am using new  interface SandboxPostCopy which is newly released in spring 16 , and write some script with dml operation  AND  email sending code  in method  called   runApexClass
 After that i added that class Name  in   Sandbox Template Apex class   which suppose to run  on sandbox after sandbox created/refreshed 
 See the release link -> https://releasenotes.docs.salesforce.com/en-us/spring16/release-notes/rn_deployment_sandbox_postcopy_script.htm

 Below is my code , but it is not doing any change in sandbox which being created/refreshed  while i checked using test class the code is having 100% coverage with no error . Please let me know if any body has any solution 

global  without Sharing class SandBoxAfterRefreshHandler implements SandboxPostCopy { 
  //Method which Run Post-Copy Script for Sandboxes
    global void runApexClass(SandboxContext context){
    
     System.debug('Hello  ' + context.organizationId() + 
           + context.sandboxId() + context.sandboxName());    
      String Status = '';
      try{
      List<User> Ulist  =  [Select id from User where name='Test' and IsActive = True];
      List<RecordType> rtlist = [Select id from RecordType where SObjectType = 'Account' And DeveloperName = 'Test' And isActive = True];
      Account acc = new Account(Name ='TestSandbox'); 
      if(!Ulist.isEmpty()){
      acc.OwnerId = Ulist[0].id;
      }
      if(!rtlist.isEmpty())
      acc.RecordTypeId = rtlist[0].id;
      
      insert acc;
      system.debug('acc.id');
      Status = acc.id;
      }
      catch(Exception de){
        Status = 'fail'+de.getMessage();
        system.debug('insert account failed' + Status);
        
      }

//sending email 
      messaging.Singleemailmessage mail = new  Messaging.SingleEmailMessage();
                    
                String[] toAddresses = new String[] {'ANY EMAIL'}; 

                mail.setToAddresses(toAddresses);
                mail.setSenderDisplayName('Coming from Refreshed Sandbox');
                mail.setSubject('Coming from Refreshed Sandbox');
                mail.setBccSender(false);  
                mail.setUseSignature(false);
                mail.setPlainTextBody('Hello Saurabh ' + context.organizationId() + 
      '' + context.sandboxId() + context.sandboxName()+'Current Sandbox Organization Id '+ Userinfo.getOrganizationId() + ' Current sandbox Name '+ UserInfo.getOrganizationName() +' and DML Status '+ Status  );
                
                List<Messaging.SendEmailResult> sendEmailResults =  new List<Messaging.SendEmailResult>{};   
                // Send the email you have created.
                try{
                sendEmailResults = new list<Messaging.SendEmailResult>() ;
                Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail }, false);
                
                }
                catch(Exception e)
                {
                    
                
                }

}
}
pconpcon
I belive your issue is that the deliverability for emails in sandboxes is default to "System email only"
 
New and refreshed sandboxes have the default email deliverability setting System email only. To configure email deliverability [1]

As for you Account insert, is Name the only required field for you Account object?  If you run the same code in the developer console of a freshly created sandbox does it work?  If not, what do you get from your debug logs?

[1] https://help.salesforce.com/HTViewHelpDoc?id=data_sandbox_implementation_tips.htm#EmailAdmin
saurabh kumar 11saurabh kumar 11
Hi @pcon. Yes when i run that code directly on newly created. Sandbox it's executed correctly 
SunilKumarSunilKumar
Hi How can we execute the above script from developer console

Please let me know