how to get a model in js for odoo 16

14 Views Asked by At

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

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

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);
            }
        });
    }
};

I tried everything I could think of, looking for a model through this.model, props.model, this.dataset.model, etc.

0

There are 0 best solutions below