0

I am looking to store few fields in custom object with fixed values for http request authentication. I see that I can use Custom setting Fields to do that. However, I am not getting an idea how can I save username and password and others in custom setting fields. If I use default values, it is not working. I am getting no rows to select error. I am kinda new to this section. Please let me know how can I save fixed values on these fields like username and password etc.. Thank you, Ashok.

1

A good mechanism to use for HTTP endpoints is "Named Credentials": see Named Credentials as Callout Endpoints. (The authentication mechanism required by the endpoint has to be one of the ones supported by "Named Credentials".)

This example from the documentation shows how easy it is to use these in your code:

HttpRequest req = new HttpRequest();
req.setEndpoint('callout:My_Named_Credential/some_path');
req.setMethod('GET');
Http http = new Http();
HTTPResponse res = http.send(req);
System.debug(res.getBody());

If you have to use a custom setting, you can use this pattern to obtain the default values:

YourSetting__c ys = YourSetting__c.getInstance();
if (ys.Id == null) {
    // Creates an org-level instance
    ys.SetupOwnerId = UserInfo.getOrganizationId();
    upsert ys;
    // Returns the org-level instance with its defaults set
    ys = YourSetting__c.getInstance();
}
// Field defaults now available
  • I don't think you understand my question here Keith. I need to store the values in the fields first. I can do the rest of the logic. When I run 'SELECT password__c FROM CustSetting__c', I should get the password right away. how can I do that? – AshSFcloud Sep 19 '18 at 8:04
  • @AshokA While you can query a hierarchical custom setting you typically shouldn't; using the methods like getInstance ensures the expected hierarchical behavior is respected. AFAIK the only way to access (easily) the default values is to insert a default instance as shown in my answer. If I'm still not getting it jere, clarify your question and wait for a better answer. – Keith C Sep 19 '18 at 8:10

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.