“With the Lightning Design System you can build custom applications with a look and feel that is consistent with Salesforce core features — without reverse engineering our styles! Simply download our platform-agnostic CSS framework and get started today.” – https://www.lightningdesignsystem.com/ Continue reading
Salesforce
Salesforce: Helper Lightning Components
Helper Lightning Components, a name I came up with; or I’d like to think so. Regardless of who came up with the name, the idea still stands. I was working on a project and was thinking of creating a component that can serve as a library, or if you prefer, helper. Continue reading
Salesforce Trailhead: The fun way to learn salesforce
Are you new to salesforce? Or maybe you have only worked on one area and want to learn about another?
I started on developer side of salesforce. Recently, I figured that if I know about others areas (admin, App Cloud etc) then it will make it so much easier to work on developer side. But I had the same questions as above. Where do I start?
Then I found salesforce trailhead and absolutely love it. It’s not just the content but the way it has been arranged. It took me from beginner admin to intermediate admin and other areas. After I had been through those modules, I wanted to put it all together but couldn’t figure out what I could build.
Lo and behold, trailhead has projects. These are simple enough but still allow me to combine various features that I learned.
My goal now? Go through ALL modules and projects on trailhead to get, at least, basic understanding of all salesforce features. Let the journey start……..
Apex: Find Object Type from Record ID Prefix
I get to work on objects in various orgs. It’s not easy (if at all possible) to remember entity type by just looking at Record ID Prefix. salesforce.com provides a Standard Record ID Prefix Decoder list. This still means you need to have this page handy to find those objects. Of course this list also cannot help with custom objects.
In my pursuit to find an answer, I came across piece of code that was answer to this exact problem. The answer uses iteration on metadata through Schema.getGlobalDescribe(). As it loops through all sObjects in the map, it will continue to check for prefix that we need to find. Once there, it will return the object name. By no means this is an optimized solution as it goes through loop of all sObjects till it can find the object. But this is better than having to go through other ways to find same information. Of course, if you come across an easier way then please share.
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;
}
}Now we can easily use this in developer console to check object name whenever we want. Of course you can take it a step further and create a visualforce page, add a search box and make it available to all developers in your org so they can easily use it (if needed).
String objectName = SchemaGlobalDescribe.findObjectNameFromRecordIdPrefix('500');
System.debug(objectName);Oh wait, we have written the class, we have tested it but we are missing an important step. Remember? Yes, “Code Coverage”. That’s an important point that we shouldn’t miss. This is important not just to be able deploy the code but it’s good development practice to make sure we test our code. Following salesforce page provides information on testing best practices.
So, I have written very basic test class to cover the code written above. You can add more functionality to your class and then expand on these basic tests.
@isTest
private class SchemaGlobalDescribeTests{
@istest
private static void testMethodPositive(){
String objectName = SchemaGlobalDescribe.findObjectNameFromRecordIdPrefix('500');
System.assertEquals(objectName,'Case');
}
@isTest
private static void testMethodNegative(){
String objectName = SchemaGlobalDescribe.findObjectNameFromRecordIdPrefix('500');
System.assertNotEquals(objectName,'Account');
}
@isTest
private static void testMethodNull(){
String objectName = SchemaGlobalDescribe.findObjectNameFromRecordIdPrefix('101');
System.assertEquals(objectName,'');
}
@isTest
private static void testMethodException(){
String objectName = SchemaGlobalDescribe.findObjectNameFromRecordIdPrefix('10');
System.assertEquals(objectName,'');
}
}That’s that and now we can easily figure out object name from Record ID prefix.
Please share your thoughts, questions, concerns or constructive criticism.