1

I am trying to make it so that if a user opens a case with the "Customer Save" record type that it updates the LE_Status__c field on the account object. I keep getting error

Comparison arguments must be compativle types: SObject:RecordType, String at line 4 column 13.

I am learning apex so if your answer includes code could you also add an explanation so I can learn from my mistake?

My code is below.

Trigger UpdateLEStatus on Case (after insert) {
    Set<Id> UpdatedCaseIds = new Set<Id>(); 
    for (Case c: Trigger.new) {
        if (c.RecordType == 'Customer Save') {
            UpdatedCaseIds.add(c.id);
        }    
    }
    List<Account> AccountForUpdating = [SELECT LE_Status__c
                                                       FROM Account
                                                       WHERE Case IN: UpdatedCaseIds];
    for (Account item: AccountForUpdating) {
        item.LE_Status__c = 'Red';
    }
    update AccountForUpdating;
}
1

You are trying to compare Record Type with a String , since both are different data types you are getting this error.

you have to either use

  • RecordType.Name - which you have to query for the record or
  • RecordTypeId which will be there in the context variable

to check/compare the type.

so your code will look like this..

Trigger UpdateLEStatus on Case (after insert) {
    Set<Id> UpdatedCaseIds = new Set<Id>(); 

    Id recordTypeId = Schema.SObjectType.Case.RecordTypeInfosByName.get('Customer Save').RecordTypeId;

    for (Case c: Trigger.new) {
        if (c.RecordTypeId == recordTypeId) {
            UpdatedCaseIds.add(c.id);
        }    
    }
    List<Account> AccountForUpdating = [SELECT LE_Status__c
                                                       FROM Account
                                                       WHERE Case IN: UpdatedCaseIds];
    for (Account item: AccountForUpdating) {
        item.LE_Status__c = 'Red';
    }
    update AccountForUpdating;
}

here's some other related discussions which you can refer as well

How to find current record's RecordType name

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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