Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

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

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');
	}
}

Mark, the same. Value are setup, but no both alerts, nor removing values when emptying reference. Maybe that's a bug in Jakarta, isn't it ? 

Mark, so to speak... You're the real ServiceNow GURU. the code has worked with the newValue=='' operator. 

 

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;
		
		
		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);
		}
		
		if (newValue =='') {
			
		
		g_form.clearValue('fru_first_name');
 		g_form.clearValue('fru_last_name');
 		g_form.clearValue('fru_email');
 		g_form.clearValue('fru_employee_id');	 
		    
	
	}


}

Thanks Piotr.  Please mark my answer above as the correct one if I've answered your question.