nwara97
nwara97

Reputation: 17

reading odoo16 record information using javascript

I am new to odoo and web in general, I have this issue I have a template that provides a drop down list with initiatives to choose from, I need to view some information about the selected initiative in the same template, I figured that I need to use java script this is part of my template

<template id="initiative_dropdown_template" name="Initiative Dropdown">
        <t t-call="website.layout">
            <div class="container mt-4">

                <form action="/initiative/proposal" method="post"
                      id="request-form"
                      class="m-5 px-5"
                      role="form">

                    <select class="form-control" id="initiative_dropdown" name="initiative_id">
                        <option value="">Select an Initiative</option>
                        <t t-foreach="initiatives" t-as="initiative">
                            <option t-att-value="initiative.id">
                                <t t-esc="initiative.name"/>
                            </option>
                        </t>
                    </select>
                      <!-- Display the selected option value -->
                <p id="selected_initiative_id">
                    Selected Initiative ID: <span id="initiative_id_placeholder"/>
                </p>

and I have this js method

// Add the script below this comment
    $(document).ready(function () {
        $('#initiative_dropdown').on('change', function () {
            var selectedInitiativeId = $(this).val();
            console.log('Selected Initiative ID:', selectedInitiativeId);
            $('#initiative_id_placeholder').text(selectedInitiativeId);

        //
        // Perform an RPC call to fetch initiative details
        console.log('self:', self);
        const rpc = require('web.rpc')
        console.log('Before RPC Call');
        rpc.query({

            model: 'initiative.initiative',
            method: 'search_read',
            args: [['id', '=', selectedInitiativeId]]
        }).then(data => {
        console.log('RPC Call Successful. Initiative Details:', data);
                
            })

         console.log('RPC Call Successful. Initiative Details:');

        });
    });

the console.log('Selected Initiative ID:', selectedInitiativeId); is viewing the right selected id but the rpc query is not working any idea how should i achieve this

I only tried using rpc call, but I don't know if there is any other methods to get record by id in java script

Upvotes: 0

Views: 380

Answers (1)

Ahrimann Steiner
Ahrimann Steiner

Reputation: 1314

You should parse your selected id to Integer:

   rpc.query({
       model: 'initiative.initiative',
       method: 'search_read',
       args: [['id', '=', parseInt(selectedInitiativeId)]]
            })

Upvotes: 0

Related Questions