Hello, can anyone help me with getting the model I'm working in now? I have a button that should be clicked to execute a webhook, but I can't get the model in which the button is clicked. i'm new to js, so it's very difficult to understand
Odoo is the world's easiest all-in-one management software.
It includes hundreds of business apps:
- CRM
- e-Commerce
- Kế toán
- Tồn kho
- PoS
- Project
- MRP
Câu hỏi này đã bị gắn cờ
usually model is inside this keyword. That depends on where you call it. If nothing works the last option is get the url of the current page and get the model from it.
var url = window.location.href;
This is how odoo field get the current model
It seem that you trying to create a new field extend from some field right, i don't think what you are coding seem fit to odoo in general, window.makeCall is not the way to define some method in Odoo OWL or Legacy
window.makeCall = function(event) {
event.preventDefault();
var $button = $(event.currentTarget);
var modelName = this.dataset.model;
var recordId = $button.attr('data-record-id');
var phone = $button.prev('input.o_input').val();
console.log('Model Name:', modelName);
// Determine context and set data accordingly
var data = {
phone: phone,
contact_id: modelName === 'res.partner' ? recordId : undefined,
lead_id: modelName === 'crm.lead' ? recordId : undefined,
};
// Example of fetching additional data if needed
if (modelName === 'crm.lead') {
odoo.define('model.func', function(require) {
var rpc = require('web.rpc');
rpc.query({
model: modelName,
method: 'read',
args: [[recordId], ['partner_id']],
}).then(function(records) {
var record = records[0];
if (record.partner_id) {
data.contact_id = record.partner_id[0];
}
// Now, make the AJAX call with the complete data object
$.ajax({
url: url,
type: "POST",
contentType: "application/json",
data: JSON.stringify(data),
success: function(response) {
console.log("Call initiated successfully", response);
},
error: function(error) {
console.error("Error initiating call", error);
}
});
});
});
} else {
// If not crm.lead or additional data not needed, make the call directly
$.ajax({
url: url,
type: "POST",
contentType: "application/json",
data: JSON.stringify(data),
success: function(response) {
console.log("Call initiated successfully", response);
},
error: function(error) {
console.error("Error initiating call", error);
}
});
}
};
<?xml version="1.0" encoding="UTF-8"?>
<templates xml:space="preserve">
<t t-name="web.PhoneField" owl="1">
<div class="o_phone_content d-inline-flex w-100">
<t t-if="props.readonly">
<a t-if="props.value" class="o_form_uri" t-att-href="phoneHref" t-esc="props.value"/>
</t>
<t t-else="">
<input
class="o_input"
t-att-id="props.id"
type="tel"
autocomplete="off"
t-att-placeholder="props.placeholder"
t-ref="input"
/>
</t>
</div>
</t>
<t t-name="web.FormPhoneField" t-inherit="web.PhoneField" t-inherit-mode="primary">
<xpath expr="//input" position="after">
<a
t-if="props.value"
href="#"
onclick="makeCall(event)"
class="o_phone_form_link ms-3 d-inline-flex align-items-center"
t-att-data-model-name="props.modelName"
t-att-data-record-id="props.recordId"
>
<i class="fa fa-phone"></i><small class="fw-bold ms-1">Call</small>
</a>
</xpath>
</t>
</templates>
Bạn có hứng thú với cuộc thảo luận không? Đừng chỉ đọc, hãy tham gia nhé!
Tạo tài khoản ngay hôm nay để tận hưởng các tính năng độc đáo và tham gia cộng đồng tuyệt vời của chúng tôi!
Đăng kýBài viết liên quan | Trả lời | Lượt xem | Hoạt động | |
---|---|---|---|---|
|
1
thg 4 24
|
1871 | ||
|
0
thg 1 24
|
1508 | ||
|
1
thg 9 23
|
2916 | ||
|
2
thg 4 23
|
5015 | ||
|
2
thg 1 23
|
7536 |