function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
ADM SMARTIAADM 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?

ADM SMARTIAADM SMARTIA

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:

function callRemoteFunction(connection, sessionId, endpoint, method, data, onSuccess, onFailure) {
  function getUrl(endpoint) { var l = location; return l.protocol + '//' + l.hostname + (l.port ? ':' + l.port : '') + '/services/apexrest' + endpoint; }
  connection.remoteFunction({
    url: getUrl(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: 'Erro desconhecido' }];
      console.error(resp);
      if (onFailure) {
        var msgMacher = RegExp(/(?:FIELD_CUSTOM_VALIDATION_EXCEPTION),\s?(.+?])/g);
        for (var i = 0; i < resp.length; i++) {
          var msg = resp[i].message.replace(/\nClass.*|^\n/g, ''), msgMatch = msgMacher.exec(msg);
          if (msgMatch) msg = msgMatch[1];
          resp[i].message = msg;
        }
        onFailure(resp);
      }
    }
  });
}

Example javascript button:

{!REQUIRESCRIPT("/soap/ajax/38.0/connection.js")}
{!REQUIRESCRIPT('/resource/my_js_static_file')}

(function() {
    callRemoteFunction(
        sforce.connection,
        '{!$Api.Session_Id}',
        '/service/endpoint',
        'POST',
        { id: '{!Lead.Id}' },
        (resp) => console.log('success'),
        (resp) => console.error('ERROR!!!', resp)
    );      
})()