0

I have an apex class which is exposed as a rest API, It sends field value of a custom metadata type in response. My response string is like this -

    string respString = '{"success":'+true+',"customMetVal":'+JSON.Serialize(customMetVal[0])+',
                          "account":'+JSON.Serialize(account[0])+'}'

while testing this class in sandbox, I get complete code coverage but when I try validating in my production org, I get this error:

System.JSONException: Unexpected character ('E' (code 69)): expected a valid value (number, String, array, object, 'true', 'false' or 'null') at input location [1,2]

I need some insights on what I am doing wrong, Is this response that I am sending from apex class not right or do I need to deploy custom metadata type before deploying apex class or anything else which I am not able to see. Also, in my custom metadata type, I have a field which contains value like this -

"Custom_Field__c": "Hi! Greetings, “We can’t help everyone, but everyone can help someone” (Ronald Reagan)."

Is the JSON exception because of this field value? If so then why I am not getting error while testing in sandbox?

4
  • what is included in your change set that you are validating? You'll need to include your custom metadata types in the same change set as the code. Commented Aug 3, 2021 at 13:39
  • The deployment needs to include apex + custom object for the MDT + the custom metadata record(s); that said, testmethods should mock MDT records via a Selector pattern) and not rely on org MDT values
    – cropredy
    Commented Aug 3, 2021 at 16:34
  • @cropredy how do we mock MDT can you provide some insights please, I am new to salesforce development
    – jaiso
    Commented Aug 4, 2021 at 10:21
  • see this answer for one idea - basically, you need to use a separate object (selector) to query the MDT and at run time, the testmethod injects a mock selector to return known values to the caller. See also Lightning Enterprise Architecture 3rd edition book chapter on unit testing for another perspective
    – cropredy
    Commented Aug 4, 2021 at 15:39

0

You must log in to answer this question.

Browse other questions tagged .