Make the date needed catalog variable from variable set the same as due date

Rhonda9
Tera Expert

Hello,

 

I have a requirement to make the( Date Needed ) due_date  (variable from variable set )  the same as the catalog due date if the Date Needed is empty.   I created an onload client script but it is not working. What am I doing wrong?

 

function onLoad() {
// Retrieve the value of the Due Date field on the Catalog Task form
var dueDateFieldValue = gForm.getValue('due_date');

// Retrieve the value of the "Date Needed" variable from the variable set
var dateNeededValue = gForm.getValue('variables.due_date');

// Debug logs (useful for troubleshooting)
console.log('Due Date Field Value:', dueDateFieldValue);
console.log('Date Needed Variable Value:', dateNeededValue);

// If "Date Needed" is empty and "Due Date" has a value, set "Date Needed" to the "Due Date"
if (!dateNeededValue && dueDateFieldValue) {
gForm.setValue('variables.due_date', dueDateFieldValue);
console.log('Date Needed updated to:', dueDateFieldValue);
}
}

1 ACCEPTED SOLUTION

Juhi Poddar
Kilo Patron

Hello @Rhonda9 

  • As per my understanding Due Date is the variable in catalog and Date Needed is the variable in variable set.
  • You want to populate the value of due date in date needed field if the date needed field is empty.
  • For this the onload client script has to written in variable set.
  • There is a small syntax error as well. It should be g_form and not gForm.

Approach: 

  • Get the value of variable Due Date and  Date Needed.
  • Checks the condition if Date Needed is empty and Due Date has value.
  • If true then set the variable Date Needed.

For better understanding I have considered variable name as:

Due Date: due_date

Date Needed: u_needed_date

 

Here is the updated script:

function onLoad() {
    // Retrieve the value of the "Due Date" field
    var dueDateFieldValue = g_form.getValue('variables.due_date');
    //g_form.addInfoMessage('Due Date Field Value: ' + dueDateFieldValue);

    // Retrieve the value of the "Date Needed" variable
    var dateNeededValue = g_form.getValue('u_needed_date');
    //g_form.addInfoMessage('Date Needed Variable Value: ' + dateNeededValue);

    // If "Date Needed" is empty and "Due Date" has a value, set "Date Needed" to the "Due Date"
    if (!dateNeededValue && dueDateFieldValue) {
        // Convert the due date to a Date object
        var dueDate = new Date(dueDateFieldValue);

        // Format the date to 'yyyy-mm-dd'
        var formattedDate = formatDateToYYYYMMDD(dueDate);

        // Set the formatted date to the "Date Needed" field
        g_form.setValue('u_needed_date', formattedDate);
        g_form.addInfoMessage('Date Needed updated to: ' + formattedDate);
    }
}

// Helper function to format a Date object to 'yyyy-mm-dd'
function formatDateToYYYYMMDD(date) {
    var year = date.getFullYear();
    var month = String(date.getMonth() + 1).padStart(2, '0'); // Month is 0-indexed
    var day = String(date.getDate()).padStart(2, '0');
    return year + '-' + month + '-' + day;
}

Result:

JuhiPoddar_0-1736539787077.png

Hope this helps!

 

"If you found my answer helpful, please like and mark it as an "accepted solution". It helps future readers to locate the solution easily and supports the community!"

 

Thank You
Juhi Poddar

View solution in original post

3 REPLIES 3

Community Alums
Not applicable

Hi @Rhonda9 

 

The method is g_form.getValue. Please refer the below code.

function onLoad() {
// Retrieve the value of the "Due Date" field on the Catalog Task form
var dueDateFieldValue = g_form.getValue('due_date');

// Retrieve the value of the "Date Needed" variable from the variable setvar dateNeededValue = g_form.getValue('variables.due_date');

// Debug logs (useful for troubleshooting in the browser console)
console.log('Retrieved Due Date Field Value:', dueDateFieldValue);
console.log('Retrieved Date Needed Variable Value:', dateNeededValue);

// Check if "Date Needed" is empty and "Due Date" has a value
if (!dateNeededValue && dueDateFieldValue) {
// Set the "Date Needed" variable to the value of the "Due Date" field
g_form.setValue('variables.due_date', dueDateFieldValue);
console.log('Updated Date Needed Variable to:', dueDateFieldValue);
}
}

 

if my answer aligns with your expectation, then please mark my response accordignly.

 

Regards,

Gagan k

Juhi Poddar
Kilo Patron

Hello @Rhonda9 

  • As per my understanding Due Date is the variable in catalog and Date Needed is the variable in variable set.
  • You want to populate the value of due date in date needed field if the date needed field is empty.
  • For this the onload client script has to written in variable set.
  • There is a small syntax error as well. It should be g_form and not gForm.

Approach: 

  • Get the value of variable Due Date and  Date Needed.
  • Checks the condition if Date Needed is empty and Due Date has value.
  • If true then set the variable Date Needed.

For better understanding I have considered variable name as:

Due Date: due_date

Date Needed: u_needed_date

 

Here is the updated script:

function onLoad() {
    // Retrieve the value of the "Due Date" field
    var dueDateFieldValue = g_form.getValue('variables.due_date');
    //g_form.addInfoMessage('Due Date Field Value: ' + dueDateFieldValue);

    // Retrieve the value of the "Date Needed" variable
    var dateNeededValue = g_form.getValue('u_needed_date');
    //g_form.addInfoMessage('Date Needed Variable Value: ' + dateNeededValue);

    // If "Date Needed" is empty and "Due Date" has a value, set "Date Needed" to the "Due Date"
    if (!dateNeededValue && dueDateFieldValue) {
        // Convert the due date to a Date object
        var dueDate = new Date(dueDateFieldValue);

        // Format the date to 'yyyy-mm-dd'
        var formattedDate = formatDateToYYYYMMDD(dueDate);

        // Set the formatted date to the "Date Needed" field
        g_form.setValue('u_needed_date', formattedDate);
        g_form.addInfoMessage('Date Needed updated to: ' + formattedDate);
    }
}

// Helper function to format a Date object to 'yyyy-mm-dd'
function formatDateToYYYYMMDD(date) {
    var year = date.getFullYear();
    var month = String(date.getMonth() + 1).padStart(2, '0'); // Month is 0-indexed
    var day = String(date.getDate()).padStart(2, '0');
    return year + '-' + month + '-' + day;
}

Result:

JuhiPoddar_0-1736539787077.png

Hope this helps!

 

"If you found my answer helpful, please like and mark it as an "accepted solution". It helps future readers to locate the solution easily and supports the community!"

 

Thank You
Juhi Poddar

Hello @Rhonda9 

Thank you for marking my response as helpful.

If this helped, kindly mark this as an accepted solution as well. 

This helps future readers to locate the solution easily in community.

 

Thank You

Juhi Poddar