Workflow if condition is not working as expected

surajsukuma
Tera Contributor

In one of Catalog item, I created a single line text variable with Validation Regex as currency.  

surajsukuma_0-1732032955319.png

 

I need if condition if the Variable value >$5000 should go to approval task if no then no approval 

 

I created condition like below screenshot and its not working as expected, approval is going <$5000 as well

 

surajsukuma_1-1732033144263.png

 

Any once can suggest what is wrong with this?

 

Thanks in advance 

 

 

1 ACCEPTED SOLUTION

You should be using a Script with the Advanced box checked, which means you need to delete the Condition so that the script runs without being influenced by the condition.  If it still doesn't work with the latest script I proposed with the temporary log to confirm the value, (which works in a background script using hard-coded values as your test case) try removing the $ as @mahemoodhus suggested, but in your script it would just be:

answer = ifScript();

function ifScript() {
    var amt = parseInt(current.variables.expected_amount.toString().replace('$', '');
    workflow.info('amount is ' + amt);
    if (amt <= 5000) {
        return 'yes';
    }
    return 'no';
}

View solution in original post

9 REPLIES 9

You should be using a Script with the Advanced box checked, which means you need to delete the Condition so that the script runs without being influenced by the condition.  If it still doesn't work with the latest script I proposed with the temporary log to confirm the value, (which works in a background script using hard-coded values as your test case) try removing the $ as @mahemoodhus suggested, but in your script it would just be:

answer = ifScript();

function ifScript() {
    var amt = parseInt(current.variables.expected_amount.toString().replace('$', '');
    workflow.info('amount is ' + amt);
    if (amt <= 5000) {
        return 'yes';
    }
    return 'no';
}

Looks this worked 

 

mahemoodhus
Tera Contributor
  • Hi surajsukuma,

you can use a Catalog Item Workflow with proper conditions or write a Scripted Condition. Here's how you can address this issue:

 

  1. Open the approval activity in the workflow.
  2. Add a Scripted Condition and use the following script
  3. (function executeRule(current, gWorkflow) {
    // Get the value of the variable
    var amount = current.variables.your_variable_name; // Replace 'your_variable_name' with the actual name of your variable

    // Remove the '$' symbol and convert to a number if needed
    var numericAmount = parseFloat(amount.replace('$', '').trim());

    // Check if the amount is greater than $5000
    if (numericAmount > 5000) {
    return true; // Trigger approval
    }
    return false; // Skip approval
    })(current, gWorkflow);

  4. Must try this code to in your instance

 

 
Copy code
 

 

I already had run script to create Approval task, is it possible to include in this script itself 

 

var collector = current.variables.select_business_unit.toString(); // (Replace list_collector_variable with your variable name)
var list = collector.split(",");
for (var i = 0; i < list.length; i++) {
    var gr1 = new GlideRecord('business_unit'); //Replace table_name with the one of list collector variable
    if (gr1.get(list[i])) {
       // var store = gr1.u_business_owner;
        //var roledata = gr1.u_role;
        // answer.push('gr1.u_business_owner_email');
       // answer.push(store);
       
        var gr= new GlideRecord("sysapproval_approver");
        gr.initialize();
        gr.approver=gr1.u_finanace_partner;
        gr.source_table=current.sys_class_name;
        gr.document_id=current.sys_id;
        gr.sysapproval=current.sys_id;
        gr.u_roles=gr1.u_role;
        gr.state='requested';
        gr.insert();
       
   
    }

 

Ankur Bawiskar
Tera Patron
Tera Patron

@surajsukuma 

try this

answer = ifScript();

function ifScript() {
    var amt = current.variables.expected_amount.getValue();
    if (amt <= 5000) {
        return 'yes';
    }
    return 'no';
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

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