[SalesForce] Comparing arguments – wrong types

I have a custom object with a number of record types. Now, in my controller, I want to write an if statement to check whether the current record is a certain record type.

I wrote:

        if (RecordType.Name == 'Recordtype1'){
            variable1= 'value1';
        }else{
            variable1 = 'value2';
        }

The problem seems to be that :

Comparison arguments must be compatible types:
Schema.SObjectField,String

So I assume the problem is that Recordtype1 is a String and RecordType.Name returns an SObjectField.

How can I solve this problem?
Thanks, Lily

Best Answer

Lily,

you cannot directly use RecordType.Name

you can query the RecordType to get the name using the RecordTypeId on the custom object record and then compare the name against a string.

something like

RecordType rt = [Select Name from RecordType where SobjectType = 'CustomObject' AND Id = :cObj.RecordTypeId limit 1];
if (rt.Name == 'Recordtype1'){
    variable1= 'value1';
}else{
    variable1 = 'value2';
} 

additional references :

What would be the best approach to get the recordtype id

Why am I not getting the Record Type name when I use RecordType.Name

Related Topic