how to copy date field from sys_user table to sc_req_item table

abhaysingh98
Tera Contributor

Hi guys, I am trying to copy one date field from sys_user table to sc_req_item table and to achieve this I created display Business rule with g_scratchpad with a couple properties and use them in the onLoad client script but it's not working can someone please help me.

I am getting output as undefined in date field on RITM form.

Many thanks.

 

Display BR:

 

g_scratchpad.u_due_date_2 = current.sys_user.u_account_expiry_date.getDisplayValue();
 
In the onLoad client script:
 
g_form.setValue('u_due_date_2', g_scratchpad.u_due_date_2);
 
1 ACCEPTED SOLUTION

SN_Learn
Kilo Patron
Kilo Patron

Hi @abhaysingh98 ,

 

Please try the below:

 

Display Business rule on [sc_req_item]

(function executeRule(current, previous /*null when async*/ ) {

    var grUser = new GlideRecord('sys_user');
    if (grUser.get(current.requested_for)) {
        g_scratchpad.getDateVal = current.requested_for.u_account_expiry_date;
    }

})(current, previous);

 

Onchange Client script:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }

    g_form.setValue("u_due_date_2", g_scratchpad.getDateVal);
}

 

Please hit like and mark my response as correct if that helps.

----------------------------------------------------------------
Mark this as Helpful / Accept the Solution if this helps.

View solution in original post

4 REPLIES 4

Maik Skoddow
Tera Patron
Tera Patron

Hi

current.sys_user

in your display BR makes no sense and furthermore you did not explain from which user you want to pull the data. From the currently logged-in user? If so, set the default value on the dictionary record of the related field u_account_expiry_date. For some inspiration, please refer to https://blog.snowycode.com/post/how-to-auto-populate-the-current-user-in-servicenow 

Hi maik,

I want to pull the data for requested for user not for logged in user.

SN_Learn
Kilo Patron
Kilo Patron

Hi @abhaysingh98 ,

 

Please try the below:

 

Display Business rule on [sc_req_item]

(function executeRule(current, previous /*null when async*/ ) {

    var grUser = new GlideRecord('sys_user');
    if (grUser.get(current.requested_for)) {
        g_scratchpad.getDateVal = current.requested_for.u_account_expiry_date;
    }

})(current, previous);

 

Onchange Client script:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }

    g_form.setValue("u_due_date_2", g_scratchpad.getDateVal);
}

 

Please hit like and mark my response as correct if that helps.

----------------------------------------------------------------
Mark this as Helpful / Accept the Solution if this helps.

thank you very much!