Showing posts with label describe. Show all posts
Showing posts with label describe. Show all posts

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 :-)

Dec 28, 2011

Fetching sObject Type From Record Id

"How I can get the sObject Type from Record Id?". In many requirements I do have 15-digit or 18-digit Id and I want to know the sObject to which the Id belongs to. Answer is using Global Describe we can get the sObject type.


Am getting too many emails from folks from Community Forums regarding this, so here is the generic method which will help all.


public class KeyPrefix
{
    // map to hold global describe data
    private static Map<String,Schema.SObjectType> gd;
    
    // map to store objects and their prefixes
    private static Map<String, String> keyPrefixMap;

    // to hold set of all sObject prefixes
    private static Set<String> keyPrefixSet;
    
    private static void init() {
        // get all objects from the org
        gd = Schema.getGlobalDescribe();
        
        // to store objects and their prefixes
        keyPrefixMap = new Map<String, String>{};
        
        //get the object prefix in IDs
        keyPrefixSet = gd.keySet();
        
        // fill up the prefixes map
        for(String sObj : keyPrefixSet)
        {
            Schema.DescribeSObjectResult r =  gd.get(sObj).getDescribe();
            String tempName = r.getName();
            String tempPrefix = r.getKeyPrefix();
            keyPrefixMap.put(tempPrefix, tempName);
        }
    }
    
    public static String GetKeyPrefix(String ObjId)
    {
        init() ;
        String tPrefix = ObjId;
        tPrefix = tPrefix.subString(0,3);
        
        //get the object type now
        String objectType = keyPrefixMap.get(tPrefix);
        return objectType;
    }
}


Now how you can use this? Simply save the class and pass your object Id in method "GetKeyPrefix" like this 


//a0090000002QGKu will be your object Id
System.debug('::::::: '+ KeyPrefix.GetKeyPrefix('a0090000002QGKu') );


And it will return you the object API name.

May 31, 2011

Picklist Describe Component

I was working over my custom visualforce page where I am displaying some fields including picklist field , text fields etc. Values filled in these fields are used as a search criteria to fetch records from my organization. There were around 30 picklist fields on this page.


Everything was working fine then I suddenly recognize that this search can be used by all users of my application. Then I logged in as user of different profile other than system admin. Pretty amazing for me that most of the picklist and text fields were not visible, then I jumped into the field level security of my object and found that these fields are not visible to this profile. I traversed back to my visualforce page where I saw that I have used input fields to display picklist and text fields. I changed all text inputFields to inputText but it was very difficult to create 30 select list after describing all picklist values from object.


So I have create a component in which we will pass Object API name and Field API name and drop down will be created with all values of that picklist.


Now how to use this component :


1) Get the component downloaded from app exchange : http://bit.ly/liFjUf
2) Install this package to your organization : http://bit.ly/mgPpaq
3) Now create a visualforce page and class code is provided below :


Apex :
public class TestPicklistController
{
    //Property to hold the selected value of component
    public String SelectedVal {get; set;}
    
    public TestPicklistController()
    {
        SelectedVal = '' ;
    }
    
    public void simpleDebug()
    {
        System.debug(':::::::: ' + selectedVal) ;
    }
}
Visualforce :
<apex:page controller="TestPicklistController">

    <apex:form >

        <apex:pageBlock >

            <apex:pageBlockButtons >
                <apex:commandButton value="Check Selected Value" action="{!simpleDebug}" />
            </apex:pageBlockButtons>

            <apex:outputLabel value="Industry"/>
            <arora:PicklistToPicklist value="{!SelectedVal}" objectName="Account" picklistFieldAPI="Industry" useDefaultValue="true" />

        </apex:pageBlock>

    </apex:form>

</apex:page>
Value (Required) : Selected Picklist Value


objectName (Required) : API Name of Object


picklistFieldAPI(Required) : API Name of picklist value


useDefaultValue(Optional) : True (--None-- will be displayed) , False (Only picklist values will be shown)


Feedbacks are always welcomed, also please let me know if there is any problem.


Cheers