How to get Object Name from Id value in Salesforce? | Techila
1414
single,single-post,postid-1414,single-format-standard,ajax_updown_fade,page_not_loaded,,qode_grid_1300,footer_responsive_adv,hide_top_bar_on_mobile_header,qode-content-sidebar-responsive,qode-theme-ver-9.4.2,bridge,wpb-js-composer js-comp-ver-4.12,vc_responsive
 

How to get Object Name from Id value in Salesforce?

id-name

How to get Object Name from Id value in Salesforce?

This blog shows how to use the getSObjectType method to obtain an sObject token from an ID. The updateOwnermethod in this sample accepts a list of IDs of the sObjects to update the ownerId field of. This list contains IDs of sObjects of the same type. The second parameter is the new owner ID. Note that since it is a future method, it doesn’t accept sObject types as parameters; this is why it accepts IDs of sObjects. This method gets the sObject token from the first ID in the list, then does a describe to obtain the object name and constructs a query dynamicallly. It then queries for all sObjects and updates their owner ID fields to the new owner ID.

Visualforce Page:

<apex:page Controller=”sample” sidebar=”false” >
<apex:form >
<apex:pageblock id=”pg” >
<apex:pageblockSection >
<apex:pageBlockSectionItem >Record Id</apex:pageBlockSectionItem>
<apex:pageblockSectionItem ><apex:inputtext value=”{!recId}” /></apex:pageblockSectionItem>
</apex:pageblockSection>
<apex:pageBlockButtons >
<apex:commandButton value=”Find” action=”{!find}” reRender=”pg”/>
</apex:pageBlockButtons>
<apex:outputText >The object type is : {!objType}</apex:outputText>
</apex:pageblock>
</apex:form>   
</apex:page>

 

Apex: Controller:

public with sharing class sample {
public Id recId {get;set;}
public String output {get;set;}
public Schema.SObjectType objType {get;set;}
 
public void find() {
objType = recId.getSobjectType();
System.debug(‘Object Type is ‘ + objType);
}
}

 

Output:      

Hence, one can easily find the object name by entering ID in the page.

Courtesy: http://www.infallibletechie.com/search/label/Apex%20and%20Visualforce

Author: techila

No Comments

Post A Comment