function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
RelaxItsJustCodeRelaxItsJustCode 

Need help making service class more dynamic, please help..

public with sharing class ServiceClass 
{
     public static Map<String, RecordType> CaseRecordTypesNameMap
    {
        get
        {
            if(CaseRecordTypesNameMap == null){
                CaseRecordTypesNameMap = new Map<String,RecordType>();
                for(RecordType rt: [Select Id, Name from RecordType where sObjectType = 'Case' ])
                CaseRecordTypesNameMap.put(rt.Name, rt);
            }
            return CaseRecordTypesNameMap;

        }
        
        private set;
    }

     public static Map<String, RecordType> TaskRecordTypesNameMap
    {
        get
        {
            if(TaskRecordTypesNameMap == null){
                TaskRecordTypesNameMap = new Map<String,RecordType>();
                for(RecordType rt: [Select Id, Name from RecordType where sObjectType = 'Task' ])
                TaskRecordTypesNameMap.put(rt.Name, rt);
            }
            return TaskRecordTypesNameMap;

        }
        
        private set;
    }
   
  }

What I need is to be able to pass in sObjectName and the RecordType Name to retrieve a single recordtypeid.

 

ServiceClass.TaskRecordTypesNameMap.get('PM Task').Id

Above is what I'm currently using but instead of having to build a look up function for every object I'd rather get it all done in one single call passing two params sObjectName and RecordType Name.  Any ideas?

 

Thank you,

Steve Laycock

harsha__charsha__c

Hi

 

You can make use of custom setting here

 

Store the sobject/sobjects name in the custom setting and make your class to use it dynamically

 

 

RelaxItsJustCodeRelaxItsJustCode

Harsha, not sure I totally understand, could you please elaborate just a little bit more?

 

Thank you,

Steve Laycock

harsha__charsha__c

Store the sobject name in the custom setting

 

In the coding part ( in the query )  without hard-coding the sObject Name, you can use the customsetting instance, which will return the sobject name, which is already stored in that

 

Here is an example for usage of custom settings

 

http://blog.jeffdouglas.com/2010/01/07/using-list-custom-settings-in-salesforce-com/

RelaxItsJustCodeRelaxItsJustCode

Thanks Harsha, but that is not nessesary, I would rather know how to pass the additional param to specify the sobject from the class calling the ServiceClass.  If I use custom settings then I will have to maintain it everytime I add an additional object to Salesforce.  If I do it based off the data in the RecordType table it is more dynamic and maintainable.

 

Any ideas?

 

Thank you,

Steve Laycock

Hong_YanHong_Yan

You could just concatenate the sobject name and record type name together. Then it would be passed in as a single variable.

You'd need a delimiter value that won't get confused with the regular part of a name though.

Calling String.split() would let you divide the parameter into the two parts you need.