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
Shivam Kapoor 33Shivam Kapoor 33 

Need Help in Salesforce Named credential

Please help me
Snippet code related to named credential
System.debug('objAuthentication'+objAuthentication.Client_s_Username__c);
Http h = new Http();
HttpRequest req = new HttpRequest(); req.setEndpoint('callout:Test/dwsso/oauth2/access_token'); req.setMethod('POST'); req.setBody('grant_type=password&username=tfeldmeier%40pfsweb.com&password={!HTMLENCODE($Credential.Password)}'); string username = ***********'; string password = 'GmmR2fH2S@A7'; req.setHeader('Content-Type', 'application/x-www-form-urlencoded'); Blob headerValue = Blob.valueOf(username + ':' +password); String authHeader = 'BASIC' +Encodingutil.base64encode(headerValue); req.setHeader('Authorization',authHeader); HttpResponse resp = h.send(req); System.debug('response'+resp.getBody());```
please take on look on screenshot as well 

https://i.stack.imgur.com/4CcBt.png

 

It is giving error
response{"error_description":"Client authentication failed","error":"invalid_client"}

Raza0195Raza0195
Step first : create connected app to generate ClientId and Clientsecret 
Step second : Create Name Credentail 
User-added image
Please find the below code :
public class UseNameCredential {
    private Static String username{get;set;}
    private Static String password{get;set;}
    private Static String clientId{get;set;}
    private Static String clientsecret{get;set;}
    //This method used to create access token auth 2.0
    public static void getAccessToken(){
        username = 'xxxxxxxxxxxxx';
        password = 'xxxxxxxxxxxxxx';
        clientId = 'xxxxxxxxxxxxxxxxxxxxxxxxxx';
        clientsecret = 'xxxxxxxxxxxxxxxxxxxxxxx';
        HttpRequest req = new HttpRequest();
        req.setMethod('POST');
        req.setHeader('Content-Type','application/x-www-form-urlencoded');
        // Salesforce_Credentail : name credential API name
        req.setEndpoint('callout:Salesforce_Credentail/services/oauth2/token');
        req.setBody('grant_type=password' + '&client_id='+clientId + 
                    '&client_secret='+clientsecret + '&username='+username + '&password='+password);
        Http http = new Http();
        HttpResponse res = http.send(req);
        if(res.getStatusCode() == 200 && res.getStatus()=='Ok'){
            System.debug('access token'+res.getBody());
        }
    }
}
Raza0195Raza0195
If it is not useful for you. so Please send your endpoint here.
Shivam Kapoor 33Shivam Kapoor 33
Thank you @Raza0195 for reply
Can you please let me know
"{!$Credential.UserName}" and "{!$Credential.Password}" will return named credential username and password or it will return salesforce username and password ?
Raza0195Raza0195
Yes, Username and password of the running user. Available only if the named credential uses password authentication.

Please find below documents URL:https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_callouts_named_credentials_merge_fields.htm

Please choose the best answer if it is helpful for you.
Shivam Kapoor 33Shivam Kapoor 33
Thank you @Raza0195 for reply
Just to be sure..
It will "{!$Credential.UserName}" and "{!$Credential.Password}" return salesforce username and password of current user?
Raza0195Raza0195
Yes, I will be return username and password.