Deploy profile metadata changes easily with simple Python script

Moving profile changes to Salesforce environments is a difficult/irritating/troublesome task. This post helps to make developer's life easier when deploying profile changes.Let me explain you with a business scenario.

Business Scenario:
Universal Containers is using Salesforce Platform for their business automation. They have different sandboxes for development teams. They use Tortoise SVN (Subversion) for version control.  Developers check-in their code, metadata(profiles, objects, layouts etc) to SVN. As continuous integration is setup in their organisation, checking into SVN automatically deploys the changes to sandboxes.

Problem:
Universal Containers have around 90 profiles in their Salesforce environment. Developers have to update the metadata files of all profiles whenever there is a new field created which is time consuming.

Solution:
We are going to create a Python script which copies the profile permissions to all the profiles. For example, consider the below field permission have to be deployed to all the profiles.
<?xml version="1.0" encoding="UTF-8"?>
<Profile xmlns="http://soap.sforce.com/2006/04/metadata">
<fieldPermissions>
<editable>false</editable>
<field>Account.new_field__c</field>
<readable>true</readable>
</fieldPermissions>
</Profile>
You can include all the common permissions in this file, ProfilePermission.txt.

The below code snippet copies the permissions in ProfilePermission.txt to all the profiles.
import os
#Enter the path of profile permission file in String format(Eg: "C:\ProfilePermission.txt")
permissionFilePath = input("Please enter the path of your Permissions file")
#Get the contents from Permissions file. Open the ProfilePermission in "READ" mode.
content=open(permissionFilePath,"r").read()
#Enter the path for profiles folder in String format("C:\DEV\src\profiles")
profileFilesPath = input("Please enter the path of your Profiles folder")
#Iterate all profile files in the folder
for profileName in os.listdir(profileFilesPath):
#Construct Profile file Path
profilePath = profileFilesPath + '\\' +profileName
#Open the profile file in "WRITE" mode and copy the content
with open(profilePath, "w") as profile:
profile.write(content)
#End of Program
Install Python and execute script:
Click here to know how to install and set up python.
Once you are done with setup, double click on the CopyProfilePermission.py file to execute the script. Woohoo!! we are done with the setup and execution.

Comments

Popular posts from this blog

New Beta Architect Certifications by Salesforce

How to deploy Processes in Process builder using ANT?