I want to increment a field value by +1 through inbound action when approval is rejected. I have a script in place but it is incrementing the value by +2.

jyotsna1
Giga Contributor

Hi All,

I want to increment a field value by +1 through inbound action when approval is rejected. I have a script in place but it is incrementing the value by +2.

I'm pasting my code below, anyone please help in fixing my issue.

function RejectOperation()
{	
		var gr2 = new GlideRecord('cmdb_hardware_product_model');
		gr2.addQuery('sys_id',current.u_variable_1);
		gr2.query();
		if(gr2.next())
		{
			var assets_in_stock = gr2.u_assets_in_stock;
			if(gr2.u_ci_type == 'not_contain_ci')//if assests in stock
			{
				
				//gr2.u_assets_in_stock += 1; 
				//gr2.u_assets_in_stock++;
				gr2.u_assets_in_stock = assets_in_stock +1;
				gr2.update();
			}
11 REPLIES 11

When I add gr.setWorkFlow(false); to the script its giving below in the logs and not incrementing the field value

find_real_file.png

Sorry, I should say add gr2.setWorkflow(false); - basically whatever you do .update() for, do .setWorkflow(false). I also spelled it wrong originally - (sorry!)

 

I hope this helps!
Michael D. Jones
Proud member of the GlideFast Consulting Team!

Sorry! It is still updating +2.

Well, getting closer anyway - I think we've ruled out a business rule coming along and doing the update a second time. Try adding:

gs.info('Reject Operation called by: ' + current.sys_id.toString());

That will tell us if maybe more than one record is triggering your process. 

I hope this helps!

If this was helpful or correct, please be kind and remember to click appropriately!

Michael Jones - Proud member of the CloudPires team!

I hope this helps!
Michael D. Jones
Proud member of the GlideFast Consulting Team!

It shows same sys_id in both the log records. Attaching the full inbound action script, please suggest if something wrong in the script

gs.include('validators');
gs.info('New Inbound email action for Licence manager approval is triggered.');
if (current.getTableName() == "sc_request") {
  //current.comments = "reply from: " + email.origemail + "\n\n" + email.body_text;	// RJP
	if (gs.hasRole("itil")) {
		
		if(email.from == gs.getProperty('thc.licence.manager.email')) //This If Block is for Licence Manager Approval
		{
				
				if(email.subject.toLowerCase().indexOf("approved") >= 0) //If licence manager approved Free User/User Licence metric REQ
				{
					LicenseManagerApproval();
				}
			
				if(email.subject.toLowerCase().indexOf("rejected") >= 0)
				 {
					 Reject();
				 }
				
				if(email.subject.toLowerCase().indexOf("reject") >= 0)
				 {
					 Reject();
				 }
				
				if(email.subject.toLowerCase().indexOf("denied") >= 0)
					{
						Reject();
					}
				
			}
		else if((email.from == current.u_one_of_budget_code.u_email) || (email.from == current.u_reoccuring_budget_code.u_email))
			// This Block is for Budget Holder Approval
			{
				if (email.body.assign != undefined)
				current.assigned_to = email.body.assign;
		
				if (email.body.priority != undefined && isNumeric(email.body.priority))
					current.priority = email.body.priority;

				if(email.subject.toLowerCase().indexOf("approved") >= 0)
					{
						Approve();								
					}
				
				if(email.subject.toLowerCase().indexOf("denied") >= 0)
					{	
						Reject();
					}
				
				if(email.subject.toLowerCase().indexOf("reject") >= 0)
					{
						Reject();
				    }					
				if(email.subject.toLowerCase().indexOf("rejected") >= 0)
					{
						Reject();
				    }	
			}
		}
}

function Approve()
{
	/**************************
	 * This If block is for Hardware Catalogue
	 ****************/
	if((current.u_category == 'Hardware')&&(current.u_sub_category == 'Install'))
		{
			if((current.u_variable_6 != '')&&(current.u_variable_5 == '1'))
				{
					//Trigger recurring budget holder approval
					gs.eventQueue('budgetholder.approval',current,current.u_variable_6);
					current.u_variable_5 = '0';
					current.update();
				}
			else
				{
					
					//Now if the Approval request Contains CI Need to update the CMDB CI record with Pricing info
					
					var asset = new GlideRecord('cmdb_ci');
					asset.addQuery('sys_id',current.u_variable_2);
					asset.query();
					if(asset.next())
						{
							asset.u_wipro_ref = current.number;
							asset.u_capital_budget_code = current.u_variable_3;
							asset.u_asc_budget_code = current.u_variable_4;
							asset.u_purchase_price = current.price;
							asset.update();
						}			
					
					current.state = 2; //Setting state to Active.
					current.update();	
				}
		}
	/************* 
	 * This if block is for Software Catalogue.
	 * This is for creating record when initial license is not available case
	 *******/
	
	else if((current.u_category == 'Software') && (current.u_sub_category == 'Install'))
		{
			createSoftwareInstanceRecord();			
		}
	else // For all other catalogues
		{
			current.state = 2; //Setting state to Active.
			current.update();			
		}	
	
}

function createSoftwareInstanceRecord()
{
	var software = current.u_application;
	var device = current.u_cmdb_item_affected;
			
	//Check if the Record is there or not in software instance table.

	var si = new GlideRecord('cmdb_software_instance');
	si.addQuery('software',software);
	si.addQuery('installed_on',device);
	si.addEncodedQuery('u_state=Reserved^ORu_state=Consumed');
	si.query();
	if(!(si.next()))
	{
		/*************
		 * Create the software instance instance record.
		 * This is the case where License available changes from false to true and state changes from scheduled job
		 ***********/
		si.initialize();
		si.installed_on = device;
		si.software = software;
		si.u_state = 'Reserved';
		si.insert();					
	}
	current.state = 2; //Setting Request state to Active.
	current.update();	
}


function LicenseManagerApproval()
{
	//check if the Deployment Type of the REQ is Manual or Automatic.
	if(current.u_variable_5 == 1)//REQ is Manual. 
	{
		current.state = '5';//Awaiting Budget Approval
		gs.eventQueue('budgetholder.approval',current,current.u_variable_2);//Trigger one-off budget holder approval
		current.update();
	}
	else //REQ is Automatic
	{
		Approve();
	}
	
}


function Reject()
{
	var ci_name;
	current.state = 4; //Setting state to Reject.
	current.approval = 'rejected';
	current.close_notes = 'APPROVAL REJECTED/DENIED';
	
	if((current.u_category == 'Hardware')&&(current.u_sub_category == 'Install'))
		ci_name = RejectOperation();
	
	current.update();
	
	if((current.u_category == 'Hardware')&&(current.u_sub_category == 'Install')&&(ci_name != ''))
		current.work_notes = "Asset \'" +ci_name + "\' Unreserved";
}



function RejectOperation()
{	
		var gr2 = new GlideRecord('cmdb_hardware_product_model');
		gr2.addQuery('sys_id',current.u_variable_1);
		gr2.query();
		if(gr2.next())
		{
			var assets_in_stock = gr2.u_assets_in_stock;
			if(gr2.u_ci_type == 'not_contain_ci')//if assests in stock
			{
				
				//gr2.u_assets_in_stock += 1; 
				//gr2.u_assets_in_stock++;
				gr2.u_assets_in_stock = assets_in_stock +1;
				gs.info('Reject Operation called by: ' + current.sys_id.toString());
				gr2.setWorkFlow(false); 
				gr2.update();
			}
			else
			{
				/*** 
				 * Query CI table and then change the status to Non reserved
				 ***/
				var ci = new GlideRecord('cmdb_ci');
				ci.addQuery('sys_id',current.u_variable_2);
				ci.query();
				while(ci.next())
				{
					ci.u_status_of_new_stock = 'non_reserved';
					ci.update();
				}
				return ci.name;				
			}
		}
	  
}

find_real_file.png