Skip to Content
Menu
This question has been flagged
1 Reply
1989 Views

i am trying to show records count in menus for that i am trying below codes.

please help me guys need to return value instead empty array

my xml code 

using below xml code i am calling javascript function for getting count by passing action id






  • my js code:
    getRecordsCount: function (action_id) {
    var rec_count = [];
    console.log("getRecordsCount is ");
    console.log(action_id);
    ajax.jsonRpc("/get_records_count", 'call', {'action_id': action_id},
    {'async': false}).then(function (data) {
    rec_count.push(data);
    console.log(data['count'])
    });
    console.log(rec_count)
    return rec_count


    },
    my controller file .py file
    @http.route('/get_records_count', type='json', auth='public')
    def get_records_count(self,**kw):
    count = 0
    vals = {
    "count": count
    }
    action_id = kw.get('action_id')
    data = {'status': 200, 'response': vals, 'message': 'records count Returned'}
    action_obj = request.env['ir.actions.act_window'].search([('id', '=', action_id)])
    logging.info("action_obj.xml_id %s" % action_obj.xml_id)
    if action_obj:
    # and (action_obj.xml_id == 'web_advanced_search_sale_unit.menu_sale_all_unit' or action_obj.xml_id == 'web_advanced_search_sale_unit.menu_sale_my_units'):
    domain = action_obj.domain if action_obj.domain else []
    result = isinstance(domain, list)

    if result:
    domain = []
    logging.info("domain %s" % domain)
    count = request.env[action_obj.res_model].search_count(domain)
    logging.info("count %s" % count)
    else:
    if len(domain) > 0:
    logging.info("domain in ast %s" % domain)
    try:
    domain = ast.literal_eval(domain)
    count = request.env[action_obj.res_model].search_count(domain)
    except Exception as e:
    domain = []
    count = request.env[action_obj.res_model].search_count(domain)

    else:
    domain = []
    count = request.env[action_obj.res_model].search_count(domain)

    logging.info("count %s" % count)
    vals = {
    "count": count
    }
    data = {'status': 200, 'response': vals, 'message': 'records count Returned'}
    return vals


    Avatar
    Discard
    Best Answer

    Hello paidy kumar,

     
    Please use the JS global variable like these,
    window.rec_count = []
    getRecordsCount: function (action_id) {
    console.log ("getRecordsCount is");
    console.log (action_id);
    ajax.jsonRpc ("/ get_records_count", 'call', {'action_id': action_id},
    {'async': false}). then (function (data) {
    window.rec_count.push (data);
    console.log (data ['count'])
    });
    console.log (window.rec_count)
    return window.rec_count
    },


    Thanks & Regards,
    Email: odoo@aktivsoftware.com
    Skype: kalpeshmaheshwari

    Avatar
    Discard
    Author

    still empty array coming in logs first it is printing "getRecordsCount is" 2nd "action_id" and third it is printing before return statement log statement and finally inside ajax call log statement "data ['count']"

    Author

    i tried below code getRecordsCount: function (action_id) {
    var i = 0
    if(i>0){
    window.rec_count.pop();
    }
    console.log("getRecordsCount is ");
    console.log(action_id);
    console.log("2window.rec count");
    console.log(window.rec_count);
    return ajax.jsonRpc("/get_records_count", 'call', {'action_id': action_id},{'async': true})
    },

    it is returning promise object how can i get actual object please help

    Author

    here i am getting promise object

    Related Posts Replies Views Activity
    4
    May 24
    10073
    1
    Apr 24
    1563
    0
    Nov 23
    525
    1
    Sep 23
    566
    2
    Aug 23
    2425