Based on Profile Select Sobjects............

Cls:  SobjectListcls


public class SobjectListcls
{
     set<string> salesforceObjectSet = new set<string>();
    Map<Id,List<ObjectPermissions>> permissionsetIdAndlstObjectPermissions = new Map<Id,List<ObjectPermissions>>();
 
    public string strSobjectName{get;set;}
    public SobjectListcls()
    {
      onlyReadPerrmission();
      system.debug('sobject...'+salesforceObjectSet);
    }
    public void onlyReadPerrmission()
    {
        String userid = UserInfo.getUserId();
        User thisUser = [select id, profile.Name from User where id=:userid];
         system.debug('UserName...'+thisUser);
        permissionset perset = [select id from permissionset where PermissionSet.Profile.id =:thisUser.profileId];
        system.debug('check ---------perset'+perset);
   
        PermissionSetAssignment[] lstPermissionSetAssignment =[SELECT AssigneeId,PermissionSetId FROM PermissionSetAssignment where AssigneeId =:UserInfo.getUserId() and PermissionSetId !=:perset.Id];
        system.debug('checklstPermissionSetAssignment'+lstPermissionSetAssignment);
        set<Id> permissionsetIds = new set<Id>();
        for(PermissionSetAssignment pe :lstPermissionSetAssignment){
            permissionsetIds.add(pe.PermissionSetId);
        }
        system.debug('check permissionset Id -------'+permissionsetIds);
       
        for(ObjectPermissions objP :[SELECT Parent.Name,SobjectType, PermissionsRead, parentid,PermissionsCreate, PermissionsEdit FROM ObjectPermissions where (parentid in :permissionsetIds OR parentid=:perset.Id) and PermissionsCreate=true]){
            if(permissionsetIdAndlstObjectPermissions.get(objP.parentid)==null){
                permissionsetIdAndlstObjectPermissions.put(objP.parentid,new List<ObjectPermissions>());
            }
            permissionsetIdAndlstObjectPermissions.get(objP.parentid).add(objP);
        }
        system.debug('check-------------'+permissionsetIdAndlstObjectPermissions);
        for(Id i :permissionsetIdAndlstObjectPermissions.keyset()){
            for(ObjectPermissions pe :permissionsetIdAndlstObjectPermissions.get(i)){
                salesforceObjectSet.add(pe.SobjectType);
            }
        }              
     
    }
    public List<selectoption> getlstAccount()
    {      
        List<selectoption> lstSelectOption= new List<selectoption>();
        lstSelectOption.add(new selectOption('None','None'));
        Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe();    
        for(Schema.SobjectType obj:gd.values())
        {
            if(obj.getDescribe().isCreateable())
            {
             if(salesforceObjectSet.contains(obj.getDescribe().getName()))
            lstSelectOption.add(new SelectOption(obj.getDescribe().getName(),obj.getDescribe().getLabel()));
            }
        }          
        return lstSelectOption;              
    }
    public pagereference redirect()
    {
     
     PageReference prRef;
   
     if(strSobjectName!='None')
         {
         
          //lstofAllfieldAndName = new List<String>();
          Map<string, Schema.SObjectType> mapOfAllObj = Schema.getGlobalDescribe();
          Schema.SObjectType sot = mapofAllObj.get(strSobjectName);
        Schema.DescribeSObjectResult r = sot.getDescribe();
        String prefix = r.getKeyPrefix();
        System.debug('Prefix is ' + prefix);
        if(prefix!=null)
        {
            prRef = new PageReference('/'+prefix+'/e?retURL=%2F'+prefix+'%2Fo');
          // prRef = new PageReference('https://ap1.salesforce.com/001/e?retURL=%2F001%2Fo');
            prRef.setRedirect(true);
           
        }
    }
    return prRef;
     
    }
}


VF page:    ShowSobjectList


<apex:page controller="SobjectListcls">
    <apex:form >
        <apex:pageBlock >
             <apex:pageBlockSection title="List Of Objects" id="pb">
                 <apex:outputLabel value="Select Object"/>
                 <apex:selectList size="1" value="{!strSobjectName}">
                     <apex:selectOptions value="{!lstAccount}"></apex:selectOptions>
                     <apex:actionSupport event="onchange" action="{!redirect}" />
                 </apex:selectList>
             </apex:pageBlockSection>
         </apex:pageBlock>
    </apex:form>
</apex:page>

Comments

Popular Post

Dynamic SOQL based on Sobject Name and Where Clause

Copy Billing Address to Shipping Address on VF Page Using JavaScript

Get list of Key Prefix of Sobject in Salesforce