0

I have a very simple query in a fairly simple class that is called from a trigger. The compiler is complaining on this singular LOC in the class:

        String recordTypeName = [Select Name from RecordType where Id=:theCase.recordTypeId and SObjectType = 'Case' limit 1]];

I can execute the query in the query editor, substituting in a viable record ID, and get back a single string value:

select name from RecordType where id='01240000000MH5TXXX'

However, the error reported by the compiler on the LOC is Illegal assignment from List<RecordType> to String

I'm scratching my head because why doesn't the compiler see the query as returning a singular String value?

  • Thanks to you all! I was working on updating this to show my other attempt which, when I added the Case qualifier to the query, made a little more sense, but not much "System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: RecordType.Name" - I really don't know why the below code would result in that error. Do not understand why I could not just get Name off the object? RecordType rt = [Select Id from RecordType where Id=:theCase.recordTypeId and SObjectType = 'Case' limit 1]; String recordTypeName = rt.Name; – Alexx Feb 19 '15 at 6:24
4

Your query return the recordtype object. So you have to use RecordType Object instead of string and your query should be like :

RecordType recordTypeName = [Select Name from RecordType where Id=:theCase.recordTypeId LIMIT 1];

OR, If you simply want the name of recordType use below query:

String recordTypeName = [Select Name from RecordType where Id=:theCase.recordTypeId LIMIT 1].Name;

  • Thank you @Himanshu! I updated my post to show the corrected query (which I realized when I posted it in here and went into the code to update it) - but then still gives same error on the typecast. Why if I query for Name, it gives the entire Object? (Still adapting from 19 years of Java... ) I did not realize you could actually tack an attribute name onto the end of the query. That is a great trick. – Alexx Feb 19 '15 at 6:17
3

I would highly recommend not querying for the record type and instead using the wonderful Describe.SObject class. Salesforce has kindly removed the limits from this class as of the Summer 2015 release so you can use this to your hearts content! The below example was written to only run Triggers on certain record types but you can get the feeling for how to use it:

//use the describe class to get Opportunity record type info
Map<String, Schema.RecordTypeInfo> RT = Opportunity.SObjectType.getDescribe().getRecordTypeInfosByName();
//List of record types to look for, note the label is used not the api name
List<String> recordTnames = new List<String>{'record type label','another record type label', 'etc'};
//List to store ids to run the trigger against
List<Id> idsToCheck = new List<Id>();

for(String check : recordTnames){
    //only works if the record type in the list exists 
    if(RT.containsKey(check)){
        SYSTEM.debug(RT.get(check).getRecordTypeId());
        //add the record type ids to the list for your trigger logic
        idsToCheck.add(RT.get(check).getRecordTypeId());
    }

}
SYSTEM.debug('ids ' + idsToCheck);

Let me know if this helps! I am sure it will help some others who have battled the dread soql limit when running test classes and hitting Triggers which query indiscriminately!

Best,

BinaryLady

1

hello ALEXX,

I guess you must declare a temporary list and then assign the record the string "recordTypeName" Some thing like this..

recordTypeName = TempList[0];

You are getting this error because you are assigning a list to string irrespective of the list containing one record.

0

Regarding the second question, you must have created an instance of Profile__c and mite have used this instance to access the fields of Profile__c.

You can use the actual object i.e. Profile__c instead of its instance say ObjPorfile .

That mite sound a bit illogical that it work ;)

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.