Interested in a ServiceNow event built for developers? Registration for now[dev]26 is officially open!

Not able to set Date type field from Client script using g_scratchpad

BijoyDeb
Tera Contributor

Hi All,

There is a planned due date (date/ time) field on my incident form.

I want whenever user tries to create incident task from related list, planned due date (date) field on incident task should auto populate date that was on incident.

 

I have written display business rule on incident task to get date from parent incident-

 

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

var inc= new GlideRecord('incident');
if(inc.get(current.incident))

var plannedDate = new GlideDateTime(inc.due_date);
g_scratchpad.plannedDueDate = plannedDate.getDate();

})(current, previous);

 

Then, I've written onload client script on incident task -

 

function onLoad() {
//Type appropriate comment here, and begin script below
if(g_form.getValue('incident'))
var plannedDate = g_scratchpad.plannedDueDate;

 

g_form.setValue('u_planned_due_date', plannedDate);

}

 But instead of setting date, it shows object object

 

BijoyDeb_0-1741695879412.png

 

Can someone please help me on what Im doing wrong in above script?

 

Thanks in Advance!!

2 ACCEPTED SOLUTIONS

Ankur Bawiskar
Tera Patron

@BijoyDeb 

simply set that from display business rule and don't use client script

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

var inc= new GlideRecord('incident');
if(inc.get(current.incident)){
current.setValue('u_planned_due_date',new GlideDateTime(inc.due_date));
}

})(current, previous);

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

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

View solution in original post

Ankur Bawiskar
Tera Patron

@BijoyDeb 

if you want to use both then do this

Display business rule:

(function executeRule(current, previous /*null when async*/) {
    var inc = new GlideRecord('incident');
    if (inc.get(current.incident)) {
        var plannedDate = new GlideDateTime(inc.due_date);
        g_scratchpad.plannedDueDate = plannedDate.getDisplayValue(); // Use getDisplayValue() to get the date as a string
    }
})(current, previous);

Client script:

function onLoad() {
    if (g_form.getValue('incident')) {
        var plannedDate = g_scratchpad.plannedDueDate;
        g_form.setValue('u_planned_due_date', plannedDate);
    }
}

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

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

View solution in original post

4 REPLIES 4

Ankur Bawiskar
Tera Patron

@BijoyDeb 

simply set that from display business rule and don't use client script

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

var inc= new GlideRecord('incident');
if(inc.get(current.incident)){
current.setValue('u_planned_due_date',new GlideDateTime(inc.due_date));
}

})(current, previous);

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

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

Ankur Bawiskar
Tera Patron

@BijoyDeb 

if you want to use both then do this

Display business rule:

(function executeRule(current, previous /*null when async*/) {
    var inc = new GlideRecord('incident');
    if (inc.get(current.incident)) {
        var plannedDate = new GlideDateTime(inc.due_date);
        g_scratchpad.plannedDueDate = plannedDate.getDisplayValue(); // Use getDisplayValue() to get the date as a string
    }
})(current, previous);

Client script:

function onLoad() {
    if (g_form.getValue('incident')) {
        var plannedDate = g_scratchpad.plannedDueDate;
        g_form.setValue('u_planned_due_date', plannedDate);
    }
}

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

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

BijoyDeb
Tera Contributor

Thanks @Ankur Bawiskar , Instead of writing additional client script, I'll use br only.

Thanks again!

 

@BijoyDeb 

Glad to help.

Would you mind closing your earlier questions by marking appropriate response as correct?

Members have invested their time and efforts in helping you.

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