AnsweredAssumed Answered

Missing API file error on AJAX call

Question asked by Richard Coleman on Jan 5, 2016
Latest reply on Jan 5, 2016 by Richard Coleman

Hi,

 

I've been struggling with the v10 AJAX framework in Sugar 7.6 and I've got it nearly there I think.

 

Having tackled authentication, and a number of other problems, I'm stuck with it stating my GET request has a "Missing API File" with a 400 error.

 

Looking in the console, the URL looks right to me by going to: http://%MY LOCAL SITE%/rest/v10/Accounts/VatCheck?vatReg=123&countryCode=GB

 

The full object returned by the error in the AJAX is:

 

readyState: 4,

responseText: "{"error":"unknown_exception","error_message":"Missing API file."}", status: 400, statusText: "Bad Request"}

 

My code is as follows:

 

custom/modules/Accounts/clients/base/views/record.js

({
    // Extends from RecordView
    extendsFrom: 'AccountsRecordView',


    initialize: function(options) {
        this._super('initialize', [options]);
  console.log('Custom Record JS in use.');
    },

  saveClicked: function(callback) {
  console.log('save was clicked');


  var vatRegNumber = document.getElementsByName('vat_reg_number_c')[0].value;        
  var countryCodeValue = document.getElementsByName('country_code_c')[0].value;


  console.log("Preparing the Ajax Call");


  $.ajax
  ({
  beforeSend: function (request)
  {
  request.setRequestHeader("OAuth-Token", SUGAR.App.api.getOAuthToken());
  },
  url: "rest/v10/Accounts/VatCheck",
  type: "GET",
  contentType: "application/json",
  data: { "vatReg" : vatRegNumber , "countryCode" : countryCodeValue },
  dataType: "json",
  success: function(data)
  {
  console.log("AJAX Call successful");
  console.log(data);
  if (data.success == 1)
  {
  console.log("Result succeeded");
  }
  else
  {
  app.alert.show('test', {
  level: 'error',
  title: 'Error:',
  messages: data.error,
  });
  }
  },
  error: function (xhr, ajaxOptions, thrownError) {
  console.log(xhr);
  console.log(ajaxOptions);
  console.log(thrownError);
  }
  });
    },
})

 

custom/modules/Accounts/clients/base/api/VatCheck.php

<?php


require_once 'include/api/SugarApi.php';

class VatCheck extends SugarApi
{

    public function registerApiRest() 
  {
        return array(
            'VatCheck' => array(
                'reqType' => 'GET',
                'path' => array('Accounts', 'VatCheck'),
                'pathVars' => array('vatReg', 'countryCode'),
                'method' => 'checkVatCode',
                'shortHelp' => 'Returns whether the VAT code entered is valid',
                'longHelp' => '',
            ),
        );
    }



  public function checkVatCode($api,$args)
  {
  global $db;


  $sql = "call AA_CHECK_VAT_NUMBER_S ('?','?')";


  $params = array($_REQUEST["vatReg"], $_REQUEST["countryCode"]);
   
  $result = $db->pQuery($sql, $params);


  $row = $db->fetchByAssoc($result);


  if ($row)
  {
  $json = array( 'success' => $row['LS_Success'], 'code' => $row['PS_VAT_Number'], 'error' => $row['errorMessage']);


  return json_encode($json);
  }
  else
  {
  return $db->lastDbError();
  }
  }
}
?>

 

I hope someone can point out where I've likely just done something silly here. I've had to make some changes for where it was working in 6.5 using a custom entry point.

Outcomes