Dot walking with current on before business rule not working

Francis16
Tera Contributor

We have requirement to change a field in a related table using dot walking. The line gs.info("Current Date lesser than grSME date"+current.scheduled_maintenance.asset.u_next_scheduled_maint_date);    works fine and i could get a log statement. But the succeeding line fails to update the record. 

(function executeRule(current, previous /*null when async*/) {

	// Add your code here
	var grSME= new GlideRecord("x_nuvo_eam_scheduled_maintenance_executions");
	grSME.addQuery('scheduled_maintenance',current.scheduled_maintenance);
	grSME.orderBy('status');
	grSME.orderBy('scheduled_date');
	grSME.setLimit(1);
	grSME.query();
	if(grSME.next()){
		gs.info(grSME.scheduled_date+current.scheduled_date);
		if(grSME.status=="Pending"){
			gs.info("Pending Status");
			if(current.scheduled_date<grSME.scheduled_date){
				gs.info("Current Date lesser than grSME date"+current.scheduled_maintenance.asset.u_next_scheduled_maint_date);
				current.scheduled_maintenance.asset.last_maintenance_date=current.scheduled_date;
			}
		}
		else{
			current.scheduled_maintenance.asset.u_next_scheduled_maint_date=current.scheduled_date;
		}
	}
	

})(current, previous);
1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron

Hi,

you cannot set dot walked field like this

you need to query that table and then update the field

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

3 REPLIES 3

Ankur Bawiskar
Tera Patron

Hi,

you cannot set dot walked field like this

you need to query that table and then update the field

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Thank you. It works

Ankur Bawiskar
Tera Patron

Hi,

like this

(function executeRule(current, previous /*null when async*/) {

	// Add your code here
	var grSME= new GlideRecord("x_nuvo_eam_scheduled_maintenance_executions");
	grSME.addQuery('scheduled_maintenance',current.scheduled_maintenance);
	grSME.orderBy('status');
	grSME.orderBy('scheduled_date');
	grSME.setLimit(1);
	grSME.query();
	if(grSME.next()){
		gs.info(grSME.scheduled_date+current.scheduled_date);
		if(grSME.status=="Pending"){
			gs.info("Pending Status");
			if(current.scheduled_date<grSME.scheduled_date){
				gs.info("Current Date lesser than grSME date"+current.scheduled_maintenance.asset.u_next_scheduled_maint_date);

				// query the asset table and update
				var gr = new GlideRecord("asset table");
				gr.addQuery("sys_id", current.scheduled_maintenance.asset);
				gr.query();
				if (gr.next()) {
					gr.last_maintenance_date = current.scheduled_date;
					gr.update();
				}
			}
		}
		else{
			// query the asset table and update
			var gr1 = new GlideRecord("asset table");
			gr1.addQuery("sys_id", current.scheduled_maintenance.asset);
			gr1.query();
			if (gr1.next()) {
				gr1.u_next_scheduled_maint_date = current.scheduled_date;
				gr1.update();
			}
		}
	}

})(current, previous);

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader