4

When running the code below anonymously I get a (paraphrased) error:

"Field Custom_Field__c does not exist"

Code:

Id bankId = 'a1e630000000oOG';
Id contactId = '00363000009GnPE';

//verify field exists
Submission__c sub = [SELECT Id, Bank__c FROM Submission__c WHERE Id=:bankId];

//verify record exists and bank field is populated
System.assert(String.valueOf(sub.Bank__c).length()>0);

//verify record exists
System.assert([Select id from contact WHERE id=:contactId].size()==1);

String unrendered = 'Dear {!Bank__c},\n Hope you\'re doing well!';

List<Messaging.RenderEmailTemplateBodyResult> renderResults = Messaging.renderEmailTemplate(contactId, bankId, new List<String>{unrendered});

System.debug(renderResults);

I've made sure the whatId record exists, the field being merged is populated, and the whoId record exists (as can be seen from the assert statements).

But I get an error:

getFieldName=Bank__c;

getMessage=Field Bank__c does not exist. Check spelling.;

getOffset=7;

getStatusCode=EMAIL_TEMPLATE_MERGEFIELD_ERROR;

Here's the full user debug:

13:32:16:146 USER_DEBUG [17]|DEBUG|(Messaging.RenderEmailTemplateBodyResult[getErrors=(Messaging.RenderEmailTemplateError[getFieldName=Bank__c;getMessage=Field Bank__c does not exist. Check spelling.;getOffset=7;getStatusCode=EMAIL_TEMPLATE_MERGEFIELD_ERROR;]);getMergedBody=null;getSuccess=false;])

As seen in the debug log the attribute getMergedBody is null. I'm trying to merge text on the fly so users can send emails with their choice of fields to merge. Why am I getting this error? What does it actually mean since the field does indeed exist?

Thank you!

Edit:

The field is accessible to the running user as checked on the FLS level.

Edit 2:

This question has already been answered. To reproduce the issue in any org use this code:

Opportunity opp = [SELECT Id, StageName FROM Opportunity LIMIT 1];
Contact con = [Select id from contact LIMIT 1];

Id oppId = opp.Id;

Id contactId = con.Id;

//verify record exists and bank field is populated
System.assert(String.valueOf(opp.StageName).length()>0);

//as per the answer, {!StageName} needs to be changed to {!Opportunity.StageName}
String unrendered = 'Dear {!StageName},\n Hope you\'re doing well!';
List<Messaging.RenderEmailTemplateBodyResult> renderResults = Messaging.renderEmailTemplate(contactId, oppId, new List<String>{unrendered});
System.debug(renderResults);
8
  • 1
    did you check the FLS and make sure the field is visible for the user trying to use the template?
    – Rao
    Nov 17, 2016 at 18:59
  • Yes, I'm the running user. It is editable by me. Nov 17, 2016 at 19:08
  • what is the unrendered list<string>? can you post the third param
    – Rao
    Nov 17, 2016 at 19:25
  • it's the variable 'unrendered' which is declared on the line above that. The code compiles in my org so all parameters are present. Nov 17, 2016 at 19:28
  • Duh I think I found the issue, based on your assert I assume you are trying to get the bank__c info related to Submission__c in the unrendered string. Change 'Dear {!Bank__c}...' to 'Dear {!sub.bank__c}...'
    – Rao
    Nov 17, 2016 at 19:35

1 Answer 1

3

I think I found the issue, based on your assert I assume you are trying to get the bank__c info related to Submission__c in the unrendered string. Change 'Dear {!Bank__c}...' to 'Dear {!Submission__c.bank__c}...'

5
  • should be the object name 'submission__c' not the apex variable 'sub' Nov 17, 2016 at 19:40
  • You queried the submission__c and assigned it to sub din't you? If you use submission__c based on how you have the controller the template may still merge. If you want to get the bank__c realted to the submission where bankId = a1e630000000oOG then use sub.bank__c
    – Rao
    Nov 17, 2016 at 19:43
  • sub.Bank__c throws the same "Field doesn't exist error." Submission__c.Bank__c merges as expected. Check the edit on the question to reproduce. Nov 17, 2016 at 19:45
  • Ah ok, may be the templatebody expects the object api name in the merge field as shown in the documentation. I assumed the string unrender gets carried over as template body developer.salesforce.com/docs/atlas.en-us.api.meta/api/…
    – Rao
    Nov 17, 2016 at 19:47
  • I think it merges it like a UI email template help.salesforce.com/… . Wish there were more documentation. In any case, thank you! Nov 17, 2016 at 19:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .