I need to auto populate one catalog item field from another table how can I achieve this requirement

Priya Singh 2 2
Tera Contributor

I am trying to achieve this requirement by script include & client script:

Script Include - 

Table from which I am trying to get date value - u_test_change

Field which is having date stored in "u_test_change" table - u_effective_date

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

    getChangeDate: function(        var userID = this.getParameter('sysparm_user_id');
        var grDate = new GlideRecord('u_test_change');
        grDategr.addQuery('u_requested_for',userID);
        grDate.orderByDesc('sys_created_on');
        grDate.query();
        if(grDate.next()){
           
            grDate.getValue('u_effective_date');
        }
        return '';
        },
   

    type: 'PopulateStartDateUtil'
});
 
Client Script - on Load 
 
Catalog Item Field - start_date
 
function onLoad() {
    //Type appropriate comment here, and begin script below
    //Calling the Script Include - getjmlchange

   
    var ga = new GlideAjax('PopulateStartDateUtil');
    ga.addParam('sysparm_name', 'getChangeDate');
    ga.addParam('sysparm_user_id',userID);
    ga.getXMLAnswer(function(response){
        var dateValue = response;
        if (dateValue){
        g_form.setValue('start_date',dateValue);
        }
    });

   

 }
But this is not auto populating date on catalog item, please suggest/advise what I am doing wrong ?
 
Thanks,
XXXX
1 ACCEPTED SOLUTION

Deepak Shaerma
Kilo Sage

@Priya Singh 2 2 

Use this script , this is working for me :

Script Include:

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

    getChangeDate: function() {
        var userID = this.getParameter('sysparm_user_id');
        var grDate = new GlideRecord('u_test_change');
        grDate.addQuery('u_user', userID); // Assuming there's a reference to user
        grDate.query();
        if (grDate.next()) {
            return grDate.getValue('u_effective_date');
        }
        return '';
    },

    type: 'PopulateStartDateUtil'
});



Catalog Client Script:

function onLoad() {
    var userID = g_user.userID;

    var ga = new GlideAjax('PopulateStartDateUtil');
    ga.addParam('sysparm_name', 'getChangeDate');
    ga.addParam('sysparm_user_id', userID);
    ga.getXMLAnswer(function(response) {
        var dateValue = response;
        if (dateValue) {
            g_form.setValue('start_date', dateValue);
        }
    });
}

Please Mark this Helpful and Accepted Solution. If this Helps you to understand. This will help both the community and me..
- Keep Learning ‌‌
Thanks & Regards 
Deepak Sharma 



View solution in original post

7 REPLIES 7

Arun_Manoj
Mega Sage

Hi @Priya Singh 2 2 ,

 

Please follow below script

 

Script Include:

 

 

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

getChangeDate: function() {
var userID = this.getParameter('sysparm_user_id');
var grDate = new GlideRecord('u_test_change');
grDate.addQuery('u_requested_for', userID);
grDate.orderByDesc('sys_created_on');
grDate.query();

if (grDate.next()) {
return grDate.getValue('u_effective_date'); // Return the value properly
}
return '';
},

type: 'PopulateStartDateUtil'
});

 

 

Client Script:

function onLoad() {
var userID = g_user.userID; // Get current user ID

var ga = new GlideAjax('PopulateStartDateUtil');
ga.addParam('sysparm_name', 'getChangeDate');
ga.addParam('sysparm_user_id', userID);
ga.getXMLAnswer(function(response) {
if (response) {
g_form.setValue('start_date', response);
}
});
}

 

 

  • Make sure u_effective_date is a valid Date/Time or Date field.

  • Ensure the Script Include is accessible from the client:

    • It must be client-callable. Confirm that Is Client Callable is checked in the Script Include definition.

  • Test by logging output in grDate.getValue(...) or using gs.info(...).

 

Please mark my answer as helpful/correct if it resolves your query.

Regards,

Arun Manoj

 

Deepak Shaerma
Kilo Sage

Hi @Priya Singh 2 2 

Please confirm, if both catalog date variable (start_date ) and table field (u_effective_date), have Date fields or Date and Time, please check the type of both fields and variable, if both is Date or Date and Time. Please confirm.
Please Mark this Helpful and Accepted Solution. If this Helps you to understand. This will help both the community and me..
- Keep Learning ‌‌
Thanks & Regards 
Deepak Sharma 


OlaN
Giga Sage
Giga Sage

Hi,

You are doing the client script as an onLoad script. Is this intentional?

Otherwise I would assume you would want to run the script as part of a change on the form, when the input for a user selection is changed, or similar.

Does the form contain a value for user to pass to the script include at loadtime ?

It seems like the variable "userID" in the client script is undefined.

Deepak Shaerma
Kilo Sage

@Priya Singh 2 2 

Use this script , this is working for me :

Script Include:

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

    getChangeDate: function() {
        var userID = this.getParameter('sysparm_user_id');
        var grDate = new GlideRecord('u_test_change');
        grDate.addQuery('u_user', userID); // Assuming there's a reference to user
        grDate.query();
        if (grDate.next()) {
            return grDate.getValue('u_effective_date');
        }
        return '';
    },

    type: 'PopulateStartDateUtil'
});



Catalog Client Script:

function onLoad() {
    var userID = g_user.userID;

    var ga = new GlideAjax('PopulateStartDateUtil');
    ga.addParam('sysparm_name', 'getChangeDate');
    ga.addParam('sysparm_user_id', userID);
    ga.getXMLAnswer(function(response) {
        var dateValue = response;
        if (dateValue) {
            g_form.setValue('start_date', dateValue);
        }
    });
}

Please Mark this Helpful and Accepted Solution. If this Helps you to understand. This will help both the community and me..
- Keep Learning ‌‌
Thanks & Regards 
Deepak Sharma