I am having an issue when attempting to reference the {!$Credential.UserName} and {!$Credential.Password} merge fields.

I created a named credential with the following configuration:

  • Identity Type: Named Principal
  • Authentication Protocol: Password Authentication
  • Generate Authorization Header: false
  • Allow Merge Fields in HTTP Header: true
  • Allow Merge Fields in HTTP Body: false

I also set the username and password as the credentials expected by our vendor's API. I then define the Authorization Header keys in Apex and use the merge fields as reference.

        req.setHeader('Authorization-UserName', '{!$Credential.UserName}');
        req.setHeader('Authorization-Token', '{!$Credential.Password}');

I initially hard-coded the Authorization-UserName and Authorization-Token (which worked). I am also using the same endpoint that was defined in Remote Site Settings. But due to the fact that I plan to use Named Credentials, I disabled the end point in Remote Site Settings (see: Create Named Credentials to Define Callout Endpoints and Their Authentication Settings). It appears that the {!$Credential.UserName} is not resolving to the defined username value.

  • ,I am also facing the same issue. Did you find a solution for this issue. – arjun singh Jan 13 '17 at 2:42
  • I have not found a solution. I'll be sure to update this thread when I do. – structure Jan 13 '17 at 12:36
  • What does show up then? It sounds like {!$Credential.Password} merges properly? – Adrian Larson Apr 26 '17 at 21:36

I was having the same issue, but I needed the username and password in the body of the request, I solved this by assigning the merge fields to string variables, then put the variables with all the other body content. You have to be aware that when you debug the variables or the body you will see the merge field '{!$Credential.UserName}' .... and not the actual value, the value is only replaced when the request is finally sent.

        Http h = new Http();
        HttpRequest req = new HttpRequest();
        req.setHeader('Content-Type', 'application/x-www-form-urlencoded');

        string username = '{!$Credential.UserName}';
        string password = '{!$Credential.Password}';

        string body = CreateRequestBody(acc);

        req.setBody('username='+ username + '&password=' + password + '&' + body);

        HttpResponse res = h.send(req);
        String responseJson = res.getBody();

  • Is there any merge field to obtain the user's token? – Mauricio Oliveira May 30 '17 at 18:26
  • @kleytman-aular So, when you say "you will see the merge field '{!$Credential.UserName}' .... and not the actual value, the value is only replaced when the request is finally sent", do you mean to say that there is no way to see the actual merge fields values in the debug logs? – Azeez Aug 30 '18 at 11:08
  • The values are not merged in until the "last mile" when the request is about to exit Salesforce. However if you need to prove to yourself (for debugging's sake) that it merges, you could use a service like PostBin or RequestBin to create a fake endpoint and it will show you the entire request that you sent to it. – Charles T Nov 29 '18 at 15:17

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.