+ Start a Discussion
Anand_agrawal@persistent.co.inAnand_agrawal@persistent.co.in 

Problem with the standard object not having NAME field available.

Hi,

 

We have a requirement of overriding the lookup functionality in which we open a popup window which displays the records in table view and selecting any record on that list will display the name field on the parent form and stores the respective ID field on a hidden component bound with the object's reference field.

 

However there are few standard object in the salesforce (service cloud based objects) which doesn't have the name fields available. So it breaks my code and throws the runtime exception name field not found.

 

Is there any way by which I can find out the field name used to identify the record's uniqueness ? (e.g. caseNumber field available in CASE object).

 

I was wondering if we could access the search layout fields information throught apex code. Can we ?

 

Thanks & Regards,

Anand Agrawal.

 

 

Best Answer chosen by Admin (Salesforce Developers) 
CLKCLK

Is there any way by which I can find out the field name used to identify the record's uniqueness ?

 

Ans -

try out this function to get name,Unique,Autonumber field from any sobjectType


String getNameUniqueField(Schema.sObjectType objType)
{
    String strResult = null;
    //Schema.sObjectType objType = obj.getSObjectType();
    Schema.DescribeSObjectResult ObjResult =  objType.getDescribe();
    Map<String, Schema.SObjectField> mapFields = ObjResult.Fields.getMap();
    for(Schema.SObjectField objField : mapFields)
    {
        if(objField.isNameField())
        {
            strResult = objField.getName();
            break;
        }
    }
    
    if(strResult != null)
        return strResult;
        
    for(Schema.SObjectField objField : mapFields)
    {
        if(objField.isAutoNumber())
        {
            strResult = objField.getName();
            break;
        }
    }
    
    if(strResult != null)
        return strResult;
        
    for(Schema.SObjectField objField : mapFields)
    {
        if(objField.isUnique())
        {
            strResult = objField.getName();
            break;
        }
    }
    return strResult;

}

All Answers

CLKCLK

Can u plz specify one of the sobject name with which u hav a problem.

 

For GlobalDescibe of object refer below link-

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_dynamic_describe_objects_understanding.htm

Anand_agrawal@persistent.co.inAnand_agrawal@persistent.co.in

Case object doesn't have name field.... similerly.. solution object doesn't have name field..

CLKCLK

Is there any way by which I can find out the field name used to identify the record's uniqueness ?

 

Ans -

try out this function to get name,Unique,Autonumber field from any sobjectType


String getNameUniqueField(Schema.sObjectType objType)
{
    String strResult = null;
    //Schema.sObjectType objType = obj.getSObjectType();
    Schema.DescribeSObjectResult ObjResult =  objType.getDescribe();
    Map<String, Schema.SObjectField> mapFields = ObjResult.Fields.getMap();
    for(Schema.SObjectField objField : mapFields)
    {
        if(objField.isNameField())
        {
            strResult = objField.getName();
            break;
        }
    }
    
    if(strResult != null)
        return strResult;
        
    for(Schema.SObjectField objField : mapFields)
    {
        if(objField.isAutoNumber())
        {
            strResult = objField.getName();
            break;
        }
    }
    
    if(strResult != null)
        return strResult;
        
    for(Schema.SObjectField objField : mapFields)
    {
        if(objField.isUnique())
        {
            strResult = objField.getName();
            break;
        }
    }
    return strResult;

}

This was selected as the best answer