How to auto- populate value from a table to a catalogue item field

Priya Singh 2 2
Tera Contributor

Hello Team,

I have a requirement to auto populate a date from a table to my Catalogue Item field how can I achieve it, I am trying to do it via Script Include & onLoad client script however it's not working-

 

onLoad Client script - 
var ga = new GlideAjax('getinchange');
    ga.addParam('sysparm_name', 'getdate');
    ga.addParam('sys_id', 'u_baseline_used.sys_id');
    ga.getXMLAnswer(function(response){
        var dateValue = response;
        g_form.setValue('start_date',dateValue);
    });
 
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Script Include -
getdate: function() {

        var datestart = new GlideRecord('incident');
        if (datestart.get(this.getParameter(sys_id))) {
            datestart.getValue('u_effective_start_date');

        }
        alert("datestart" + test);
        return '';

    },
 
It's not working please advise what I can do make it working ?

 

4 REPLIES 4

Bhavya11
Mega Patron

Hi @Priya Singh 2 2 ,

 

Your script is not missing because you have issue with logic

1. your not suppose to use alert in Server side script meaning Script include 

2 you cannot pass the parameter like this 'sys_id' instead you need to use 'sysparm_id'

 

try to use onchange client script for better results u_baseline_used variable

Client script : 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return; // Don't run on load or if the value is empty
    }

    var sysIdToSend = g_form.getValue(newValue);// hoping this is reference variable 
    var ga = new GlideAjax('getinchange'); // Name of your Script Include
    ga.addParam('sysparm_name', 'getdate'); // Name of the function in your Script Include
    ga.addParam('sysparm_id', sysIdToSend); // Pass the retrieved sys_id as a parameter

    // Call the server and handle the response
    ga.getXMLAnswer(function(response) {
        var dateValue = response;
        if (dateValue) {
            g_form.setValue('start_date', dateValue); // <-- : Change 'start_date' to your actual target field name
        } else {
            g_form.setValue('start_date', ''); // Clear the field if no date was returned
            g_form.showFieldMsg('start_date', 'Could not retrieve a valid start date.', 'error');
        }
    });
}

 

 

Script include : check client callable true

var getinchange = Class.create();
getinchange.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    // This function will be called by sysparm_name='getdate'
    getdate: function() {
        var recordSysId = this.getParameter('sysparm_id'); // Get the sys_id passed from the client

      
        var gr = new GlideRecord('incident'); 
        if (gr.get(recordSysId)) {
            var dateValue = gr.getValue('u_effective_start_date'); 
            return dateValue;
        }

        // If the record is not found or the field is empty, return an empty string
        // or a specific message to indicate failure.
        gs.log('getinchange.getdate: Record with sys_id ' + recordSysId + ' not found or date field empty.');// you can check in logs
        return '';
    },

    type: 'getinchange' /
});

 

 

 

If this information proves useful, kindly mark it as helpful or accepted solution.

Thanks,

BK

 

Ankur Bawiskar
Tera Patron

@Priya Singh 2 2 

why not use auto populate feature in catalog item and no scripting will be required?

Simply create a reference variable pointing to incident table and another date or date/time variable and use this

Auto-populate a variable based on a reference type variable (Utah) 

If my response helped please mark it correct and close the thread so that it benefits future readers.

@Priya Singh 2 2 

Hope you are doing good.

Did my reply answer your question?

If my response helped please mark it correct and close the thread so that it benefits future readers.

@Priya Singh 2 2 

Hope you are doing good.

Did my reply answer your question?

If my response helped please mark it correct and close the thread so that it benefits future readers.