3

In my class I have 2 lists and I am displaying the results on a Visualforce component page.

I would like to add a column on my VF page where the value displayed is determined by the first 3 letters of the Salesforce custom object Id (String Id).

If Id startsWith 'a07' ---> replace with "Policy" OR If Id startsWith 'a0Y' ---> replace with "Claim"

public List<CoverageTeamMember> getCoverageTeam() {
    Account a = getAccount(this.controller != null ? this.controller.getId() : accountId);
        List<CoverageTeamMember> l = getCoverageTeam(a);

    return l;
    }

    //Account query to get Servicer 1 & 2
    private Account getAccount( ID account ) {
        return [ SELECT Id, Producer__r.Name, Producer__r.Email__c, Producer__r.Work_Phone__c, Servicer_1__c, Serv1__r.Name, Serv1__r.Email__c, Serv1__r.Work_Phone__c, Servicer_2__c FROM Account WHERE Id = :account ];
    }

    //Policy query to get Active Policy Coverage Team Members
    private List<CoverageTeamMember> getCoverageTeam( Account account ) {
        List<CoverageTeamMember> result = this.getStaticCoverageTeam( account );

        for ( AggregateResult records : [ 
            SELECT Id, Servicer_Department_Name__c role, Servicer__r.Id name, Servicer__r.Name actualname, Servicer__r.Email__c email, Servicer__r.Work_Phone__c phone, Active__c a
            FROM Policy__c
            WHERE Account__c = :account.Id AND 
                Servicer_Department_Name__c != null AND
                Servicer__c != null AND
                Active__c = True
            GROUP BY Id, Servicer_Department_Name__c, Servicer__r.Id, Servicer__r.Name, Servicer__r.Email__c, Servicer__r.Work_Phone__c, Active__c
        ] ) {
            result.add( new CoverageTeamMember( records ) );
        }

    //Claims query to get Active Claims & Policy Coverage Team Members
        for ( AggregateResult claims : [ 
            SELECT Id, Servicer_Department_Name__c role, Servicer__r.Id name, Servicer__r.Name actualname, Servicer__r.Email__c email, Servicer__r.Work_Phone__c phone, IsClosed__c a
            FROM Claim__c 
            WHERE Account__c = :account.Id AND 
                Servicer_Department_Name__c != null AND
                Servicer__c != null AND
                IsClosed__c = False
            GROUP BY Id, Servicer_Department_Name__c, Servicer__r.Id, Servicer__r.Name, Servicer__r.Email__c, Servicer__r.Work_Phone__c, IsClosed__c
        ] ) {
            result.add( new CoverageTeamMember( claims ) );
        }

        return result;
    }

    private List<CoverageTeamMember> getStaticCoverageTeam( Account account ) {
        List<CoverageTeamMember> result = new List<CoverageTeamMember>();
        result.add( new CoverageTeamMember('Client Account', 'Producer', account.Producer__r.Id, account.Producer__r.Name, account.Producer__r.Email__c, account.Producer__r.Work_Phone__c) );
        result.add( new CoverageTeamMember('Client Account', 'Client Owner', account.Serv1__r.Id, account.Serv1__r.Name, account.Serv1__r.Email__c,account.Serv1__r.Work_Phone__c ) );
        //result.add( new CoverageTeamMember( 'P&C Servicer Coordinator', account.Servicer_1__c ) );
        //result.add( new CoverageTeamMember( 'Benefits Coordinator', account.Servicer_2__c ) );   
        return result;
    }

    public class CoverageTeamMember {
    public final String id { get; set; }
    public final String role { get; set; }
    public final String name { get; set; }
    public final String actualname { get; set; }
    public final String email { get; set; }
    public final String phone { get; set; } 


    public CoverageTeamMember( AggregateResult records) {
        this(     
        String.valueOf( records.get( 'id' ) ), 
        String.valueOf( records.get( 'role' ) ), 
        String.valueOf( records.get( 'name' ) ), 
        String.valueOf( records.get( 'actualname' ) ),
        String.valueOf( records.get( 'email' ) ), 
        String.valueOf( records.get( 'phone') ) );
    }

    public CoverageTeamMember(String id, String role, String name, String actualname, String email, String phone) {
        this.id = id;
        this.role = role;
        this.name = name;
        this.actualname = actualname;
        this.email = email;
        this.phone = phone;
    }
  }

2 Answers 2

1

I'm assuming that a07 and a0Y are keyPrefix values for Policy__c and Claim__c, respectively. Normally you can convert an Id to the label of its' SObjectType as follows:

public static String getSObjectTypeLabel(Id input)
{
    return input.getSObjectType().getDescribe().getLabel();
}

However, you have a couple things in your structure complicating that level of simplicity:

  1. Your property is of the String type, meaning you have to convert it to an Id first
  2. Your property is named Id, so you cannot call Id.valueOf.

You can add the following property to your CoverageTeamMember wrapper. If you change the type of your id property to Id, you can remove the need to cast altogether:

public class CoverageTeamMember
{
    public String id { get; set; }
    // replace with public Id id { get; set; }
    // other properties

    public String sObjectLabel
    {
        get
        {
            SObjectType idType = ((Id)this.id).getSObjectType();
            return idType.getDescribe().getLabel();
        }
        // no setter required
    }
}
7
  • Thanks Adrian! Receiving this error > "Method does not exist or incorrect signature: [String].valueOf(String)" - What am I missing here?
    – SFDC-ThEq
    Commented May 5, 2016 at 21:12
  • public class CoverageTeamMember { public final String id { get; set; } // other properties public String sObjectType { get { SObjectType idType = id.valueOf(this.id).getSObjectType(); return idType.getDescribe().getLabel(); } // no setter required } public final String role { get; set; } public final String name { get; set; } public final String actualname { get; set; } public final String email { get; set; } public final String phone { get; set; }
    – SFDC-ThEq
    Commented May 5, 2016 at 21:13
  • @SFDCCrystalCo Answer updated.
    – Adrian Larson
    Commented May 5, 2016 at 21:37
  • @Adran Larson: my VF component is still showing the 'ID' of the record rather than the sObjectLabel. Would you have any ideas why?
    – SFDC-ThEq
    Commented May 6, 2016 at 13:54
  • 1
    That was it!! Thanks @Adrian Larson!! Where should I send the beers?
    – SFDC-ThEq
    Commented May 6, 2016 at 14:39
2

If you want to use the object name as set in SF, then you could use the built in key prefix functionality as described here:

public class SchemaGlobalDescribe{
    public static String findObjectNameFromRecordIdPrefix(String recordIdOrPrefix){
        String objectName = '';
        try{
            //Get prefix from record ID
            //This assumes that you have passed at least 3 characters
            String myIdPrefix = String.valueOf(recordIdOrPrefix).substring(0,3);

            //Get schema information
            Map<String, Schema.SObjectType> gd =  Schema.getGlobalDescribe(); 

            //Loop through all the sObject types returned by Schema
            for(Schema.SObjectType stype : gd.values()){
                Schema.DescribeSObjectResult r = stype.getDescribe();
                String prefix = r.getKeyPrefix();
                System.debug('Prefix is ' + prefix);

                //Check if the prefix matches with requested prefix
                if(prefix!=null && prefix.equals(myIdPrefix)){
                    objectName = r.getName();
                    System.debug('Object Name! ' + objectName);
                    break;
                }
            }
        }catch(Exception e){
            System.debug(e);
        }
        return objectName;
    }
}

Then you can pass in prefix and get back object name:

String objectName = SchemaGlobalDescribe.findObjectNameFromRecordIdPrefix('500'); System.debug(objectName);

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .