You need to sign in to do that
Don't have an account?
ADM SMARTIA
Calling @RestResource from static resource
I'm trying to call an APEX @RestResource from a Javascript in a static resource file. My static resource has the code below:
function callRestService(endpoint, method, sessionId, data, onSuccess, onFailure) { function getServiceUrl(endpoint) { var l = location, host = l.protocol + '//' + l.hostname + (l.port ? ':' + l.port: ''); return host + '/services/apexrest' + endpoint; } sforce.connection.remoteFunction({ url: getServiceUrl(endpoint), requestHeaders: { 'Authorization': 'Bearer ' + sessionId, 'Content-Type': 'application/json'}, requestData: JSON.stringify(data), method: method, onSuccess : (resp) => { if(onSuccess) { if(resp) resp = JSON.parse(resp); onSuccess(resp); } }, onFailure: (resp) => { resp = resp ? JSON.parse(resp) : [{ message: 'Unknown error' }]; console.error(resp); if(onFailure) { for(var i = 0; i < resp.length; i++) { var msg = resp[i].message.match(/(?:FIELD_CUSTOM_VALIDATION_EXCEPTION),\s?(.+?])/g); if(msg[0]) resp[i].message = msg[0]; } onFailure(resp); } } }); }
This code works fine when I put it inside a custom javascript button, but when I try to use it from a static resource I receive a http 404 error because the http call is redirected to https://xxxxx--desenv.cs99.my.salesforce.com/services/proxy by the sforce.connection.remoteFunction API.
Is it possible to call an APEX REST resource from a javascript inside a static resource?
I finally fix the issue. I created a static javascript file with my JS code and imported in a Javascript button.
My static resource function:
Example javascript button: