script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-29-2023 01:38 PM
I want a script to update a variable name task, depending on the short description on the catalog task.
function onLoad() {
var newValue = g_form.getValue('short_description');
if(newValue =='Part 2') {g_form.setValue('variables.task','2');
}
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-29-2023 01:56 PM
Hello @Cherly,
You have written a script that updates the variable name task to ‘2’ if the short description on the catalog task is ‘Part 2’. However, your script has some errors and limitations that prevent it from working properly. Here are some suggestions to improve your script:
- Your script is missing a closing curly brace } at the end of the if statement. This will cause a syntax error and prevent the script from running.
- Your script is using the g_form.getValue and g_form.setValue methods incorrectly. These methods require the field name as a parameter, not the field label. The field name is usually in lowercase and uses underscores instead of spaces. For example, the field name for ‘Short description’ is ‘short_description’, not ‘Short description’. You can find the field name by right-clicking on the field label and selecting Show Name .
- Your script is hard-coding the value ‘2’ for the variable name task. This will limit the flexibility and scalability of your script, as you will have to change it manually if you want to use a different value or condition. A better approach is to use a variable or a function to store or generate the value dynamically based on some logic or criteria.
- Your script is running on the onLoad event, which means it will execute when the catalog task form loads. This may not be the best time to update the variable name task, as it may overwrite any existing value or cause confusion for the user. A better option is to run the script on the onChange event, which means it will execute when the short description field changes. This way, you can ensure that the variable name task is updated only when necessary and reflects the latest value of the short description.
Based on these suggestions, here is an improved version of your script:
// Define a function that returns a value for the variable name task based on the short description
function getVariableNameTask(shortDescription) {
// Use a switch statement to handle different cases of short description
switch (shortDescription) {
case "Part 1":
return "1";
case "Part 2":
return "2";
case "Part 3":
return "3";
default:
return "0";
}
}
// Run the script on the onChange event of the short_description field
function onChange(control, oldValue, newValue, isLoading) {
// Check if the form is loading
if (isLoading || newValue == "") {
return;
}
// Get the value of the short_description field
var shortDescription = g_form.getValue("short_description");
// Call the function to get the value for the variable name task
var variableNameTask = getVariableNameTask(shortDescription);
// Set the value of the variables.task field
g_form.setValue("variables.task", variableNameTask);
}
Hope this helps.
Kind Regards,
Swarnadeep Nandy
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-16-2023 02:43 AM
Thank you for explaining, it helps me a lot.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-29-2023 05:20 PM
function onLoad() {
var newValue = g_form.getValue('short_description');
var updatedValue = '';
if (newValue == 'Part 2') {
updatedValue = '2';
} else if (newValue == 'Part 3') {
updatedValue = '3';
} // Add more conditions as needed
g_form.setValue('variables.task', updatedValue);
}