function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
KbhaskarKbhaskar 

apex code to update permission sets

hi, 
i'm trying to update permission set from apex code  but still it's not working can anyone help me out !
String parameterValue = ApexPages.currentPage().getParameters().get('Permission_Set_ID'); //Permission_Set_ID from the URL for reference 






         }
      }  
   }
       public pagereference Assign()   //assign of user difend permissions
              {
            if(selectedValue == 'readselected')          
             {
              //update Object permission for Opportunity 
                PermissionSet ps =[select id,ProfileId,SystemModstamp,UserLicenseId FROM PermissionSet where id='parameterValue '];
                ObjectPermissions op = new ObjectPermissions();
                op.parentid          = ps.id;
                op.SobjectType       = 'Opportunity';  
                op.PermissionsCreate = false;
                op.PermissionsRead   = true;
                op.PermissionsEdit   = false;
                insert op;
                 } 
          return null;
        
     }
     }

 
Anup JadhavAnup Jadhav
Are you getting an error? or is it just not working as you expect. If it is the latter, perhaps you can try logging the value in the 'selectedValue' variable, and see if it is indeed 'readselected'. 
Prashant WayalPrashant Wayal
Hi Bhaskar,

Please change your query as shown below and your code will work correctly:
 
PermissionSet ps =[select id,ProfileId,SystemModstamp,UserLicenseId FROM PermissionSet where id=: parameterValue];

Here, "parameterValue" is a variable, so you have to use =: syntax for it in SOQL query.

Hope it helps you.

Thanks,
Prashant
KbhaskarKbhaskar
HI, 
Anup ,Prashant it's still not working as expected i have changed as per your syntax but still the premission set is not updating .below is my code would you please check this out !
 
apex code
public with sharing class actionradioab
{
    public String selectedValue {get;set;}
    public boolean selectedread  {get;set;}
    public boolean selectedwrite {get;set;}               //selected value 
    public List<wrapper> lstwrapperIntString {get;set;}  //selected  lstwrapperIntString

    String parameterValue = ApexPages.currentPage().getParameters().get('Permission_Set_ID'); //Permission_Set_ID
    public pagereference actionsupport()                 //pageReference method
    {
        lstwrapperIntString = new List<wrapper>();       //new lstwrapperIntString list
        Map<String, SobjectField> fieldMap = Opportunity.getsObjectType().getDescribe().Fields.getMap(); 
        for(String f : fieldMap.keySet())
        {
         lstwrapperIntString.add(new wrapper(fieldMap.get(f).getDescribe().getLabel(),selectedValue));
        }
        return null;
    }
    public class wrapper    //Wrapper Class 
    {
        public boolean selectedread  {get;set;}
        public boolean selectedwrite {get;set;}
        
        public String  fieldName     {get;set;}
     
    public wrapper(String Name,String write) // wrapper constructor 
         {
        this.fieldName=Name;                     
        this.selectedread=true;
        if(write=='writeseleted')
         {
        this.selectedwrite=true;
         }
      }  
   }
       public pagereference Assign()   //assign of user difend permissions
              {
            if(selectedValue == 'readselected')          
             {
              //update Object permission for Opportunity 
                PermissionSet ps =[select id,ProfileId,SystemModstamp,UserLicenseId FROM PermissionSet where id=: parameterValue];
                ObjectPermissions op = new ObjectPermissions();
                op.parentid          = ps.id;
                op.SobjectType       = 'Opportunity';  
                op.PermissionsCreate = false;
                op.PermissionsRead   = true;
                op.PermissionsEdit   = false;
                insert op;
                 } 
          return null;
        
     }
     }

vf page
 
<apex:page controller="actionradioab">
    <apex:form >
        <apex:pageBlock >
            <apex:pageBlockSection >
                Permission_Set_ID:
                <apex:outputText value="{!$CurrentPage.parameters.Permission_Set_ID}" />
                <apex:selectRadio value="{!selectedValue}">
                    Object Permissions:
                    <apex:selectOption itemLabel="Read" itemValue="readselected"  />
                    <apex:selectOption itemLabel="Write" itemvalue="writeseleted" />   
                    <apex:actionSupport event="onchange" action="{!actionsupport}" reRender="SampleView"  />  
                </apex:selectRadio>
            </apex:pageBlockSection>
            <apex:outputPanel id="SampleView">
                <apex:pageBlockSection rendered="{!IF(selectedValue == 'readselected', true, false)}">
                    <apex:pageblockSectionItem >
                       <apex:pageBlockTable value="{!lstwrapperIntString}" var="w">
                    <apex:column headervalue="Read">
                        <apex:inputcheckbox value="{!w.selectedread}"/>
                        </apex:column>
                         <apex:column headervalue="Write">
                        <apex:inputcheckbox value="{!w.selectedwrite}"/>
                        </apex:column>
                    <apex:column headervalue="Opportunity fields">
                        {!w.fieldName}                 
                       </apex:column>  
                      </apex:pageBlockTable>
                    </apex:pageblockSectionItem>                           
                </apex:pageBlockSection>
                <apex:pageBlockSection rendered="{!IF(selectedValue == 'writeseleted', true, false)}">
                    <apex:pageblockSectionItem >
                      <apex:pageBlockTable value="{!lstwrapperIntString}" var="w">
                    <apex:column headervalue="Read">
                        <apex:inputcheckbox value="{!w.selectedread}"/>
                        </apex:column>
                         <apex:column headervalue="Write">
                        <apex:inputcheckbox value="{!w.selectedwrite}"/>
                        </apex:column>
                    <apex:column headervalue="Opportunity fields">
                        {!w.fieldName}                 
                       </apex:column>  
                      </apex:pageBlockTable>   
                    </apex:pageblockSectionItem>            
                </apex:pageBlockSection>
            </apex:outputPanel>
            <apex:pageBlockButtons >
                <apex:commandButton value="Assign"/>
            </apex:pageBlockButtons>
        </apex:pageBlock>
    </apex:form>
</apex:page>