• saurabh kumar 11
  • NEWBIE
  • 0 Points
  • Member since 2015

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 1
    Replies
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)
                {
                    
                
                }

}
}
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)
                {
                    
                
                }

}
}