All I want to do is to copy and paste values from one column to another column...

C_dric Chen
Tera Contributor

I know that many people have asked similar questions before, but I have no choice but to pose it again, because ServiceNow is refusing to execute any script I wrote, no matter how I modify them. (ノಥ益ಥ)ノ ┻━┻

I'm developing an app that is essentially a sign-up sheet for work stations. At this point, I'm creating a Daily Script Execution that updates the sign-up sheet by moving the (current+1) day's reservations to the current day, moving the (current+2) day's reservations to the (current+1) day, etc., regardless whether a field is empty or not.

find_real_file.png

find_real_file.png

All "Reservation" columns have the type "Reference".

These are my codes for the Daily Script Execution:

// This SSE moves D-day+1's reservations to the D-day
// And clears the D-Day's & D-Day+30's reservations
// At 00:00:00 every day

var gR = new glideRecord('x_123456_work_st_1_list_of_work_stations');
gR.query();
	
while (gR.next()) {
	
	// D -> 0
	gR.clearValue('reservations_current_day');
	gR.update();
		
	// D+1 -> D
	var reserv_dDay_1 = g_form.getValue('reservations_current_day_1');
	g_form.setValue('reservations_current_day', reserv_dDay_1);
	gR.update();
		
	// D+2 -> D+1
	var reserv_dDay_2 = g_form.getValue('reservations_current_day_2');
	g_form.setValue('reservations_current_day_1', reserv_dDay_2);
	gR.update();
		
	// D+3 -> D+2
	var reserv_dDay_3 = g_form.getValue('reservations_current_day_3');
	g_form.setValue('reservations_current_day_2', reserv_dDay_3);
	gR.update();
		
	// D+4 -> D+3
	var reserv_dDay_4 = g_form.getValue('reservations_current_day_4');
	g_form.setValue('reservations_current_day_3', reserv_dDay_4);
	gR.update();
		
	// D+5 -> D+4
	var reserv_dDay_5 = g_form.getValue('reservations_current_day_5');
	g_form.setValue('reservations_current_day_4', reserv_dDay_5);
	gR.update();

	/*
	The rest of the codes will be completes
	once the existing codes start working properly
	*/

	g_form.clearValue('reservations_current_day_30');
	gR.update();

	}

I tried both gR.setValue() and g_form.setValue, but neither worked.

I tried both gR.clearValue() and g_form.clearValue, but neither worked.

Any help would be greatly appreciated!

6 REPLIES 6

Jan Cernocky
Tera Guru

Hi,

I assume your script is server-side when you want to run it on a schedule

 

Try this

- corrected spelling of GlideRecord

- changed g_form to gR

- updating just at the end

- removed the initial clean up of D, no need to empty if you assign value in the next step

If it does not help just clarify where and how is your script being executed.

var gR = new GlideRecord('x_123456_work_st_1_list_of_work_stations');
gR.query();
	
while (gR.next()) {

	// D+1 -> D
	var reserv_dDay_1 = gR.getValue('reservations_current_day_1');
	gR.setValue('reservations_current_day', reserv_dDay_1);
		
	// D+2 -> D+1
	var reserv_dDay_2 = gR.getValue('reservations_current_day_2');
	gR.setValue('reservations_current_day_1', reserv_dDay_2);
			
	// D+3 -> D+2
	var reserv_dDay_3 = gR.getValue('reservations_current_day_3');
	gR.setValue('reservations_current_day_2', reserv_dDay_3);
			
	// D+4 -> D+3
	var reserv_dDay_4 = gR.getValue('reservations_current_day_4');
	gR.setValue('reservations_current_day_3', reserv_dDay_4);
			
	// D+5 -> D+4
	var reserv_dDay_5 = gR.getValue('reservations_current_day_5');
	gR.setValue('reservations_current_day_4', reserv_dDay_5);
	
	/*
	The rest of the codes will be completes
	once the existing codes start working properly
	*/

	gR.setValue('reservations_current_day_30','');
	gR.update();

	}

It's starting to work!

Now I just need to figure out how to make it work when a fiels's value is null.

Thank you so very much for your help!

Cedric,

how do you mean it?

Even if it is null, the null value moves same as if there was an actual value.

Can you give me an example?

 

Yes, sir, I can.

	// D+1 -> D
	var reserv_dDay_1 = gR.getValue('reservations_current_day_1');
	if (reserv_dDay_1 == null) {
		gR.setValue('reservations_current_day', '');
		}
	if (reserv_dDay_1 != null) {
		gR.setValue('reservations_current_day', reserv_dDay_1);
		}
		
	// D+2 -> D+1
	var reserv_dDay_2 = gR.getValue('reservations_current_day_2');
	if (reserv_dDay_2 == null) {
		gR.setValue('reservations_current_day_1', '');
		}
	if (reserv_dDay_2 != null) {
		gR.setValue('reservations_current_day_1', reserv_dDay_2);
		}
			
	// D+3 -> D+2
	var reserv_dDay_3 = gR.getValue('reservations_current_day_3');
	if (reserv_dDay_3 == null) {
		gR.setValue('reservations_current_day_2', '');
		}
	if (reserv_dDay_3 != null) {
		gR.setValue('reservations_current_day_2', reserv_dDay_3);
		}
			
	// D+4 -> D+3
	var reserv_dDay_4 = gR.getValue('reservations_current_day_4');
	if (reserv_dDay_4 == null) {
		gR.setValue('reservations_current_day_3', '');
		}
	if (reserv_dDay_4 != null) {
		gR.setValue('reservations_current_day_3', reserv_dDay_4);
		}

As you can see, I had to create two conditional statements, because the system wouldn't copy and paste the null from one column to the other.