You need to sign in to do that
Don't have an account?
Soundhariyaa M
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?
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?
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
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
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
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);
}
}
I'm new to salesforce so Could you tell how to do the same with custom settings too?
Mysettings__c setting = [select url__c from mysettings__c where site__c ='zamamzinaws'];
Use the line above to replace custom metadata query