The CreatorCon Call for Content is officially open! Get started here.

Please help me prevent this Client Script from running when the Project Task form loads

Justin-DNV
Tera Contributor

Hello and happy Friday!  I am looking for assistance on the script below.  This should only run when a user modifies the Planned End Date field from one date to another.  It should not run when the form is first loaded.  However, the script is running when a user creates an new Project Task, and, when the user opens an existing Project Task.  

I understand that the second line in the code should be preventing it from running when the form loads, but it doesn't.  

Can anyone help me out with this?

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    if (oldValue != '') { //Check if the Planned End Date field was blank	
        g_form.addErrorMessage('Planned End Date changed.  Please complete the template in the Work Notes field and provide details as to why the Planned End Date was modified.');
        var myFieldValue = g_form.getValue('work_notes'); //Put the value of the work_notes field into a variable.
        //if (myFieldValue.toString().length < 1000) { // Verify number of characters currently in the Work Notes field.
        g_form.setValue('work_notes', 'Planned End Date updated.  Complete the following template and provide details as to why the Planned End Date was modified: \n\nCurrent Status: \nNext Steps: \nNew Planned End Date: \n\n\nExamples: \n\nCurrent Status: This documentation is being written. I have steps completed for how to update the system. \nNext Steps: I will need access to server wexap100 before I can obtain the final screenshots. I have reached out to the Microsoft team to request this access. \nNew Planned End Date: 2022-11-19 16:01:15 \n\nCurrent Status: The access has been provided. Please test the access \nNext Steps: We are moving the Planned End Date to allow time to test access to the system. \nNew Due Date:2022-11-15 16:01:15\n\n' + myFieldValue); //Populates the Customer Notes field with the text in the string.  \n for a new line.
    }
}

 

1 ACCEPTED SOLUTION

Prince Arora
Tera Sage

@Justin-DNV 

 

You can give it a try by adding a and condition in the script as:

 

if (oldValue != ''  && oldValue != newValue) 

 

It indicates that when value gets changed, it will trigger

 

If my answer solved your issue, please mark my answer as Correct & 👍Helpful based on the Impact.

 

View solution in original post

5 REPLIES 5

Johns Marokky
Tera Guru

Hi @Justin-DNV ,

I don't think this script is running onLoad. There might be some other script that is causing the issue.

Also do check on what field this onChange Script works because there might be a case where any one of the onLoad script is changing one of the field values and it might be triggering this onChange script.

 

Mark helpful if it helps in Solving your query.

 

Regards,

Johns

 

Prince Arora
Tera Sage

@Justin-DNV 

 

You can give it a try by adding a and condition in the script as:

 

if (oldValue != ''  && oldValue != newValue) 

 

It indicates that when value gets changed, it will trigger

 

If my answer solved your issue, please mark my answer as Correct & 👍Helpful based on the Impact.

 

NaveenGN
Tera Expert

Simply change isLoading == 'false'

 

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading == 'false' || newValue === '') {
alert ('hello')
return;
}
if (oldValue != '' && oldValue != newValue) { //Check if the Planned End Date field was blank
g_form.addErrorMessage('Planned End Date changed. Please complete the template in the Work Notes field and provide details as to why the Planned End Date was modified.');
var myFieldValue = g_form.getValue('work_notes'); //Put the value of the work_notes field into a variable.
//if (myFieldValue.toString().length < 1000) { // Verify number of characters currently in the Work Notes field.
g_form.setValue('work_notes', 'Planned End Date updated. Complete the following template and provide details as to why the Planned End Date was modified: \n\nCurrent Status: \nNext Steps: \nNew Planned End Date: \n\n\nExamples: \n\nCurrent Status: This documentation is being written. I have steps completed for how to update the system. \nNext Steps: I will need access to server wexap100 before I can obtain the final screenshots. I have reached out to the Microsoft team to request this access. \nNew Planned End Date: 2022-11-19 16:01:15 \n\nCurrent Status: The access has been provided. Please test the access \nNext Steps: We are moving the Planned End Date to allow time to test access to the system. \nNew Due Date:2022-11-15 16:01:15\n\n' + myFieldValue); //Populates the Customer Notes field with the text in the string. \n for a new line.
}
}

DrewW
Mega Sage

Have you tried changing this line

if (isLoading || newValue === '') {

To

if (isLoading) {

 

Also have you put in an alert or console.log like to see when its triggering to make sure its doing it when you think it is?