1

I am trying to call Composite Request from LWC class (.js) using fetch function. But it is not working.

Actually i wanted to upload 10MB File without Parent which is not possible right now in LWC. So i have tried composite request but it is giving below error. But this REST API working perfect from REST Client like Postman

enter image description here

3

You cannot call any Salesforce API other than LDS from JavaScript in LWC. The documentation Call APIs from JavaScript specifically mentions this:

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.

Your option here is to write an Apex class, for details on this, you can refer to Call APIs from Apex section in the documentation.

  • Unfortunately Apex is not an option for uploading 10MB files as this would bust the heap limit. – Charles T Mar 7 at 23:26
  • Not ideal for sure but there’s no other option. To address this size there definitely needs to be additional consideration when using Apex. – Jayant Das Mar 7 at 23:35
  • @JayantDas We cannot call REST API due to CORS But using Proxy we can do and I have did it for create sample record Refer stackoverflow.com/questions/43262121/… but may be this proxy is not working with Multipart request – Mekyush Jariwala Mar 8 at 7:39
  • Per docs it says you will need to add CSP as trusted site and that you cannot call any SF APIs, are you saying that you have been able to call other SF APIs but not this one? – Jayant Das Mar 8 at 13:15
  • @MekyushJariwala If you did the sample, then that is an example using LDS, the docs mentions (excerpt as in the answer), you cannot make a call to any other SF API from JS other than LDS. – Jayant Das Mar 8 at 15:01
3

It is clearly mentioned in the documentation that:

By default, you can’t make WebSocket connections or calls to third-party APIs from JavaScript code. To do so, add a remote site as a CSP Trusted Site.

I created this component to call a 3rd party API, and I am able to see the output:

import { LightningElement } from 'lwc';

export default class CallAPI extends LightningElement {

    connectedCallback() {
        const xhr = new XMLHttpRequest();

        const number = 5;

        const url = 'http://api.icndb.com/jokes/random/' + number;

        xhr.open('GET', url, true);

        xhr.onload = function() {
            if(this.status === 200) {
                const response = JSON.parse(this.responseText);

                let output = '<ul class=\'slds-list--dotted\'>';

                if(response.type === 'success') {
                    response.value.forEach(function(joke){
                        output += `<li>${joke.joke}</li>`;
                    });
                }

                output += '<ul>';
                console.log(output);
            }
        };

        xhr.send();
    }
}

enter image description here

1

I don't think LWC allows you to make direct calls to arbitrary Salesforce APIs.

However since it does give you access to UI API, you may be able to use its Create Record function to create files: https://developer.salesforce.com/docs/component-library/documentation/lwc/lwc.reference_create_record

  • this wont allow you to upload more then 3MB file – Mekyush Jariwala Mar 8 at 7:44
  • Interesting. Is this limit of UI API documented anywhere? – Charles T Mar 8 at 12:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.