1

Related to my latest flurry of questions, I am trying to write the Record Type Name as a String data type into a text DML Log custom object field.

You will likely be able to tell by my code that I have been banging my head against the wall on this one for a while:

SObject_Record_Type__c = (String) sobj.getSObjectType.getDescribe().getRecordTypeInfosById().getName()

This is the farthest I have come, and it throws this compile error:

Field expression not allowed for generic SObject at line 11 column 68

I would continue to bang my head against this and scour the Sobj method documentation (as I have already), but I am against a deadline and feel I am missing something big-- What is the next step in successfully writing the sObject's Record Type name into the text field?

EDIT: Here is the latest update to that statement (shoutout to cropredy for the help):

Name__c = (String) sobj.get('name'),
                                 SObject_Id__c = (Id) sobj.get('Id'),
                                 SObject_Record_Type__c = sobj.getSobjectType().getDescribe().getRecordTypeInfos().size() <= 1  // master recordtype will exist in describe even if no others 
                                        ? null
                                        : (String) sobj.get('recordtypeId')

This has worked in passing the RecordTypeId to the field, but I am trying to pass the Record Type Name in to that field.

Does it make sense to just assign this new statement to a temporary variable, then do a .getName() lookup against that variable and pass it into the custom object's field?

7
  • 2
    Try replacing getSObjectType with getSObjectType()
    – javanoob
    Apr 27, 2016 at 3:50
  • 2
    Also getRecordTypeInfosById() returns a map and there is no method named getName() for Map object. You might want something like this getRecordTypeInfosById().get('validRecordTypeIdHere').getName()
    – javanoob
    Apr 27, 2016 at 3:55
  • @javanoob thanks-- for broader context, I am trying to write a method that will work for Objects that have multiple record types, along with Objects that do not have any.
    – GPP
    Apr 27, 2016 at 5:00
  • In that case, you need to add few more lines of code. You need to assign the map returned into a Map type variable and check the size of the map and go from there. But I am interested if you can pull it off all these scenarios in one line :)
    – javanoob
    Apr 27, 2016 at 5:17
  • 1
    I am sorry but I did not get what you are trying to mention. Are you still facing any issues with what you want to achieve finally?
    – javanoob
    Apr 27, 2016 at 5:53

1 Answer 1

2

Example to get the dynamic recordType

//Generate a map of tokens for the sObjects in your organization
Map<String, SObjectType> gd = Schema.getGlobalDescribe();

//Retrieve the describe result for the desired object
DescribeSObjectResult result = gd.get('Account').getDescribe();

//Generate a map of tokens for all the Record Types for the desired object
Map<String,Schema.RecordTypeInfo> recordTypeInfo = result.getRecordTypeInfosByName();
system.debug('RECORD TYPES:' + recordTypeInfo);

//Retrieve the record type id by name
String recordTypeId = recordTypeInfo.get('RECORDTYPE_NAME').getRecordTypeId();
system.debug('RECORD Id:' + recordTypeId);

NOTE: Replace Account and RECORDTYPE_NAME based on your requirement.

5
  • Thanks Ratan, I will make sure to clarify in OP, but I am currently attempting to retrieve this record type name in the middle of a For() loop. It looks like what you're suggesting would require breaking this out into a separate variable and then assigning that variable in the For() loop, correct?
    – GPP
    Apr 27, 2016 at 8:30
  • 1
    @GPP you can make the global describe outside of for loop and inside for loop do the rest things..
    – Ratan Paul
    Apr 27, 2016 at 9:14
  • @GPP is it resolved ?
    – Ratan Paul
    May 3, 2016 at 11:45
  • thanks for circling back! For this string: //Retrieve the describe result for the desired object \ DescribeSObjectResult result = gd.get('Account').getDescribe(); Can I just replace ('Account') with ('SObject')? Not sure why Account is in there
    – GPP
    May 3, 2016 at 19:05
  • after reading this, I think we are talking past each other. I am NOT trying to retrieve a Record Type's Id by using the record type Name as a key, I am trying to write the deleted SObject's Record Type Name to a text field. Not sure this code achieves that. Thanks
    – GPP
    May 3, 2016 at 20:22

You must log in to answer this question.

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