Fields changed by g_form.setValue reverting after save

Sam Scott1
Tera Expert

I have a UI action on the Incident table that runs a client script which does the following four things: 1. Copies the (unpublished) additional comments, 2. Sends those comments to a Teams Chat via Deeplink URL, 3. sets the incident state to "On Hold" with a hold reason of "Awaiting Caller", 4. Saves the record to commit the state change and post the comment to the record. Steps 1, 2, and 4 always work. Teams opens with the correct message, and the unpublished comment gets published during the save event. Step 3 is not working as intended.

If I run this client script with the "g_form.save()" function commented out, all 4 steps appear to work. I can see that the values in the state and Hold Reason fields are correct. However, when I go to save the record using the OOB save or update UI Actions, both fields revert to their original value. This is also the case when I run g_form.save(). If I change any other fields before manually saving, those fields update correctly. The two fields modified by the script always revert unless I manually adjust them.

Here's the relevant portion of the script:

function sendMessage() {
	
	//set incident hold status
	g_form.setValue('state', 3, "On Hold");
	g_form.setValue('hold_reason', 1, "Awaiting Caller");
	
	g_form.save();
}

And here's the whole script:

function sendMessage() {
	// get message from additional comments
	var message = g_form.getValue('comments');
	if (message == '') {
		g_form.showFieldMsg("comments", "Message to user cannot be empty.", "error");
		return;
	}
	
	//set incident hold status
	g_form.setValue('state', 3, "On Hold");
	g_form.setValue('hold_reason', 1, "Awaiting Caller");
	
	//Build teams redirect
	var prefix = "msteams:/l/chat/0/0?users=";
	var user = g_form.getReference('u_contact_person').email;
	var fname = g_form.getReference('u_contact_person').first_name;
	var subject = '&message=Hi ' + fname + ', I need more information regarding your incident ' + g_form.getValue('number') + ": " + g_form.getValue('short_description') + ". ";
	
	//open URL
	var w = getTopWindow();
	var url = prefix + user + subject;
	url += message;
	
	//g_form.save();
	w.open(url);
}

Have I missed something in the documentation about g_form.setValue? Is there a different method I should be using to update the record from a client script?

3 REPLIES 3

Marcin20
Mega Guru

Hi Sam,

It looks like the 'Additional comments' field should be also populated.

When tried manually, I have received the following message:

If my answer helped you in any way, please mark this answer as helpful and correct.

Marcin20
Mega Guru

Hi Sam,

 

Please try the following approach:

 

function sendMessage() {
    
    //set incident hold status
    g_form.setValue('state', 3, "On Hold");
    g_form.setValue('hold_reason', 1, "Awaiting Caller");

    g_form.setValue('comments','The state is changed');
    g_form.save();
}

 

If my answer helped you in any way, please mark this answer as helpful and correct.

 

Hi Marcin,

I tried this and it doesn't make a difference. Normally, this script only runs when the comments were already filled. So I tried to set the comments to the value already there, as well as overwriting the existing comments with "The state is changed", and the behavior is the same. The comments are posted (either the overwritten comment or the original, as expected) but the state and hold reason still revert to In Progress.