• ADM SMARTIA
  • NEWBIE
  • 0 Points
  • Member since 2017

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 2
    Replies
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'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 do not know why static resource call via URLFOR seems not to be supported in javascript invoke button, whereas the function is available for use.Have anyone used another method to get the url of a static resource in javascript invoke button?