Updating asset action for swap in stock substate

Jason_DaSilva
Tera Guru

Ok, a few things we are trying to do here.  First off, with HAM pro we can use the Asset action in an incident ticket on the Affected CIs tab to allow techs working on INC tickets to be able to modify the asset state and substate.  The default for the Asset action of swap is to set the Affected CI's asset to In Stock state and Pending disposal Substate.  This is not ideal for some of our changes, as there are times that the Affected CI should be set to available or Pending Repair.   We are trying to keep the techs from poking around in the hardware table as in the past that has caused... issues...

I created a business rule to try and deal with this but am having some issues with timing.  I have tried setting it to run After Update order 900.  On the filter conditions, I have set it to state changes to Resolved, as that seems to be when the event for the update of the asset seems to happen, but it appears that I have either coded it wrong, or I am doing it to early and its getting overwritten.  here's the code:

(function executeRule(current, previous /*null when async*/) {
    // pulling current from incident table
    var incNumber = current.number.toString();
    var tsGR = new GlideRecord("task_ci");
    tsGR.addEncodedQuery("task.number=" + incNumber);
    tsGR.query();

    //possible multiple task_ci records for this INC ticket if multiple CIs were selected in Affected CIs tab
    while(tsGR.next()){
        //collect data to test.  Remove case for easier testing (make tests lower case), just in case.
        var myAction = tsGR.asset_action.toString().toLowerCase();
        var myReason = tsGR.u_action_reason.toString().toLowerCase();
        var mySubstate = 'Pending disposal';

        if(myAction == 'swap'){
            //only continue if the action was swap(?)

            //get the asset data for this task_ci
            var ahGR = new GlideRecord('alm_hardware');
            ahGR.addEncodedQuery('serial_numberSTARTSWITH' + tsGR.ci_item.serial_number.toString());
            ahGR.query();

            var workString = '';

            if(ahGR.getRowCount() < 1){
                //no hardware record found, this should not happen....  Just in case, add note to INC
                workString = 'No asset found with matching serial number to Configuration Item ' + tsGR.ci_item.name.toString();
                current.work_notes = workString;
                break;
            }
            while(ahGR.next){
                switch(true){
                    case myReason.includes("fix"😞
                        mySubstate = 'Pending repair';
                        //console.log("set substate 'Pending repair' if action reason contains 'fix'");
                        break;
                    case myReason.includes("role"😞
                        mySubstate = 'Available';
                        //console.log("set substate 'Available' if action reason contains 'role'");
                        break;
                    default:
                        mySubstate = 'Pending disposal';
                        //console.log("set substate 'Pending disposal' as default");
                        break;          
                }
                //set substate to hardware record
                agGR.substatus = mySubstate;
                ahGR.update();

                //add work note to INC ticket for tracking.
                workString = "Asset action set to " + myAction + ".  Action reason set to " + myReason + ".  "
                workString += "Setting the substate of asset " + tsGR.ci_item.name.toString() + " to " + mySubstate + ".";
                current.work_notes = workString;
            }
        }
    }
    //update the INC ticket with updated notes.
    current.update();
    // Add your code here
})(current, previous);



(also trying to update the work notes in the INC to help track this as well, but that does not seem to be working at all, even tried this by bringing up an existing INC ticket that is open and still cannot seem to write to the work notes from the script, but I can edit them in the ticket themselves...)

 

1 ACCEPTED SOLUTION

Here is the code:

(function executeRule(current, previous /*null when async*/) {
	// pulling current from incident table
	var incNumber = current.number.toString();
	var tsGR = new GlideRecord("task_ci");
	tsGR.addEncodedQuery("task.number=" + incNumber);
	tsGR.query();
    
  
	//possible multiple task_ci records for this INC ticket if multiple CIs were selected in Affected CIs tab
	while(tsGR.next()){
		//collect data to test.  Remove case for easier testing (make tests lower case), just in case.
		var myAction = tsGR.asset_action.toString().toLowerCase();
		var myReason = tsGR.u_action_reason.toString().toLowerCase();
		//set default for substate(substatus).
		var mySubstate = 'pending_disposal';
		var workString = '';

    
		if(myAction == 'swap'){
			//only continue if the action was swap(?)  Should not need for other options.
			
			//capture the serial number to find the asset record
			var ciSerial = tsGR.ci_item.serial_number.toString();
      
			//get the asset data for ci related to this task_ci
			var ahGR = new GlideRecord('alm_hardware');
			ahGR.addEncodedQuery('serial_numberSTARTSWITH' + ciSerial);
			ahGR.query();

			if(ahGR.next()){
				gs.info("asset tag is " + ahGR.asset_tag);     
				switch(true){
					case myReason.includes("fix"):
						mySubstate = 'pending_repair';
						//console.log("set substate 'Pending repair' if action reason contains 'fix'");
						break;
					case myReason.includes("role"):
						mySubstate = '	available';
						//console.log("set substate 'Available' if action reason contains 'role'");
						break;
					default:
						mySubstate = 'pending_disposal';
						//console.log("set substate 'Pending disposal' as default");
						break;			
				}
        
				gs.info("Substate was : " + ahGR.substatus);
        
				//set substate to hardware record
				ahGR.setValue('substatus', mySubstate);
				ahGR.update();
        
				gs.info("setting substate of " + mySubstate);

				//add work note to INC ticket for tracking.
				workString = "Asset action set to '" + myAction + "'.  Action reason set to '" + myReason + "'.  "
				workString += "Setting the substate of asset " + tsGR.ci_item.name.toString() + " to '" + mySubstate + "'.";

        
  
				//gs.info("Adding work notes : ");
				//gs.info(workString);
			}else{
				//if asset found with the CI serial number, then error.  All CIs should have an asset record.
				workString = 'No asset found with matching serial number to Configuration Item : ' + tsGR.ci_item.name.toString();
				gs.info("ERROR - no asset found");
			}
			//update the INC ticket with updated notes if there are any
			if(workString != ''){
				current.work_notes.setJournalEntry(workString);        
				gs.info("Update ticket with work notes");
				current.update();  
			}
		}
	}
})(current, previous);

 

There are a few gs.info lines still in there, I did testing in Xplore where I made current a glide record of a specific inc ticket.

This code also take into account that I have added a custom field to the task_ci table called 'u_action_reason' which has the following values:

Action reason

Break/Fix - No Change

Break/Fix - InStock Asset

Break/Fix - New Asset

Role Change - InStock Asset

Role Change - New Asset

Retire - InStock Asset

Retire - New Asset

Lost - InStock Asset

Lost - New Asset

Stolen - InStock Asset

Stolen - New Asset

View solution in original post

4 REPLIES 4

yerasu reddy
Tera Contributor

Hello Jason, 

If you find the answer, please tell me know, I think there is an OOTB business rule or script include for this

seems I need someone to proof read my code.  my 'while(ahGR.next)' was missing () after next.  This does trigger and work as expected, but I didn't seem to find a business rule.  The documentation states that 2 event run, but nothing about a business rule.  I am too new to SN to know about dealing with events...

For now I got it working and can re-share the code if you want.  I set it to run 'after - update' with the condition that the state of the incident changes to 'resolved'.  We are doing some testing now to see that it all works as expected, but for now, prelim testing seems good.

 

Yup, you can share me the code

Here is the code:

(function executeRule(current, previous /*null when async*/) {
	// pulling current from incident table
	var incNumber = current.number.toString();
	var tsGR = new GlideRecord("task_ci");
	tsGR.addEncodedQuery("task.number=" + incNumber);
	tsGR.query();
    
  
	//possible multiple task_ci records for this INC ticket if multiple CIs were selected in Affected CIs tab
	while(tsGR.next()){
		//collect data to test.  Remove case for easier testing (make tests lower case), just in case.
		var myAction = tsGR.asset_action.toString().toLowerCase();
		var myReason = tsGR.u_action_reason.toString().toLowerCase();
		//set default for substate(substatus).
		var mySubstate = 'pending_disposal';
		var workString = '';

    
		if(myAction == 'swap'){
			//only continue if the action was swap(?)  Should not need for other options.
			
			//capture the serial number to find the asset record
			var ciSerial = tsGR.ci_item.serial_number.toString();
      
			//get the asset data for ci related to this task_ci
			var ahGR = new GlideRecord('alm_hardware');
			ahGR.addEncodedQuery('serial_numberSTARTSWITH' + ciSerial);
			ahGR.query();

			if(ahGR.next()){
				gs.info("asset tag is " + ahGR.asset_tag);     
				switch(true){
					case myReason.includes("fix"):
						mySubstate = 'pending_repair';
						//console.log("set substate 'Pending repair' if action reason contains 'fix'");
						break;
					case myReason.includes("role"):
						mySubstate = '	available';
						//console.log("set substate 'Available' if action reason contains 'role'");
						break;
					default:
						mySubstate = 'pending_disposal';
						//console.log("set substate 'Pending disposal' as default");
						break;			
				}
        
				gs.info("Substate was : " + ahGR.substatus);
        
				//set substate to hardware record
				ahGR.setValue('substatus', mySubstate);
				ahGR.update();
        
				gs.info("setting substate of " + mySubstate);

				//add work note to INC ticket for tracking.
				workString = "Asset action set to '" + myAction + "'.  Action reason set to '" + myReason + "'.  "
				workString += "Setting the substate of asset " + tsGR.ci_item.name.toString() + " to '" + mySubstate + "'.";

        
  
				//gs.info("Adding work notes : ");
				//gs.info(workString);
			}else{
				//if asset found with the CI serial number, then error.  All CIs should have an asset record.
				workString = 'No asset found with matching serial number to Configuration Item : ' + tsGR.ci_item.name.toString();
				gs.info("ERROR - no asset found");
			}
			//update the INC ticket with updated notes if there are any
			if(workString != ''){
				current.work_notes.setJournalEntry(workString);        
				gs.info("Update ticket with work notes");
				current.update();  
			}
		}
	}
})(current, previous);

 

There are a few gs.info lines still in there, I did testing in Xplore where I made current a glide record of a specific inc ticket.

This code also take into account that I have added a custom field to the task_ci table called 'u_action_reason' which has the following values:

Action reason

Break/Fix - No Change

Break/Fix - InStock Asset

Break/Fix - New Asset

Role Change - InStock Asset

Role Change - New Asset

Retire - InStock Asset

Retire - New Asset

Lost - InStock Asset

Lost - New Asset

Stolen - InStock Asset

Stolen - New Asset