function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Atul Shendge 2Atul Shendge 2 

Develop with and Test with Custom Metadata Types

Hi,

When I am writing below code, I am getting an error 'Variable does not exist: theRecord'
Here is the code:
public class CustomMetadataService {
    public CustomMetadataService() {}
    /**
     * This method instantiates a custom metadata record of type Support_Tier__mdt
     * and sets the DeveloperName to the input String.
     * The record is not inserted into the database, 
     * and would not be found by a SOQL query.
     */
    public Support_Tier__mdt getCustomMetadataRecord(String myName) {
        Support_Tier__mdt supportTier = new Support_Tier__mdt();
        theRecord.DeveloperName = myName;
        return supportTier;
    }
    /**
     * This method retrieves a custom metadata record, changes a field, and returns it
     * to the caller, but does not update the database.
     */
    public Support_Tier__mdt getChangedCustomMetadataRecord(String myNewName) {
        Support_Tier__mdt supportTier = [SELECT Id, DeveloperName from Support_Tier__mdt LIMIT 1];
        theRecord.DeveloperName = myNewName;
        return supportTier;
    }
}
 Help would be really appreciated.

Regards,
Atul Shendge
Best Answer chosen by Atul Shendge 2
David Zhu 🔥David Zhu 🔥
Small typo  in your code. use the fix below.

public Support_Tier__mdt getCustomMetadataRecord(String myName) {
        Support_Tier__mdt supportTier= new Support_Tier__mdt();
        supportTier.DeveloperName = myName;
        return supportTier;
    }

All Answers

David Zhu 🔥David Zhu 🔥
Small typo  in your code. use the fix below.

public Support_Tier__mdt getCustomMetadataRecord(String myName) {
        Support_Tier__mdt supportTier= new Support_Tier__mdt();
        supportTier.DeveloperName = myName;
        return supportTier;
    }
This was selected as the best answer
Atul Shendge 2Atul Shendge 2
Hi David,

Thanks for the reply, But on the second part of the code. I am getting error as Missing '<EOF>' at 'public':

public Support_Tier__mdt getChangedCustomMetadataRecord(String myNewName) {
        Support_Tier__mdt supportTier = [SELECT Id, DeveloperName from Support_Tier__mdt LIMIT 1];
        supportTier.DeveloperName = myNewName;
        return supportTier;
    }
Regards,
Atul Shendge
David Zhu 🔥David Zhu 🔥
Your code is very clear. I don't think any issue in the code. could be special characters between lines?