onChange Cient Script causing "Match not found, reset to original" error

Abbottronix
Tera Guru

I've created the following onChange client script to automatically set the Caller Id and custom Company field on incidents. 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {

	g_form.getReference('u_requestor', setContact);
	}

	function setContact(requestor) {
		if (requestor && requestor.company != '')
			g_form.setValue('caller_id', requestor.name);
			g_form.setValue('u_company', requestor.company);
	}

When I try to submit a new incident it gives me the error "Invalid update" and "Match not found, reset to original". When I try to change the company of an existing incident and then update the incident, my update to the company field isn't saved. 

 

 

1 ACCEPTED SOLUTION

Karan Chhabra6
Mega Sage
Mega Sage

Hi @Abbottronix ,

 

Please use this script, it should work

function onChange(control, oldValue, newValue, isLoading, isTemplate) {

	g_form.getReference('u_requestor', setContact);
	}

	function setContact(requestor) {
		if (requestor && requestor.company != '')
			g_form.setValue('caller_id', requestor.sys_id);
			g_form.setValue('u_company', requestor.company);
	}

 

If my answer has helped with your question, please mark it as correct and helpful

 

Thanks!

 

View solution in original post

12 REPLIES 12

Hi Karan,

 

The good news is that it totally worked! The bad news is I don't understand why it worked at all, haha. I would have thought that using ('caller_id', requestor.sys_id) would have filled the field with the requestor sys_id, so I don't understand why it filled it with the name instead. 

 

I'm also having the issue where, if the requestor company is blank and I fill the company field in mandatory, when I save the incident, the company field goes back to being blank. 

Hi @Abbottronix  - glad it worked, reference fields accepts 'sys_id' as input.

For the other issue where the company is blank after you save, this is happening because your onchange script is running onLoad as well.

Update this script as:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
 if (isLoading || newValue === '') {
      return;
   }
	g_form.getReference('u_requestor', setContact);
	}

	function setContact(requestor) {
		if (requestor && requestor.company != '')
			g_form.setValue('caller_id', requestor.sys_id);
			g_form.setValue('u_company', requestor.company);
	}

 

If my answer has helped with your question, please mark it as correct and helpful.

 

Thanks!

Hi Karan,

 

So if I put a sys_id into a reference field, it will automatically update to show the name value?

 

I would like the script to work onLoad as well because I want it to automatically fill out the Affected Contact and Company fields when the Requestor field is automatically updated as the logged in user. But you made me realise I can fix the issue by just adding an else {return;} to the end of my function, so everything's working for now, thanks a bunch!