how can I using rest api in customCode?

I have added 'customCode' to a field in detailviewdefs.php for campaign module.  I want to populate a dropdown with some other values using a custom endpoint.

'customCode' => '<script type="text/javascript" src="custom/js/custom.js?v13225"></script><script>load_linked_vouchers("' . $focus->id . '")</script>'

so it loads my js file, and then runs a function comtained inside the js file, passing the record ID to the function.

Then in my custom.js I have the function

function load_linked_vouchers(id) {
    $.ajax({
        beforeSend: function (request) {
            request.setRequestHeader("OAuth-Token",SUGAR.App.api.getOAuthToken());
},
url: "rest/v10/Campaigns/" + id + "/link/igsm_igsmk_vouc_campaigns_1",
dataType: "json",
type: "GET",
}).done(function (response) {
console.log(response);
})
}

I get "Cannot read property 'api' of undefined" javascript error. I dont know why it SUGAR.App is undefined because it shows in the console so it must be global. Is the customCode ran seperately? how can I put the SUGAR.App into it? Am I doing this wrong or is there a better way?

Parents
  • If you are calling your custom API from within a sugarcrm controller just leverage the internal tools.

    Because you are in the Sugar application you are already authenticated. You don't need to call the oauth again.

    app.api.buildURL builds the url for you adding the path to the rest v10.

    the app.api.call executes the API and returns the data or an error.

        var self = this,
             url = app.api.buildURL('Campaigns/'+id+'/link/igsm_igsmk_vouc_campaigns_1');
         app.api.call('GET',url,params,{
           success: function(data){
             //do something with the return data
           },
           error: function(error){
             app.alert.show('err', {
               level:'error',
               title: app.lang.getAppString('ERR_INTERNAL_ERR_MSG'),
               messages: err
             });
           },
         });

    HTH

    FrancescaS

Reply
  • If you are calling your custom API from within a sugarcrm controller just leverage the internal tools.

    Because you are in the Sugar application you are already authenticated. You don't need to call the oauth again.

    app.api.buildURL builds the url for you adding the path to the rest v10.

    the app.api.call executes the API and returns the data or an error.

        var self = this,
             url = app.api.buildURL('Campaigns/'+id+'/link/igsm_igsmk_vouc_campaigns_1');
         app.api.call('GET',url,params,{
           success: function(data){
             //do something with the return data
           },
           error: function(error){
             app.alert.show('err', {
               level:'error',
               title: app.lang.getAppString('ERR_INTERNAL_ERR_MSG'),
               messages: err
             });
           },
         });

    HTH

    FrancescaS

Children
No Data