This question has been flagged
2 Replies
2560 Views

Hello,

I am using Odoo 15.

I am trying to call do_action from JavaScript. In my do_action call, I need to return an action and a view, but I only know the name of my view.

I tried to do the below rpc call to find my view_id, but it isn't working.

rpc.query({
// Get view id
    model:'ir.model.data',
method:'xmlid_to_res_model_res_id',
args: ['my_module.'my_view_name], // View id goes here
}).then(function(data){
 self.do_action({
name:'view name',
type: 'ir.actions.act_window',
res_model: 'my_model',
view_mode: 'form',
views: [[data[1], 'form']],
})

});

But this throws this error:

*****AttributeError: type object 'ir.model.data' has no attribute 'xmlid_to_res_model_res_id'*****

Please let me know the correct way to make rpc calls in Odoo 15.

Thanks.









Avatar
Discard
Author Best Answer

Salaam Muhammad Bhai,

Yes it was very helpful.

I finally used this code (with some changes) :

rpc.query({
model: 'ir.model.data',
method: 'search',
args: [
[
['name', '=', 'name_of_my_view']
]
],
}).then(function(view_ids) {
if (view_ids.length > 0) {
var view_id = view_ids['res_id'];
// Do something with the view_id
self.do_action({
name: 'name_of_my_view',
type: 'ir.actions.act_window',
res_model: 'name_of_my_model',
view_mode: 'form',
views: [
[view_id, 'form']
],
})
} else {
console.log("View not found");
}
});

Thanks,

-Varun

Avatar
Discard
Author

Nope, I actually used your code itself!

I discarded the above code written by me.
But this website isn't allowing me to edit it due to lack of KARMA!

rpc.query({
model: 'ir.ui.view',
method: 'search',
args: [
[
['name', '=', 'VIEWNAME']
],
],
}).then(function(view_ids) {
if (view_ids.length > 0) {
var view_id = view_ids[0];
// Do something with the view_id
self.do_action({
name: 'VIEWNAME',
type: 'ir.actions.act_window',
res_model: 'MODELNAME',
view_mode: 'form',
views: [
[view_id, 'form']
],
})
} else {
console.log("View not found");
}
});

Best Answer

To find the view_id for a specific view using an RPC call from JavaScript in Odoo, you can use the following code:


Copy code

rpc.query({

    model: 'ir.ui.view',

    method: 'search',

    args: [[['name', '=', 'view_name']]],

}).then(function (view_ids) {

    if (view_ids.length > 0) {

        var view_id = view_ids[0];

        // Do something with the view_id

    } else {

        console.log("View not found");

    }

});



In this code, replace 'view_name' with the name of the view for which you want to find the view_id. The code will perform a search for a view with a name matching 'view_name', and if a match is found, it will retrieve the view_id for the first view in the search results.


I hope this helps! Let me know if you have any further questions.

Avatar
Discard