Populate Objects in Drop down and on Selection redirect on standard creation page

Requirement: Show a drop down on which there are standard and custom object will be appear when select any object it should redirect to standard new creation page

Class Code:

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();
    }
    public void onlyReadPerrmission()
    {
        String userid = UserInfo.getUserId();
        User thisUser = [select id, profile.Name from User where id=:userid];
    
        permissionset perset = [select id from permissionset where PermissionSet.Profile.id =:thisUser.profileId];
        PermissionSetAssignment[] lstPermissionSetAssignment =[SELECT AssigneeId,PermissionSetId FROM PermissionSetAssignment where AssigneeId =:UserInfo.getUserId() and PermissionSetId !=:perset.Id];

        set<Id> permissionsetIds = new set<Id>();
        for(PermissionSetAssignment pe :lstPermissionSetAssignment){
            permissionsetIds.add(pe.PermissionSetId);
        }
      
        
        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);
        }
     
        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')
         {
           
Map<string, Schema.SObjectType> mapOfAllObj = Schema.getGlobalDescribe();
Schema.SObjectType sot = mapofAllObj.get(strSobjectName);
Schema.DescribeSObjectResult r = sot.getDescribe();
String prefix = r.getKeyPrefix();

        if(prefix!=null)
        {
            prRef = new PageReference('/'+prefix+'/e?retURL=%2F'+prefix+'%2Fo');
            prRef.setRedirect(true);          
        }
    }
    return prRef;
       
    }
}


VF Page Code


<apex:page controller="SobjectListcls" sidebar="false">
    <apex:form >
        <apex:pageBlock tabStyle="Account" >
             <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>




Thanks,
Sumit Shukla




Comments

Popular posts from this blog

Salesforce Spring 16 Release Exam (Maintenance Exam Q&A) for Developer 201 Admin

Show Hyper Link On Add Error in Salesforce Trigger

Show the Success Message before Redirecting the Detail page on Visualforce Page