AnsweredAssumed Answered

Authentication error when using REST v10

Question asked by Gaelle Fernandez on Aug 1, 2014
Latest reply on Feb 20, 2015 by Gustav Lindström
Hello everyone!

i would like to create a custom button on Accounts module to create a new record of a custom module i've made. I want to pass two datas to the custom module's create view : the id of the button clicked on Account module and the Account's id (to link them both).

I've followed the steps of insightful to create a custom button on Accounts module to lead to a new view.

So, i've added my custom button in custom/modules/Accounts/clients/base/views/record/record.php (and in language file too). it works, my button appears.

I've extended record view of the Account to say "hey, if you click on custom button, launch the custom rest api i've made!"
so in custom/modules/Accounts/clients/base/views/record/record.js i have this :

({extendsFrom: 'RecordView',

initialize: function(options) {

    this._super('initialize', [options]);
    this.context.on('button:create_cplus_button:click', this.navigateCPlusCreate, this);
},
navigateCPlusCreate: function() {  var societyid = this.model.get('id');
     var societyname = this.model.get('name');
     var type = 'cplus';
     app.api.call('GET', app.api.buildURL('cplus_ContactsPlus/create_cplus/datas=' + societyid +'&' +type), null, {
          success: function(data) {
               app.drawer.open({layout:'create-cplus',context:{infos: data}});
          },
          error: function(error) {
              console.log("didn't succeed");
               app.alert.show("server-error", {
                    level: 'error',
                    messages: 'ERR_GENERIC_SERVER_ERROR',
                    autoClose: false
                });
                app.error.handleHttpError(error);
           }
     });
}

})

i've added custom/modules/cplus_ContactsPlus/clients/base/api/CustomCPlusApi.php
which contains the code below :

<?phpif(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
 
require_once('data/BeanFactory.php');
 
class CustomCplusApi extends SugarApi
{
        public function registerApiRest()
        {
                return array(
                        'create_cplus' => array(
                                'reqType' => 'GET',
                                'path' => array('cplus_ContactsPlus', 'create_cplus','?'),
                                'pathVars' => array('module','','data'),//cplus/accountid/type/create_cplus
                                'method' => 'createCplus',
                                'shortHelp' => 'call the custom views',
                                'longHelp' => '',
                        ),
                );
        }
 
        public function createCplus($api, $args)
        {
                // get the Account
                $record = BeanFactory::getBean($args['module']);
 
                // check ACL
               /* if(!$record->ACLAccess('view'))
                {
                        return;
                }*/

   
    $infos = substr($args['data'],5);
    $info = explode('&',$infos);
    $type = $info[1];
    $accid = $info[0];
               
 $datas = array();
    $datas['accid'] =  $accid;
    $datas['type'] = $type;
                return $datas;
        }
}
 
?>

Now, to call a custom layout, i've made custom/modules/cplus_ContactsPlus/clients/base/layouts/create-cplus/create-cplus.php with simply this :

<?php$viewdefs['cplus_ContactsPlus']['base']['layout']['create-cplus'] = array(
        'components' =>
        array(
                array(
                        'view' => 'create-cplus',
                ),
        ),
        'type' => 'simple',
        'name' => 'base',
        'span' => 12,
);
?>

Then, added the js and hbs files for the custom view in
custom/modules/cplus_ContactsPlus/clients/base/views/create-cplus :
create-cplus.js

({        extendsFrom: 'RecordView',
        className: 'count-contats',
        events: {
                'click [name=close_button]': 'closeDrawer'
        },
 
        initialize: function(options) {
                app.view.invokeParent(this, {
                        type: 'view',
                        name: 'record',
                        method: 'initialize',
                        args: [options]
                });
        },
 
        _renderHtml: function () {
                this.infos = this.context.get('infos');
                app.view.invokeParent(this, {
                        type : 'view',
                        name : 'record',
                        method: '_renderHtml'
                });
        },
 
        closeDrawer: function() {
                app.drawer.close();
        }
})

and create-cplus.hbs :
<div class="row">        <div class="span4 offset1">
                <h3>Contacts Count</h3>
                {{#each infos}}
                  <p>{{@key}}: {{this}}</p>
                {{else}}
                  <p>There are no Contacts in this Account.</p>
                {{/each}}
                <br/>
                <a class="btn cancel" href="javascript:void(0);" track="click:close_button" name="close_button">Close</a>
        </div>
</div>


Then a QRR.

When I click on my button, i've got a 500 error and my console.log that i've set in account's record.js

I've tested like that : <sugar>/rest/v10/cplus_ContactsPlus/create_cplus/datas=3af80257-f39c-a218-ce8c-5369e32b52b8&cplus
And i got a 
{"error":"need_login","error_message":"No valid authentication for user."}

What shoul i do? How can i sort of this problem?

it is quite urgent, i'm tired of looking for sugar' helps and docs...

Thanks for the time you're giving me.

Gaëlle

Outcomes