Sunday, August 16, 2015

How to deploy field permission by Migration Tool

When creating a new custom field and deploying to Sandbox or production, system admin always need to manually change the field-level security. By using migration tool, we can do this automatically.

1. prepare meta data files
For each profile name, create a file PROFILE_AME.profile, the content should be like this:
Following file will modify the Lead.BusinessName__c field as read/write for system administrator profile users.

Admin.profile
<?xml version="1.0" encoding="UTF-8"?>
<Profile xmlns="http://soap.sforce.com/2006/04/metadata">
<custom>false</custom>
<fieldPermissions>
        <editable>true</editable>
        <field>Lead.BusinessName__c</field>
        <readable>true</readable>
</fieldPermissions>
</Profile>

<custom>false</custom>: means it is standard profile. If you do not specify this, the tool will consider it is a custom profile. 

If you want to modify system administrator and AE profiles, please create two similar files for each profiles.  Please note, system administrator profile file should be Admin.profile. Files name for Standard profiles might be a little different than its profile names.  

AE.profile
<?xml version="1.0" encoding="UTF-8"?>
<Profile xmlns="http://soap.sforce.com/2006/04/metadata">
<custom>true</custom>
<fieldPermissions>
        <editable>false</editable>
        <field>Lead.BusinessName__c</field>
        <readable>true</readable>
</fieldPermissions>
</Profile>
Put above two files in retrieveUnpackaged/profiles folder

2. Modify package file
modify unpackaged/package.xml and retrieveUnpackaged/package.xml file as following:

package.xml
<?xml version="1.0" encoding="UTF-8"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
    <types>
        <members>*</members>
        <name>CustomField</name>        
     </types>
     <types>
        <members>*</members>     
        <name>Profile</name>        
     </types>
    <version>31.0</version>
</Package>


3. run ant command
Open command under ant migration tool folder, type ant deployUnpackaged. The deploy process will start. You can login to the target organisation to check the status of this deployment.  Please note, when deploying this to production, the tool will run all test classes.




No comments:

Post a Comment