Monday 21 August 2017

Create Custom Permission Set Using Custom Metadata API Via JavaScript

It is very useful article to create custom permission set using metadata.

What is custom metadata

By using custom metadata , we can create our framework for partners, teams, customers etc. Metadata is the data that describe other data.It is used to retrieve,deploy,create,update and delete customization information such as custom object definition and page layout for our organization. It is used to describe objects, their field and their properties.

Now take a example to create custom permission set using vf page and apex class:

Apex Class

Create a apex class 'PermissionSetClr' to get current host url to find your org you are using and create permission for this org.

public class PermissionSetClr{
    public String sHostUrl{get;set;}
    public PermissionSetClr()
    {
        sHostUrl= ApexPages.currentPage().getHeaders().get('Host'); //Get current Host url.
    }
}

Visualforce Page

Now create visualforce page for above controller and define Javascript function 'createPermissionSet()' in this create XML and provide contents like permission set activation when create in org like here I provide false and provide permission set name and provide page name and class name and set object permissions in permission set when create in org.

Now create XML Http Request to send data through API to create permission set. here data will be XML type which we create in 'sXML' variable and attach with metadata and send the request to create permission set.

<apex:page controller="PermissionSetClr">
     <script>
        function createPermissionSet()
        {
            var sXml = '';
            sXml +='<hasActivationRequired>false</hasActivationRequired>'; // default it shlould be false
            sXml +=' <fullName>salesforceadda_B</fullName><label>salesforceadda_B</label>'; // Permission Setting name 'salesforceadda_B' 
            sXml +='<pageAccesses><apexPage>PermissionSet</apexPage><enabled>true</enabled></pageAccesses>'; //Set Page name to access in permission set
            sXml +='<classAccesses><apexClass>PermissionSetClr</apexClass><enabled>true</enabled></classAccesses>'; //Set class name to access in permission set
            sXml +='<objectPermissions><allowCreate>true</allowCreate><allowDelete>false</allowDelete><allowEdit>true</allowEdit><allowRead>true</allowRead><viewAllRecords>true</viewAllRecords><modifyAllRecords>false</modifyAllRecords><object>Account</object></objectPermissions>';   //Set sObject permission in this permission set like create,delete,edit,read etc.         
          
            // Calls the Metdata API from JavaScript to create the PermissionSet to permit Apex callouts
            var binding = new XMLHttpRequest();
            var request =
                '<?xml version="1.0" encoding="utf-8"?>' +
                '<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">'+
                    '<env:Header>' +
                        '<urn:SessionHeader xmlns:urn="http://soap.sforce.com/2006/04/metadata">' +
                            '<urn:sessionId>{!$Api.Session_ID}</urn:sessionId>' +
                        '</urn:SessionHeader>' +                       
                    '</env:Header>' +
                   
                    '<env:Body>' +                        
                       '<createMetadata xmlns="http://soap.sforce.com/2006/04/metadata">' +
                          '<metadata xsi:type="PermissionSet">'+sXml+'</metadata>'+
                        '</createMetadata>' +                         
                    '</env:Body>' +
                '</env:Envelope>';  
            binding.open('POST', 'https://{!sHostUrl}/services/Soap/m/31.0');
            binding.setRequestHeader('SOAPAction','""');
            binding.setRequestHeader('Content-Type', 'text/xml');
            binding.onreadystatechange = function() {
                if(this.readyState==4) {
                    var parser = new DOMParser();
                    var doc  = parser.parseFromString(this.response, 'application/xml');
                    var errors = doc.getElementsByTagName('errors');
                    var messageText = '';
                }
            }
            binding.send(request);
        }  
        createPermissionSet();
    </script>
   <center> <h1 style="margin-top:30px;font-size:20px">successful created permission set salesforceadda_B</h1></center>
  </apex:page>

After create visualforce page and apex class class successfully then preview visualforce page then permission set will create automatically in your org.

lets look screenshots of permission set in my org below-


Now click on permission then 



Now go to visualforce page access and Apex class access you will find visualforce page and class which you provide name in <pageAccess> and in <classAccess> and these page name and class name should exist in org.






2 comments:

  1. can you plz share oops concept on vf page

    ReplyDelete
    Replies
    1. Sure , but can you please tell me what exactly you want? So that I will write according to that.

      Delete

If you have any doubts, please let me know.