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

So, in your main function you have two if statements that would both potentially evaluate to true: 

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

If the email subject contains "rejected" then index of reject would be true as well, resulting in Reject() being called twice. 

Try replacing those conditions with something like this: 

if(email.subject.toLowerCase().search(/\breject\b/) >= 0)
{
Reject();
}					
if(email.subject.toLowerCase().search(/\brejected\b/) >= 0)
{
Reject();
}

That should only return true for an exact match on reject or on rejected, rather than both. 

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!

Replacing the code as you mentioned above did not work as it was showing error. 

Then I modified my code as below and now it is running once. Hope this will not cause any issues.

/*if(email.subject.toLowerCase().indexOf("reject") >= 0)
{
Reject();
}	*/				
if(email.subject.toLowerCase().indexOf("rejected") >= 0)
{
Reject();
}