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

Ankur Bawiskar
Tera Patron
Tera Patron

@Priya Singh 2 2 

why not use auto populate feature/logic and then no scripting is required?

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.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@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.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Priya Singh 2 2 

Thank you for marking my response as helpful.

I believe I also answered your question with a better alternative.

As per new community feature you can mark multiple responses as correct.

If my response helped please mark it correct as well so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader