Clearing value onChange client script when a Reference field was emptied

Piotr Ba_amut
Giga Expert

I've got a problem with clearing values in fields x,y,z when a Reference was emptied (user removed as a value). It concerns catalog client script. Am trying to manipulate with "newValue", "odValue", but in vain..

 

Code: 

 

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '' ) {
      return;
   }
 
	var req = g_form.getReference('fru_requested_for',pullAttributes);   //call to the sys_user reference field
	//below is the callBack function to pull user's attributes
	

	function pullAttributes(req) {
		
		
		
		var h = g_form.getDisplayBox('fru_requested_for').value;
		if (newValue !=='') {
		alert("ustawiono");
		g_form.setValue('fru_first_name',req.first_name);
		g_form.setValue('fru_last_name',req.last_name);
		g_form.setValue('fru_email',req.email);
		g_form.setValue('fru_employee_id',req.employee_number);
		} else if (newValue == '')
		
		
		  {
		alert("test");
		g_form.setValue('fru_first_name','');
		g_form.clearValue('fru_last_name');
		g_form.clearValue('fru_email');
		g_form.clearValue('fru_employee_id');	 
		    }
	
	}
 
}
1 ACCEPTED SOLUTION

Try this and let me know what you get.

function onChange(control, oldValue, newValue, isLoading) {
	if (isLoading ) {
		return;
	}
	
	// Call to the sys_user reference field
	var req = g_form.getReference('fru_requested_for', pullAttributes); 
}

// Callback function to pull user's attributes
function pullAttributes(req) {
	if (newValue != '') {
		alert("Not empty");
		g_form.setValue('fru_first_name', req.first_name);
		g_form.setValue('fru_last_name', req.last_name);
		g_form.setValue('fru_email', req.email);
		g_form.setValue('fru_employee_id', req.employee_number);
	}
	else {
		alert("Empty");
		g_form.clearValue('fru_first_name','');
		g_form.clearValue('fru_last_name');
		g_form.clearValue('fru_email');
		g_form.clearValue('fru_employee_id');
	}
}

View solution in original post

8 REPLIES 8

Mark Stanger
Giga Sage

The very beginning of your script has this section, which is returning from the script if the field value is empty.  I don't think you're even getting to any of the other code...

if (isLoading || newValue == '' ) { return; }

If you take the 'newValue' check out it should work better...

if (isLoading) { return; }

Thanks Mark, 

I've followed your advice, but it still does not clear values..  However, when I select referecen user, values are being setup and 1st alert launched. The problem, however, is with clearing values. The version is Jakarta. 

 

Modified code: 

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading ) {
    return;
   }
 
	var req = g_form.getReference('fru_requested_for',pullAttributes);   //call to the sys_user reference field
	//below is the callBack function to pull user's attributes
	

	function pullAttributes(req) {
		
		
		
		var h = g_form.getDisplayBox('fru_requested_for').value;
		if (newValue !=='') {
		alert("ustawiono");
		g_form.setValue('fru_first_name',req.first_name);
		g_form.setValue('fru_last_name',req.last_name);
		g_form.setValue('fru_email',req.email);
		g_form.setValue('fru_employee_id',req.employee_number);
		} 
		
		else if (newValue == '')
		
		
		  {
		alert("test");
		g_form.setValue('fru_first_name','');
		g_form.clearValue('fru_last_name');
		g_form.clearValue('fru_email');
		g_form.clearValue('fru_employee_id');	 
		    }
	
	}

}
	

Try this script and let me know how it works.  If it isn't clearing the values, do you at least see the 'Empty' alert?

function onChange(control, oldValue, newValue, isLoading) {
	if (isLoading ) {
		return;
	}
	
	// Call to the sys_user reference field
	var req = g_form.getReference('fru_requested_for', pullAttributes); 
}

// Callback function to pull user's attributes
function pullAttributes(req) {
	if (newValue) {
		alert("Not empty");
		g_form.setValue('fru_first_name', req.first_name);
		g_form.setValue('fru_last_name', req.last_name);
		g_form.setValue('fru_email', req.email);
		g_form.setValue('fru_employee_id', req.employee_number);
	}
	else {
		alert("Empty");
		g_form.clearValue('fru_first_name','');
		g_form.clearValue('fru_last_name');
		g_form.clearValue('fru_email');
		g_form.clearValue('fru_employee_id');
	}
}

Piotr Ba_amut
Giga Expert

Hi Mark, 

Thank you. Neither 1st, not 2nd alert is shown, however, values are setup.. Maybe as a result of the callBack.