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
Jason FungJason Fung 

Is there a way to automate the creation of custom user profile, OWD and sharing rules?

I'm working for an ISV. We have this problem that everytime we install our managed package to our new customers, we have to set up the custom user profile, OWD and sharing rules manually. I wonder if anyone knows a way to automate this process? 
Raj VakatiRaj Vakati
Hi Jason,

You can use Apex to create a User, profile, Sharing rules. See the sample code here 

User ---> User Object 
Profile --> Profile Object 
Permission Set --> Permission Set and Permission Set Assignement Objects 

 
global class PostInstallClass implements InstallHandler {
  global void onInstall(InstallContext context) {
    if(context.previousVersion() == null) {
      Account a = new Account(name='Newco');
      insert(a);
      
      Survey__c obj = new Survey__c(name='Client Satisfaction Survey');
      insert obj;
               
      User u = [Select Id, Email from User where Id =:context.installerID()];   
      String toAddress= u.Email;
      String[] toAddresses = new String[]{toAddress};
      Messaging.SingleEmailMessage mail = 
        new Messaging.SingleEmailMessage();
      mail.setToAddresses(toAddresses);
      mail.setReplyTo('support@package.dev');
      mail.setSenderDisplayName('My Package Support');
      mail.setSubject('Package install successful');
      mail.setPlainTextBody('Thanks for installing the package.');
      Messaging.sendEmail(new Messaging.Email[] { mail });
      }
    else
      if(context.previousVersion().compareTo(new Version(1,0)) == 0) {
      Survey__c obj = new Survey__c(name='Upgrading from Version 1.0');
      insert(obj);
      }
    if(context.isUpgrade()) {
      Survey__c obj = new Survey__c(name='Sample Survey during Upgrade');
      insert obj;
      }
    if(context.isPush()) {
      Survey__c obj = new Survey__c(name='Sample Survey during Push');
      insert obj;
      }
    }
  }









 
Jason FungJason Fung
Thanks for your idea Raj. But it looks a bit complicated and I'm not sure if I can implement it. Any easier way to simply export and import custom user profile, OWD and sharing rules from one org to another?