ifScript Activity in Workflow

michaelcory
Giga Expert

I have the following IF Activity script in Workflow (global table).   Variables are held in a custom Import table.   If I just evaluate the companyCode variable, the script works.   When I add the jobCode variable values the script does not evaluate correctly.   I want to return NO if the companyCode matches, but the jobCode does not.   Is there some other way to script this?

answer = ifScript();

function ifScript(){

  var companyCode = current.variables.company_code;

  var jobCode = current.variables.job_code;

  if(companyCode == 'SJC' || companyCode == 'SHE' && (jobCode != '2000' || jobCode != '2019' || jobCode != '2057' || jobCode != '2064' || jobCode != '2086' || jobCode != '2088' || jobCode != '2130' || jobCode != '2208')){

  return 'yes';

  } else {

  return 'no';

  }

}

1 ACCEPTED SOLUTION

Mike,



let me clarify the requirement, if companycode is either SJC or SHE and jobcode is not one of 2000,2019,2057,2064,2086,2088,2130,2208 then you need to return yes. am i right?



so shouldn't the if statement be:



  if((companyCode == 'SJC' || companyCode == 'SHE') && jobCode != '2000' && jobCode != '2019' && jobCode != '2057' && jobCode != '2064' && jobCode != '2086' && jobCode != '2088' && jobCode != '2130' && jobCode != '2208')



View solution in original post

14 REPLIES 14

Michael Ritchie
ServiceNow Employee
ServiceNow Employee

In my personal experience when I have a complex condition, I will break it up into smaller chunks and then evaluate those smaller chunks.   I would suggest evaluating the companies for a match and then evaluate the job code for a match.   Then check those overall matches for your final answer.   Plus it will be easier to read that way!



Btw you can shortcut your code by creating a list of the items you are looking for and then using the indexOf() function to check.



Example:


var validCompanies = ["SJC", "SHE"];


var validCompany = validCompanies.indexOf(current.variables.company_code) > -1



-1 means its not in that array, 0 or higher means it is.


Thank you for the tips!   I modified the script and ran a test using one of the Invalid Job Codes. Successful result.   I'll run another test with a Valid Job Code and see what it returns.



answer = ifScript();



function ifScript(){


  var Companies = ["SJCF","SHE"];


  var validCompany = Companies.indexOf(current.variables.company_code) >-1;


  var JobCodes = ["2000","2019","2057","2064","2086"];


  var invalidJobCodes = JobCodes.indexOf(current.variables.job_code) >-1;



  if(validCompany && !invalidJobCodes){



  return 'yes';


  } else {


  return 'no';


  }


}


Awesome glad to hear!   Please mark the appropriate posts helpful or the correct answer to your questions so others on the community can benefit.


I spoke too soon.   I'm getting a No result on a Valid Job Code as well.