OOB Business rule stop tasking creation

cpinedatx94
Tera Expert

I have a workflow that makes work order task but its getting stopped dude to the OOB "validate changes" BR on the work order task table.

 

Ive tried reading through the BR can anyone give me some info on what's going on?

4 REPLIES 4

Kieran Anson
Kilo Patron

Could you share the BR script, looking in an OOB PDI, I wasn't able to find a BR with a name of 'validate changes', perhaps this is a custom script?

function onBefore(current, previous) {

    var ss = new SharedServiceUtils();
    var sc = new sn_sm.SMConfiguration();
    var crewSchedulingActive = GlidePluginManager.isActive('com.snc.fsm_crew_scheduling');

	var territoryActiveWM = isTerritoryActiveforFSM();
	
	if(current.state == SMConstants.TASK_PENDING_DISPATCH && territoryActiveWM && current.territory.nil() && current.sys_class_name == 'wm_task'){
		gs.addErrorMessage(gs.getMessage('A Territory has to be provided'));
		current.setAbortAction(true);
		return;
	}
	
    if (sc.isEnabled(current, "use_sf", "false")) {
        if (current.operation() == "update" && ss.fieldChangeCheck("state") && (new SMTask().needsWorkNotesTask(current.state, previous.state)))
            VerifyWorkNotes(current);

        if (current.state == SMConstants.TASK_PENDING_DISPATCH && current.dispatch_group.nil() && sc.isEnabled(current, 'dispatch_queue', false))
            CheckDispatchGroup(current);
    }
    if (ss.fieldChangeCheck("dispatch_group,assignment_group,assigned_to, assigned_crew")) {

        if (current.sys_class_name != 'hr_task')
            GroupValidation(current);
        else if (current.sys_class_name == 'hr_task')
            HRGroupValidation(current);

    }

    if (ss.fieldChangeCheck("work_notes") && (new sn_sm.SMConfiguration()).isEnabled(current, "rollup_work_notes", false))
        RollUpWorkNotes(current);

    function CheckDispatchGroup(current) {
        gs.addErrorMessage(gs.getMessage('A dispatch group has to be provided'));
        if (previous.state == 0)
            current.state = 1;
        else {
            current.state = previous.state;
            current.setAbortAction(true);
        }
    }

    function VerifyWorkNotes(current) {
        var stm = new SMTask();
        if (!stm.hasWorkNotes(current)) {
            gs.addErrorMessage(stm.getNoWorkNotesMessage(current.state));
            current.state = previous.state;
            current.assigned_to = previous.assigned_to;
            current.setAbortAction(true);
        }
    }

    function RollUpWorkNotes(current) {
        if (current.parent) {
            var parent = new GlideRecord('sm_order');
            if (parent.get(current.parent)) {
                parent.work_notes = current.work_notes;
                parent.update();
            }
        }
    }

    function GroupValidation(current) {
        var valid = true;

        if (crewSchedulingActive && current.sys_class_name == 'wm_task' && current.requires_crew == true) {
            if (current.assigned_crew == '""' || current.assigned_crew == "''") {
                current.assigned_crew = '';
            }
        } else {
            if (current.assigned_to == '""' || current.assigned_to == "''") {
                current.assigned_to = '';
            }
        }

        var dispatchQueue = (new sn_sm.SMConfiguration()).isEnabled(current, 'dispatch_queue', false);
        if (crewSchedulingActive && current.sys_class_name == 'wm_task' && current.requires_crew == true) {
            if (!current.assigned_crew.nil()) {
                if (current.assignment_group.nil()) {
                    gs.addErrorMessage(gs.getMessage('Cannot have a crew when the assignment group is empty'));
                    valid = false;
                } else if (!(new SMTask()).hasValidCrew(current) && (!current.dispatch_group.nil() || !dispatchQueue)) {
                    // SMTask.hasValidCrew returns false when dispatch group is empty, localize extra checking here to minimize risk
                    gs.addErrorMessage(gs.getMessage('The crew is not a member of the assignment group'));
                    valid = false;
                }
            }
        } else {
            if (!(current.assigned_to.nil() || current.assigned_to == 'NULL')) {
                if (current.assignment_group.nil()) {
                    gs.addErrorMessage(gs.getMessage('Cannot have an assignee when the assignment group is empty'));
                    valid = false;
                } else if (!(new SMTask()).hasValidAssignee(current) && (!current.dispatch_group.nil() || !dispatchQueue)) {
                    // SMTask.hasValidAssignee returns false when dispatch group is empty, localize extra checking here to minimize risk
                    gs.addErrorMessage(gs.getMessage('The assignee is not a member of the assignment group'));
                    valid = false;
                }
            }
        }

        if (dispatchQueue) {
            //dispatch is required in order to be assigned
            if (crewSchedulingActive && current.sys_class_name == 'wm_task' && current.requires_crew == true) {
                if (current.dispatch_group == '' &&
                    (current.assigned_crew != '' ||
                        current.assignment_group != '' ||
                        current.state != SMConstants.TASK_DRAFT)) {
                    gs.addErrorMessage(gs.getMessage('The dispatch group is required in order to be assigned'));
                    valid = false;
                }
            } else {
                if (current.dispatch_group == '' &&
                    (current.assigned_to != '' ||
                        current.assignment_group != '' ||
                        current.state != SMConstants.TASK_DRAFT)) {
                    gs.addErrorMessage(gs.getMessage('The dispatch group is required in order to be assigned'));
                    valid = false;
                }
            }            
            if (valid && (!gs.nil(current.getValue('assignment_group')) && current.getValue('assignment_group')!= "''")) {
				var isCrewPluginActive = GlidePluginManager.isActive("com.snc.fsm_crew_scheduling");
				var groups = '';
				if((new WMFilters()).isTerritoryPlanningActive() && current.sys_class_name == 'wm_task'){
					groups = new WMFilters().filterAssignmentGroups(current);
				} else{
					groups = new SMFilters().filterWorkGroupBasedOnDispatchGroup(current.dispatch_group, current.getTableName(), false, current.location);
				}
                // Don't show error message when Dynamic scheduling is in progress and is yet to fill assigned_to for Territory Planning
			    // as assignment group might be backfilling and not necessarily part of territory.
                if ((groups.substring(8) == '' || groups.indexOf(current.assignment_group) == -1) && (current.scheduling_method != "dynamic_schedule" && (new WMFilters()).isTerritoryPlanningActive() && !(isCrewPluginActive && current.requires_crew == true && !gs.nil(current.assigned_to)))) {
                    gs.addErrorMessage(gs.getMessage('The assignment group is not contained in the dispatch group'));
                    valid = false;
                }
            }
        }

        if (!valid) {
            current.setAbortAction(true);
        }
    }


    function HRGroupValidation(current) {
        var valid = true;

        if (current.assigned_to != '') {
            if (current.assignment_group == '') {
                valid = true;
            } else {
                var vendorGroupType = (new sn_sm.SMConfiguration()).getGroupType(current, 'vendor');
                if (current.assignment_group.type.indexOf(vendorGroupType) == -1 || vendorGroupType == '') {
                    var gr = new GlideRecord('sys_user_grmember');
                    gr.addQuery('user', current.assigned_to);
                    gr.addQuery('group', current.assignment_group);
                    gr.query();
                    if (!gr.hasNext()) {
                        gs.addErrorMessage(gs.getMessage('The assignee is not a member of the record\'s group'));
                        valid = false;
                    }
                } else if (new SMFilters().assignedToFilter(current).indexOf(current.assigned_to) == -1) {
                    gs.addErrorMessage(gs.getMessage('The assignee is not a member of the record\'s vendor group'));
                    valid = false;
                }
            }
        }

        if ((new sn_sm.SMConfiguration()).isEnabled(current, 'dispatch_queue', false)) {
            //dispatch is required in order to be assigned
            if (current.dispatch_group == '' &&
                (current.assigned_to != '' ||
                    current.assignment_group != '' ||
                    current.state != SMConstants.TASK_DRAFT)) {
                gs.addErrorMessage(gs.getMessage('The dispatch group is required in order to be assigned'));
                valid = false;
            }
// Don't show error message when Dynamic scheduling is in progress and is yet to fill assigned_to for Territory Planning
			// as assignment group might be backfilling and not necessarily part of territory.
            if (valid && current.assignment_group.toString() != '') {
                var groups = new SMFilters().filterWorkGroupBasedOnDispatchGroup(current.dispatch_group, current);
                if (groups.substring(8) == '' || groups.indexOf(current.assignment_group) == -1) {
                    gs.addErrorMessage(gs.getMessage('The assignment group is not contained in the dispatch group'));
                    valid = false;
                }
            }
        }

        if (!valid) {
            current.setAbortAction(true);
        }
    }
	
	function isTerritoryActiveforFSM(current){
		if(current.getTableName() == 'wm_task' && new WMFilters().isTerritoryPlanningActive()){
			return true;
		}
		return false;
	}


    if ((current.sys_class_name == "wm_task") && gs.nil(current.assigned_to) && (current.state == "16" || current.state == "17")) {
        current.state = "10"; //Pending dispatch/ pending assignment
    }
}

Thanks for the BR. The BR will prevent an update on the following conditions

  • If territory planning is active, and the task is set to pending dispatch without a territory, the update is prevented.
  • If the state changes then work notes will be needed if the state updates to one of (Cancelled, Closed Incomplete, Closed Complete).
  • If the state changes then work notes will be needed if the state transitions from one of the following:
    • Pending dispatch back to draft
    • Assigned back to pending dispatch (e.g someone rejected the assignment)
    • Work In Progress back to pending dispatch (e.g someone rejected the task)
  • If state changes to Pending dispatch, and the dispatch group is empty
  • If one of the following fields change (dispatch group, assignment group, assigned to, or crew)
    • If Crew Operations is being used, then a task can't be updated if there isn't an assignment group and the task requires a crew to be assigned
    • The assigned to user needs to be a member of the wm_crew_group record
    • If dispatch crew is being used, then in order to be assigned, the dispatch group needs to have a value.

Sanket Landge
Tera Expert

Hello @cpinedatx94 ,

 

I think you need to add specific condition in BR as per your workflow.

 

Also if you shear the OOB Br name/script then we can help more.

 

Thanks,

Sanket Landge