Saturday, June 13, 2015

How to Mass Update Salesforce profiles

when coming to deploy process, I think many people feel it is a big headache for changing all kinds of accessibility of objects fields, records type, Visualforce page because most of these are needed to be done manually. If the organization is not big, it might not be a so big thing, just click several buttons repeatedly. But how about if an org has hundreds of profiles?

Currently, SF provide deploy profiles by change set. But it might a little difficult to make sure that it only updates what you want to change.  This blog will detail how to update profiles by SF Migration API tool. And you will feel very safe as it will only update what you want to change.

This blog will describe the detail steps of how to deploy record type visibility as an example.

1. Download and install Migration tool:
Please following this document to download and install this tool.  Please make sure delete example files under several folders. 

2. retrieve profile metadata
For example if you want to change several user profiles' visibility of all objects' record types. 
First modify the package.xml file in unpackaged folder like following:
package.xml
<?xml version="1.0" encoding="UTF-8"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
     <types>
        <members>*</members>
        <name>RecordType</name>
    </types>
    <types>
        <members>*</members>
        <name>Profile</name>
    </types>
    <version>31.0</version>
</Package>

First you could retrieve these information by using following CMD:
ant retrieveUnpackaged
After successfully finished, you should get all these meta data in retrieveUnpackaged folder (You can modify the target folder by modifying build.xml file).  In this folder, it will generate a file for every profile, like following:

The tool retrieves recordTypeVisibilities and other information of all profiles as shown blow. Each profile has a separate file to store these information. Each file will like following: 
<?xml version="1.0" encoding="UTF-8"?>
<Profile xmlns="http://soap.sforce.com/2006/04/metadata">
    <custom>true</custom>
    <loginHours>...</loginHours>
    <recordTypeVisibilities>
        <default>false</default>
        <recordType>Account.Business</recordType>
        <visible>true</visible>
    </recordTypeVisibilities>

     ...
    <userPermissions>
        <enabled>true</enabled>
        <name>EditMyDashboards</name>
    </userPermissions>
    ...
</Profile>

3. modify profile meta data file
As the second step retrieved more information than we want to change, we need to modify these files before deploy to production. Delete all other properties, only include the recordtypevisibilities elements of the record types you need to modify. For example, If deploy following file (Admin.profile) to production, it will modify the visibility of Campaign.Advocacy record type to be true for Admin profile 
<?xml version="1.0" encoding="UTF-8"?>
<Profile xmlns="http://soap.sforce.com/2006/04/metadata">
    <recordTypeVisibilities>
        <default>false</default>
        <recordType>Campaign.Advocacy</recordType>
        <visible>true</visible>
    </recordTypeVisibilities> 
</Profile>

4. Deploy the changes
use ant deployUnpackaged to deploy all those files modified in previous step.

You can use similar method to manually deploy others:
- apex page / visualforce page accessibility. sample package.xml file for this:

- Apex class
- CustomObject accessibility
- CustomField accessibility
- etc.


Following package.xml will retrieve all metadata about custom object, field, record type accessibility:
After modifying the retrieved profiles, you can deploy to production. 

3 comments: