0

I want to check the record type of Account before doing anything else. I have the following method:

    public static void methodName(Id accountId) {
            
            // get account record type
            List<Account> accType = [SELECT Id, RecordType.Name FROM Acccount WHERE Id = :accountId];
            for(Account[] acc: accType) {
                accRecordTypeName = acc.RecordType.Name;
            }
            
            if(accRecordTypeName == 'Patient_Account') {
                // code
            }
  }

When I try to save the code, it gives me the following error:

  • Variable does not exist: RecordType Line 8
  • Variable does not exist: accRecordTypeName Line 8
  • Variable does not exist: accRecordTypeName Line 11
1

Welcome to the Salesforce Stack Exchange community...

You have some errors there in your code:

  1. You forgot to declare the accRecordTypeName variable...
  2. You are querying filtering by Id is not necessary to save the query results inside a list, I would recommend you just save them in an single SObject not a List of SObjects
  3. Normally, its better to use the DeveloperName for validations, this value dont gets translated, so normally is a better choice.
  4. You wrote Acccount with 3 c
  5. You can use the new safe navigaton operator in case RecordType field is null https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_SafeNavigationOperator.htm
public static void methodName(Id accountId) {
    
    // get account record type
    Account acc = [
        SELECT Id, RecordType.DeveloperName
        FROM Account
        WHERE Id = :accountId
    ];
    
    if(acc.RecordType?.DeveloperName == 'Patient_Account') {
        // code
    }
}
0

You need to instantiate the accRecordTypeName variable like a String. You should consider doing some trailheads for basic understanding of Apex.

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.