Get records in Lightning web components without APEX

import { LightningElement, track, api, wire } from 'lwc';
import { getRecord } from 'lightning/uiRecordApi';

const FIELDS_SOLICITUD = [
    'DCABM_obj_solicitudABM__c.DCABM_fld_aplicacionSolicitud__c',
];

export default class DCABM_lwc_form extends LightningElement {

    @api recordId;

    idAplicacion;

    @wire(getRecord, { recordId: '$recordId', fields: FIELDS_SOLICITUD })
    aplicacionSolicitud({ data, error }) {
        if (data) {
            this.idAplicacion= data.fields.DCABM_fld_aplicacionSolicitud__c.value;
        } else if (error) {
            window.console.log("Error");
        }
    }

}

I have a second object that has a relation with the value of “idAplicacion”. How can I get all the objects that have that id as a value, without using APEX?

Answer

It is possible to get records “without apex” or without using the lwc wrappers that leverage the uiRecordApi. If you go through the User Interface API Developer Guide, you can find sample apps (external) and detailed methods and endpoints that can be used to interact with the DB.

There are several things that need to be considered to make this work:

1.- Security, you still need to authenticate the requests.

2.- Forget about using GET due to CSP.

3.- You will most likely need to use an Apex Class to securely handle getting an access token to securely request data.

4.- A connected app is required for the apex Class.

Once you do manage to set yourself up with the aforementioned (securely getting access tokens to auth. your requests), you can use the Fetch Api to get data with and write your SOQL’s from the client (amongst other things).

Also, it is important to understand that not everything is supported if you opt to using this approach.

Sample Applications:

  • Record Viewer
  • Record Viewer Native
    FYI, both these repo’s are archived, however, if you browse through it, you can get a general idea of how they interact with the API.

Bear in mind that this API was mainly created for external web apps to interact with your SF data.

Quick example.
this is my utility method that I use to fetch an access token from my apex class:

import getAccessToken from '@salesforce/apex/UiAuth.getAccessToken';

const getToken = async () => {
    const token = await getAccessToken();
    return token;
   
}

I can use the access token to get data using the available resources documented here.

const queryRecords = async (soql) =>{    
    const token = await getToken();    
    const requestURL =`${document.location.origin}/services/data/v51.0/query?q=${soql}`;    
    const getData = await fetch(requestURL,{
       method : 'POST',
       headers : {
           'Authorization': `Bearer ${token}`
       },    
    });    
    const response = await getData.json();    
    return response; 
}

Attribution
Source : Link , Question Author : Alex M. , Answer Author : glls

Leave a Comment