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

Trouble accessing named credentials with merge fields

I am on a task where I need to call an external webservice that takes the username and password in the request body, not as part of Authorization header. I did setup the username and password using Named Credentials, but I am having trouble accessing the actual username and password from the named credentials as merge fields.
Below is my setup
Identity Type : Named Principal
Authentication Protocol : Password Authentication
Username : {Service Account Username}
Password : {Service Account Password}

Generate Authorization Header : Unchecked
Allow merge fields in HTTP Header : Unchecked
Allow merge fields in HTTP Body : Checked

I tried various options, but nothing seems to work. When I access the named credential via code using the merge fields {!$Credential.Username} and {!$Credential.Password} and assigning them to string variables, the debug log is only printing {!$Credential.Username} and {!$Credential.Password} only and not the actual username and password.

Apparently, the HTTP send is throwing a ScriptThrownException when I make a callout to the external webservice with the above credentials.

Also, if I have multiple named credentials in my salesforce org, how do I access the correct one using merge fields?

Can someone help?
SwethaSwetha (Salesforce Developers) 
I see you posted on for which the fix you mentioned is 

"The external service was setup in other system's QA/DEMO environment which can only be called from our salesforce QA/DEMO environment. Since I am still in the process of development, the service call from my individual sandbox did not work. I had to fake as if I am calling from Salesforce QA/DEMO environment and it is working now"

Please mark the answer as best to close this thread and also so that others facing same issue would find this information helpful. Thanks