I am trying to fire a platform event directly from lightning Web component without using Apex. I choosen the route to call API from JavaScript in Lightning Web component.
My code looks like:-
import { LightningElement } from 'lwc';
const QUERY_URL = 'https://lextraining-dev-ed.my.salesforce.com/services/data/v48.0/sobjects/Low_Ink__e/';
export default class Batch extends LightningElement {
searchKey;
handleSearchKeyChange(event) {
this.searchKey = event.target.value;
}
handleSearchClick() {
let user = {
"Printer_Model__c" : this.searchKey
};
fetch(QUERY_URL, {
method: 'POST',
headers: {
'Content-Type': 'application/json;charset=utf-8'
},
body: JSON.stringify(user)
});
}
}
I have added the remote site setting and CORS in my Org.
Problem :
I am getting below error while making the call.
Access to fetch at 'https://lextraining-dev-ed.my.salesforce.com/services/data/v48.0/sobjects/Low_Ink__e/' from origin 'https://lextraining-dev-ed.lightning.force.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
In the docs, it says Use Lightning Data Service (LDS) to work with data and metadata for Salesforce records. Lightning Data Service is built on top of the public User Interface API, but it only supports a subset of the API. That subset covers many of the typical use cases for working with data. You can’t make calls to Salesforce APIs other than LDS from JavaScript code.
If LDS doesn’t support the object you are looking to use, or if you want to use another Salesforce API, write an Apex class.
So, Does it mean, i must use Apex or is there any alternative available to achieve it?