跳至內容
選單
此問題已被標幟
3 回覆
3342 瀏覽次數

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


頭像
捨棄
最佳答案

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

https://github.com/odoo/odoo/blob/849ece86a6cf49c0a9249ed910aa52add197f431/addons/web/static/src/views/fields/binary/binary_field.js#L43

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>

相關帖文 回覆 瀏覽次數 活動
1
4月 24
1810
0
1月 24
1478
1
9月 23
2865
2
4月 23
4945
2
1月 23
7473