Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save rgvaldovino/943663a742c6c82cecea5ae9bdca1ca7 to your computer and use it in GitHub Desktop.
Save rgvaldovino/943663a742c6c82cecea5ae9bdca1ca7 to your computer and use it in GitHub Desktop.

Find Permission Sets and Profiles with certain permissions enabled

Sample script using jsforce to query permission set object in order to find all Permission Sets and Profiles that have certain permissions enabled.

//index.js
var jsforce = require('jsforce');
var config = require('./config.js');

var conn = new jsforce.Connection({
  loginUrl : config.serverUrl
});

conn.login(config.username, config.password)
.then(function(){

    //validate if permissions were specified
    if(config.permissionNames == null){
        reject('Permissions not specified');
    }

    //prepare where clause conditions
    var permConditions = [];
    config.permissionNames.forEach(function(perm){
        permConditions.push(perm + ' = true');
    });

    return new Promise(function(resolve, reject){

        //build query
        conn.sobject('PermissionSet')
        .select('Id,Label,IsOwnedByProfile,Profile.Name,' + config.permissionNames.join(','))
        .where(permConditions.join(' OR '))
        .execute(function(err, records){
            if(err)
                reject(err);

            else{
                //for each permission specified
                //create a list of permission sets and a list of profiles that have them enabled
                var arrPermissions = {};
                config.permissionNames.forEach(function(permName){

                    var permissions = {Profiles:[], PermissionSets:[]};

                    records.forEach(function(permSet){

                        if(permSet[permName] == true){
                            if(permSet.IsOwnedByProfile){
                                permissions.Profiles.push(permSet.Profile.Name);
                            }
                            else{
                                permissions.PermissionSets.push(permSet.Label);
                            }
                        }

                    });

                    arrPermissions[permName] = permissions;
                });

                resolve(arrPermissions);
            }
        });
    });
})
.then(function(arrPermissions){
    console.log('########## Permissions Enabled ##########');
    for(var key in arrPermissions){
        console.log('%s: %s', key, JSON.stringify(arrPermissions[key], null, 4));
    }
})
.catch(function(err){
    console.log(err);
});
How to use

Set up your env variables and list of permissions that you want to check.

// config.js
module.exports = {
    username : 'your-user-name',
    password : 'password+securitytoken',
    serverUrl : 'https://test.salesforce.com',
    permissionNames : [
        'PermissionsIsSsoEnabled'
    ]
}

Open a terminal and execute:

$ node index.js
PermissionsIsSsoEnabled: {
    "Profiles": [
        "System Administrator"
    ],
    "PermissionSets": [
        "Custom Permission Set 1",
        "Custom Permission Set 2"
    ]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment