I am trying to bring some data from an API call to the salesforce UI with LWCs but I am struggling with where to start on it. I have a cls call out for it and some HTML added below. This is my first time working with payloads off of the platform or not doing SOQL queries to bring data to the front end. This is why I am a little confused on how to grab the data via JSON and then store the payload into object/objects to then render on the front end.
Controller.cls:
@AuraEnabled
public static Object retrievePhone(String parameter){
Map<String,String> params = (Map<String,String>)JSON.deserialize(parameter, Map<String,String>.class);
String phone = params.get('phone');
String type = params.get('type');
String user = params.get('user');
String address = params.get('address');
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('callout:Endpoint');
request.setHeader('Content-Type', 'application/json');
request.setMethod('POST');
JSONGenerator gen = JSON.createGenerator(true);
gen.writeStartObject();
gen.writeStringField('phone', phone);
gen.writeStringField('type', type);
gen.writeFieldName('user name');
gen.writeStartObject();
gen.writeStringField('user', user);
gen.writeStringField('address', address);
gen.writeEndObject();
gen.writeEndObject();
String body = gen.getAsString();
request.setBody(body);
Map<String, Object> res = new Map<String, Object>();
try{
HttpResponse response = http.seend(request);
if(response.getStatusCode() == 200){
res = (Map<String,Object>) JSON.deserializedUntyped(response.getBody());
res.put('statusCode', response.getStatusCode());
} else{
//code for other responses here
} catch(Exception ex){//exception handling here}
return res;
}
}
Html:
<template>
<template for:each={userNumbers} for:item='userNumber'>
<lightning-layout class="slds-border_bottom" key={userNumber.ID}>
<lighting-layout-item flexibility="auto, no grow">
<div class="custom-box">
<lightning-radio-group name="radioGroup" options={options} value={value} type="radio"></lightning-radio-group>
</div>
</lighting-layout-item>
<lighting-layout-item flexibility="auto" padding="around-small">
<div class="custom-box">
<p>{userNumber.lastName}</p>
<p>{userNumber.address}</p>
<p>{userNumber.fullPhoneNumber}</p>
</div>
</lighting-layout-item>
</lightning-layout>
</template>
</template>
JavaScript:
nothing at the moment besides imports and get options function for radio button