+ Start a Discussion
KbhaskarKbhaskar 

apex code to update Field permission sets

hi
i'm trying to update Field permission set from apex code  but still it's not working can anyone help me out !
Error: Compile Error: Method does not exist or incorrect signature: [List<FieldPermissions>].put(Integer, String) 

 
String parameterValue = ApexPages.currentPage().getParameters().get('Permission_Set_ID'); //Permission_Set_ID reference from URL

}
}
//update field permission for Opportunity  permission
        fieldpermissions fp=new fieldpermissions();
        list<fieldpermissions>rt=[select id FROM fieldpermissions where parentid=:parameterValue AND      SobjectType='Opportunity' ];       
 rt.put(1,parameterValue );
        
        if(!rt.isempty())
        {
        fp.id=rt[0].id ;
        }else{
          rt.parentid          = parameterValue ;
          rt.SobjectType       ='Opportunity'; 
        }
        
        for(wrapper i:lstwrapperIntString)
        {
        if(i.selectedread ==true){  //vf page if selected read
         rt.PermissionsRead= true;
        }
        if(i.selectedwrite ==true){
         rt.PermissionsEdit=true;
         rt.PermissionsRead= true;
         }
           upsert rt;
        }
           return null;

    }
}

 
Andy BoettcherAndy Boettcher
You cannot update these as part of standard DML/SOQL.  You need to hit up the Metadata API.
KbhaskarKbhaskar
Hi Andrew Boettcher,i'm a great fan of you and i do visit your website andyinthecloud.com. well coming to my query i was able to update object permisssion by useing standard DML/SOQL 
}
    public pagereference Assign()   //assign of user defined permissions
    {
       //update Object permission for Opportunity 
        ObjectPermissions op = new ObjectPermissions();
        list<ObjectPermissions>ps=[select id FROM ObjectPermissions where parentid=:parameterValue AND SobjectType='Opportunity' ];
        if(!ps.isempty()){
            op.id=ps[0].id;
        }else{                
            op.parentid          = parameterValue ;
            op.SobjectType       ='Opportunity'; 
        }
        if(selectedValue == 'readselected')          
        {
            op.PermissionsCreate = false;
            op.PermissionsRead   = true;
            op.PermissionsEdit   = false;
            }else{
            op.PermissionsCreate = true;
            op.PermissionsRead   = true;
            op.PermissionsEdit   = true;
        }  
        upsert op;

but if I'm wrong,please bare with me.

Abhishek BansalAbhishek Bansal
Hi Bhaskar,

You are using put method on a list that is why you are getting error becuase put is not a method of List.
put method is used with the maps.
So either you change your method to add in place or put or you have to use map in place of list.

If you want to use List than change your code as follows :
String parameterValue = ApexPages.currentPage().getParameters().get('Permission_Set_ID'); //Permission_Set_ID reference from URL

}
}
//update field permission for Opportunity  permission
        fieldpermissions fp=new fieldpermissions();
        list<fieldpermissions>rt=[select id FROM fieldpermissions where parentid=:parameterValue AND      SobjectType='Opportunity' ];       
 rt.add(parameterValue );
        
        if(!rt.isempty())
        {
        fp.id=rt[0].id ;
        }else{
          rt.parentid          = parameterValue ;
          rt.SobjectType       ='Opportunity'; 
        }
        
        for(wrapper i:lstwrapperIntString)
        {
        if(i.selectedread ==true){  //vf page if selected read
         rt.PermissionsRead= true;
        }
        if(i.selectedwrite ==true){
         rt.PermissionsEdit=true;
         rt.PermissionsRead= true;
         }
           upsert rt;
        }
           return null;

    }
}

Let me know if you have any issue or you need more help on this.

Thanks,
Abhishek