3

I am using named credentials to store the username password for apex callout. In my apex class. I am storing using the below code to use the named credentials. Is there way not to hard code some_path URL in the code? Link - https://help.salesforce.com/articleView?id=000206791&language=en_US&type=1

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());
1
  • I suppose that if your named credential define the whole url, you don't need to add the '/some_path' at the end. Commented Feb 2, 2017 at 15:55

1 Answer 1

5

I often use a Custom Label to make string values configurable in my code:

req.setEndpoint(Label.My_Credential_Name);

You can also always go to Custom Settings when you want to make parts of your Apex code configurable:

  • List Custom Setting

    MySetting__c myCredential = MySetting__c.getInstance('Some Name');
    req.setEndpoint(myCredential.Path__c);
    
  • Hierarchy Custom Setting

    req.setEndpoint(MySetting__c.getOrgDefaults().My_Credential_Name__c);
    

You might also consider Custom Metadata Types, which would operate similarly to the List Custom Setting approach.

3
  • I would never use custom label .Never for endpoints .Incase organizations translate they can translate these and would create huge issues later Commented Feb 2, 2017 at 19:25
  • Yes, translation would be problematic with labels. It seems like a bit of an admin training issue, though. I considered mentioning it but didn't find a way to concisely do so.
    – Adrian Larson
    Commented Feb 2, 2017 at 19:29
  • 1
    i've moved to Custom Metadata Types and away from list custom settings for the simple reason I get page layouts and hence easier to organize the various config fields
    – cropredy
    Commented Feb 2, 2017 at 20:44

You must log in to answer this question.

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