Ir al contenido
Menú
Se marcó esta pregunta
3 Respuestas
3665 Vistas

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


Avatar
Descartar
Mejor respuesta

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;
Avatar
Descartar
Mejor respuesta

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

Avatar
Descartar
Autor Mejor respuesta
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);
}
});
}
};


Avatar
Descartar
Autor

<?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>

Publicaciones relacionadas Respuestas Vistas Actividad
1
abr 24
2076
0
ene 24
1662
1
sept 23
3082
2
abr 23
5231
2
ene 23
7739