Coding using indexof not working

Laxmi18
Tera Expert

Hi ,

 

I have a field "text field" called Grade code in sys_user table.

When a requested for's  grade code vale in the sys _user table is greater than Level 11(example we have  Level 12  Level 13 and soo on ) then a task should trigger for certain group.

 

i have used index of to check the 11 and applied greater than but it is not working. 

 

NOTE: I have to use workflow for this

2 REPLIES 2

M Ismail
Tera Guru

Hi @Laxmi18,

If you're attempting to extract a numeric value from a string field like "Level 12", you need to use string manipulation functions rather than `indexOf()`, as `indexOf()` only returns the position of the specified substring within the string.

To achieve this in ServiceNow workflows, you can use a combination of string manipulation functions like `substring()`, `parseInt()`, and possibly `match()` or `split()` depending on the format of your data.

Here's how you can approach it:

1. **Extract the Numeric Value**:
- Use a string manipulation function to extract the numeric portion of the Grade code field. Assuming your Grade code field always follows the format "Level X", you can use `substring()` to extract the numeric part.

2. **Convert to Integer**:
- Once you have extracted the numeric value, convert it to an integer using `parseInt()`.

3. **Compare with the Threshold**:
- Compare the extracted numeric value with your threshold value (in this case, 11).

Here's a simplified example of how you might implement this in a workflow condition:

```javascript
// Assuming 'gradeCode' is the name of your Grade code field
var gradeCode = current.grade_code; // Replace 'grade_code' with your actual field name
var numericValue = parseInt(gradeCode.substring(6)); // Extract numeric part and convert to integer

// Check if the numeric value is greater than 11
if (numericValue > 11) {
// Trigger task for certain group
gs.addInfoMessage("Grade code is greater than 11.");
answer = true; // This condition is met
} else {
answer = false; // This condition is not met
}
```

Adjust the field names and logic according to your specific requirements and the format of your Grade code field. This example assumes that the Grade code field always starts with "Level " followed by the numeric value. You may need to adjust the substring index if your field format is different.

Please hit helpful and accept this as a solution if it solved your problem.
Thank you!

shikhatyagi
Kilo Sage

Hi @Laxmi18 

 

You are trying to compare string type value to the integer. First you need to Extract the Number part from Grade Code and then convert it to Integer. Then you can compare the value with 11.

Use below code


var grade=current.u_grade_code;
grade=grade.substr(5);
grade=parseInt(grade);
if(grade>11)
{
gs.addInfoMessage("Grade is >11");
}
else{
gs.addInfoMessage("Grade is <11");
}

 

 

Please mark my answer as Accepted as Solution & hit helpful button if it helped you.

 

Thanks,

Shikha