Introduction
The main issue for the topic of this article was saving the security token. Since I am new to Salesforce, I have used the trial version of trailhead and trial o365 tenant. The only configuration I did on salesforce is to add remote site settings. In the below approach, you can see that I have used a simple button to make a REST callout every time. In Approach 1, I have used the GetJWT to get access_token, deserialize it, and pull the access_token from the JSON response. Then pass it to the HTTP Callout with sample POST request.
Note
The access token being generated was approximately 1100 chars.
I was researching where to store the Client secret, client ID. Then I stumbled upon a lot of options, shown below.
- Custom Settings - No way to store the 1000 chars. Fields limited to 255 chars.
- Custom Metadata Types - This was the best option for me, since these support long fields. But I thought the implementation of these was a bit complex. I will post the code below which will basically read and write to a custom metadata type. Also every time to update a value, it would use a deploy container which didn’t mean anything because I am a SharePoint guy. ;) (there is deployment done every time we need to update the value (code sample below).
Controller
- public class SPSFOAuthController {
- public final List VATs {
- get;
- set;
- }
- final Map VATsByApiName {
- get;
- set;
- }
- public SPSFOAuthController() {
- VATs = new List();
- VATsByApiName = new Map();
- for (SPSFOAuth__mdt v: [SELECT QualifiedApiName, MasterLabel, Accesstoken__c
- FROM SPSFOAuth__mdt
- ]) {
- VATs.add(v);
- VATsByApiName.put(v.QualifiedApiName, v);
- }
- }
- public PageReference save() {
- // Create a metadata container.
- Metadata.DeployContainer container = new Metadata.DeployContainer();
- List vatFullNames = new List();
- for (String recordName: VATsByApiName.keySet()) {
- vatFullNames.add('SPSFOAuth.' + recordName);
- }
- List records = Metadata.Operations.retrieve(Metadata.MetadataType.CustomMetadata, vatFullNames);
- for (Metadata.Metadata record: records) {
- Metadata.CustomMetadata vatRecord = (Metadata.CustomMetadata) record;
- String vatRecordName = vatRecord.fullName.substringAfter('.');
- SPSFOAuth__mdt vatToCopy = VATsByApiName.get(vatRecordName);
- for (Metadata.CustomMetadataValue vatRecValue: vatRecord.values) {
- vatRecValue.value = vatToCopy.get(vatRecValue.field);
- }
- // Add record to the container.
- container.addMetadata(vatRecord);
- }
- // Deploy the container with the new components.
- Id asyncResultId = Metadata.Operations.enqueueDeployment(container, null);
- return null;
- }
- }
VFP




Jean CadieuxPosted Jul 26, 2022, 12:26 AM
Hi Sudheer, what a trove of precious info. I have a question, if we use the named credentials approach, we need a subscription to microsoft Azure? Also, will this approach work to send image data without using that 'Files connect' feature of Salesforce?