Skip to main content

What is the best way to make http external call? FYI: external site is already configured in remote site setting. Any example of making call and creating LWC  .html file usable object would helpful? 

4 réponses
  1. 24 févr. 2023, 12:35

    Yes, you will need to convert the external API JSON response to a format that can be used by your LWC component. The most common way to do this is to use the built-in JSON.parse() method to parse the JSON response into a JavaScript object.

    To make an external API call from your LWC component, you can use the built-in Lightning web service (lwc-wire-service-ui). Here is an example of how to use this service:

    1. Create a new LWC component: Go to Setup > Developer Console, click on "File" > "New" > "Lightning Web Component". Give the component a name, and select "HTML" as the file type.
    2. Add a JavaScript controller file: In the Developer Console, click on "File" > "New" > "JavaScript". Give the file a name, and add the following code:

    import { LightningElement, wire } from 'lwc';

    import { getRecord } from 'lightning/uiRecordApi';

     

    export default class MyComponent extends LightningElement {

        @wire(getRecord, { recordId: '001XXXXXXXXXXXXXXX', fields: ['Account.Name'] })

        account;

    }

     

    This code uses the getRecord method from the lwc-wire-service-ui to make a call to the Salesforce API and retrieve the name of an Account record with ID '001XXXXXXXXXXXXXXX'.

     

    1. Add an HTML template file: In the Developer Console, go back to the HTML file for your LWC component, and add the following code to display the Account name:

    <template>

        <p>{account.data.fields.Name.value}</p>

    </template>

     

    This code displays the name of the Account record that was retrieved by the JavaScript controller.

     

    1. Test your component: Save your component and add it to a Lightning page or app to test it out.

    I hope this example helps! Let me know if you have any further questions.

0/9000