You need to sign in to do that
Don't have an account?
James Buckelew
-- Meta --
Apex http request from user input on LWC
I am trying to allow our Salesforce Users to paste a number into an input field, then let an Apex Class process a named Credential HTTP request - currently getting an error and not sure why.
I will post the code below.
Alternatively, this number they are inputting also exists on the account object but I'm struggling to pass that information through to the Apex class as well. (Prefill the input box with the number, or submit the request onClick with a button)
-- Apex Class --
public class samCall { @AuraEnabled(cacheable=true) public static String samQuery(Integer accountDuns) { HttpRequest req = new HttpRequest(); req.setEndpoint( 'callout:samAPI' + accountDuns ); req.setMethod('GET'); Http http = new Http(); HTTPResponse res = http.send(req); return res.getBody(); } }-- HTML --
<template> <div > <lightning-input type="text" label="Enter DUNS" value={dunsNumber} onchange={handleChange}> </lightning-input> <p>Displaying SAM Information</p> <template if:true={samRecord}> <p> Name: {samRecord} </p> </template> <template if:true={error}> Some error occured. </template> </div> </template>-- JS --
import { LightningElement,wire,track,api } from 'lwc'; import getDuns from '@salesforce/apex/samCall.samQuery'; export default class searchSam extends LightningElement { @api dunsNumber; @track samRecord; @track error; handleChange(event){ const userInput = event.target.value; this.dunsNumber = userInput; } @wire(getDuns,{ accountDuns: '$dunsNumber'}) samData({ error, data }) { if (data) { //console.log('RecordId is'+recordId); this.samRecord = data; this.error = undefined; } else if (error) { //console.log('Error block'); this.error = error; this.samRecord = undefined; } } }
-- Meta --
<?xml version="1.0" encoding="UTF-8"?> <LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata"> <apiVersion>51.0</apiVersion> <isExposed>true</isExposed> <targets> <target>lightning__RecordPage</target> <target>lightning__AppPage</target> </targets> </LightningComponentBundle>
All Answers
Looking at your code, it seems you are using integer parameter in method defined in apex class whereas the lightning-input in your LWC is of type text. So there is type mismatch, you would like to consider both as either text or number. Making this change should work for you.
In case you are going with text/string type both side, consider converting the string parameter to integer first before passing in the callout request.
Hope this answers your question, if so please can you mark this as best answer. Many thanks
It appears to be an issue with the Callout from the names credential. My understanding was the callout would provide the URL from the named credential and then append the user input to the end of that URL, but it is not doing that.