Jul 27, 2012

Please Remove The System Objects From My sObject List

Question is "Can I remove the system objects from list returned by describe?"

I need to identify on which all objects I can create approval process. First and only thought is to use describe and get the list of sObjects, but the problem is describe returns too many other objects on which we can not create approval process like "campaignfeed", "caseteamrole" etc.. So what is the use of showing them all. Why not we can show the list of only those object which are displayed when we create approval from native UI?



Here is the solution for this big time problem. Use this apex code to get the sObject List (use describe in a smart way)

public class ApprovalEnabledObjectsController
{
    //List displayed on UI
    public List<selectoption> supportedObject {get; set;}
    
    //Constructor
    public ApprovalEnabledObjectsController()
    {
        //Initialize
        supportedObject = new List<selectoption>() ;
        
        //Get only reference to objects
        for(Schema.SObjectType item : ProcessInstance.TargetObjectId.getDescribe().getReferenceTo())
        {
            //Excluding custom setting objects
            if(!item.getDescribe().CustomSetting)
            {
                //Adding to list
                supportedObject.add(new SelectOption(item.getDescribe().getLocalName().toLowerCase() , item.getDescribe().getLabel() ));
            }
        }
        
    }
}


Visualforce Page (Test page to show the picklsit)



    
        
    




Special thanks to Amit Jain who helped me with this :-)

9 comments:

  1. Hai ankit, can we distinguish objects ,like on which we can create approval process and Not.?

    ReplyDelete
    Replies
    1. Hari

      Above code snippet will give you list of object on which Approval process can be defined. But it doesn't tell if there is one already defined on a particular object.

      Delete
    2. I was successfully using this approach in my appexchange app code, however these lines don't work (are not supported) in PE org as PE Orgs don't support approval process by default.

      Any other work around to fetch ALL standard and custom objects (and ignore system objects)?

      Delete
  2. how to get only custom objects

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete
  5. Hi sir,
    your developing " Page Block Table With Dynamic Columns In Salesforce" is not working on custom objects.

    help me......

    ReplyDelete
  6. Hi,

    How to get only those objects which the user has access to?
    Currently I am getting all the objects are separating out knowledge article types, with __kav filter. but it displays all the article types, I want to display only those article types, which the user has access to.

    Thanks !!

    ReplyDelete