Last active
December 13, 2020 22:29
-
-
Save Greyeye/0225e317c7c8c6579ba871671c2674e3 to your computer and use it in GitHub Desktop.
salesforce remote api call template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public without sharing class CustomController { | |
// this value cannot be read inside the testing, make sure you insert temporary value | |
public static final String endpoint = Custom_Setting__c.getOrgDefaults().custom_endpoint__c; | |
public static String getAuthHeader() { | |
Auth.JWT jwt = new Auth.JWT(); | |
jwt.setSub(UserInfo.getUserId()); | |
Map<String, String> claims = new Map<String, String>(); | |
claims.put('name', userInfo.getusername()); | |
jwt.setAdditionalClaims(claims); | |
// Sign the JWT token using JWS | |
Auth.JWS jws = new Auth.JWS(jwt, 'CERTIFICATE_NAME_IN_SF'); | |
return jws.getCompactSerialization(); | |
} | |
public static HttpResponse callRemoteAPI(String targetId) { | |
HttpRequest req = new HttpRequest(); | |
req.setEndpoint(endpoint); | |
req.setMethod('GET'); | |
req.setTimeout(120000); | |
req.setHeader('Content-Type', 'application/json'); | |
Map<String, Object> requestBody = new Map<String, Object>(); | |
requestBody.put('target', targetId); | |
req.setBody(JSON.serialize(requestBody)); | |
// Add the JWT payload to the request header | |
req.setHeader('Authorization', getAuthHeader()); | |
return new Http().send(req); | |
} | |
@AuraEnabled(Cacheable=false) | |
public static Map<String, Object> getObjectDetails(Map<String, Object> _object) { | |
HttpResponse response = callRemoteAPI((String) _object.get('Name')); | |
Map<String, Object> returnResponse = new Map<String, Object>(); | |
returnResponse.put('data', response.getBody()); | |
returnResponse.put('error', null); | |
return returnResponse; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@IsTest | |
public with sharing class CustomControllerTest { | |
private static List<Map<String, Object>> listObjtoStringObj(List<Object> source) { | |
if (source == null) { | |
return new List<Map<String, Object>>(); | |
} | |
List<Map<String, Object>> targetObj = new List<Map<String, Object>>(); | |
for (Object a : source) { | |
targetObj.add((Map<String, Object>) a); | |
} | |
return targetObj; | |
} | |
class HttpMock implements HttpCalloutMock { | |
public Integer statusCode = 200; | |
public String responseBody = '{"mockatt":"mockvalue"}'; | |
public HttpResponse respond(HttpRequest req) { | |
HttpResponse res = new HttpResponse(); | |
res.setHeader('Content-Type', 'application/json'); | |
res.setBody(responseBody); | |
res.setStatusCode(statusCode); | |
return res; | |
} | |
} | |
public static testMethod void testGetObjectDetails() { | |
// setup dummy url for test endpoint, as testing environment cannot see the configured value | |
upsert new Custom_Setting__c(custom_endpoint__c = 'dummy.com.au'); | |
Test.startTest(); | |
Test.setMock(HttpCalloutMock.class, new LambdaHttpMockForGetEmailAccounts()); | |
// TODO: create _object | |
Map<String, Object> returnResponse = CpanelController.getEmailAccounts(_object); | |
Test.stopTest(); | |
List<Object> data = (List<Object>) returnResponse.get('data'); | |
System.assert(data.size() > 0); | |
List<Map<String, Object>> target = listObjtoStringObj( | |
(List<Object>) returnResponse.get('data') | |
); | |
System.assert(target[0].get('email') == 'test2@testing122.com.au'); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { LightningElement, api, wire } from 'lwc'; | |
import { getRecord } from 'lightning/uiRecordApi'; | |
import getObjectDetails from '@salesforce/apex/CustomController.getObjectDetails'; | |
const FIELDS = ['Object.Name']; | |
export default class getObjectDetails extends LightningElement { | |
// if this LWC is embedded directly inside an object layout, recordId will be the object's Id. | |
// however, if you've got custom wrapper (eg modal) you will have to feed recordId to this LWC. | |
@api recordId; | |
_object = undefined; | |
_objectDetails = undefined; | |
@wire(getRecord, { recordId: '$recordId', fields: FIELDS }) | |
async handleObjectInfo({ data, error }) { | |
if (data) { | |
if (data.fields.Name.value) { | |
this._object = { Name: data.fields.Name.value }; | |
this._objectDetails = await getObjectDetails(this._object); | |
} | |
} else if (error) { | |
console.log(error); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment