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
Soundhariyaa MSoundhariyaa M 

How to add URL that changes often as variable and not writing them directly in Apex Class

public with sharing class searchProduct2Controller 
{
   
    @AuraEnabled
    public static List < Product2 > fetchProduct2(String searchKeyWord) {
        String searchKey = searchKeyWord + '%';
        List < Product2 > returnList = new List < Product2 > ();
        List < Product2 > lstOfProduct2 = [select Name,ProductCode,Price__c from Product2
                                           where Name LIKE: searchKey LIMIT 500];
        
        for (Product2 prod: lstOfProduct2) {
            returnList.add(prod);
        }
        return returnList;
    }
    
    @AuraEnabled
    public static List<AWSWrapper> awsData(){
        
        Http h = new Http();
        HttpRequest req = new HttpRequest();
        req.setEndpoint('https://s3.amazonaws.com/targetx-sfdc-interview/packages.json');
        req.setMethod('GET');
        HttpResponse res = h.send(req);
        return (List<AWSWrapper>)JSON.deserialize(res.getBody().replaceAll('__c','_Tempc'), List<AWSWrapper>.class);
    }
}

In awsData(), in req.setEndpoint('https://s3.amazonaws.com/targetx-sfdc-interview/packages.json'); I have given the URL directly here.
How can I assign it to a configurable variable and access that variable here so that whenever URL changes in future, I can change only there and not to make any modifications to the code?
 
Best Answer chosen by Soundhariyaa M
David Zhu 🔥David Zhu 🔥
You may use custom metadatype or custom settings to implement this.
please refer the following steps.
1. Add a custom metadata type, lets call it mySetting, add a customer field called URL__c.
2. Add a record to this metadatype
    Masterlabel: Amazonaws
    Url__c: your URL used in the code

3.in apex code

@AuraEnabled
    public static List<AWSWrapper> awsData(){
        
       Mysettins__mdt setting = [select url__c from mysettings__mdt where masterlabel = 'amazonaws' limit 1];

        Http h = new Http();
        HttpRequest req = new HttpRequest();
        req.setEndpoint(setting.url__c);
        req.setMethod('GET');
        HttpResponse res = h.send(req);
        return (List<AWSWrapper>)JSON.deserialize(res.getBody().replaceAll('__c','_Tempc'), List<AWSWrapper>.class);
    }
}
 

All Answers

David Zhu 🔥David Zhu 🔥
You may use custom metadatype or custom settings to implement this.
please refer the following steps.
1. Add a custom metadata type, lets call it mySetting, add a customer field called URL__c.
2. Add a record to this metadatype
    Masterlabel: Amazonaws
    Url__c: your URL used in the code

3.in apex code

@AuraEnabled
    public static List<AWSWrapper> awsData(){
        
       Mysettins__mdt setting = [select url__c from mysettings__mdt where masterlabel = 'amazonaws' limit 1];

        Http h = new Http();
        HttpRequest req = new HttpRequest();
        req.setEndpoint(setting.url__c);
        req.setMethod('GET');
        HttpResponse res = h.send(req);
        return (List<AWSWrapper>)JSON.deserialize(res.getBody().replaceAll('__c','_Tempc'), List<AWSWrapper>.class);
    }
}
 
This was selected as the best answer
Soundhariyaa MSoundhariyaa M
Hi David Zhu,

I'm new to salesforce so Could you tell how to do the same with custom settings too?
David Zhu 🔥David Zhu 🔥
With custom setting you need to add another field ,let's say site__c.
Mysettings__c setting = [select url__c from mysettings__c where site__c ='zamamzinaws'];
Use the line above to replace custom metadata query