Covert variables to number using Number() before incrementing it.

Vijay Baokar
Kilo Sage

Hi All,

I was working on a small requirement where i need to capture the count and then do the increment by 1 whenever ticket is updated by caller or assignee, below code is written and working but i want to 

Covert countOfComReq, countOfComAgent variables to number using Number() before incrementing it.    

 

if(gs.getUserID()== current.caller){
var countOfComReq = current.getValue('u_communication_count');    
        countOfComReq++;
        current.setValue('u_communication_count', countOfComReq.toString());
}
if(gs.getUserID()== current.assigned_to){
var countOfComAssignee  = current.getValue('u_communication_count_by_assignee');
        countOfComAssignee  ++;
        current.setValue('u_communication_count_by_assignee', countOfComAssignee .toString());
}
 
below conversion need to be applied in above code
 

If (countOfComReq ) {

    countOfComReq = Number(countOfComReq );

} else {

    countOfComReq = 0;

}

 

countOfComReq++;

 

Note: countOfComReq and u_communication_count_by_assignee both fields are string type.

   
1 ACCEPTED SOLUTION

Community Alums
Not applicable

Hi @Vijay Baokar 

 

We can use Number() in ServiceNow Scripting. For reference check the below link 

https://www.servicenow.com/community/developer-articles/useful-number-methods-in-javascript/ta-p/232...

 

 

if (gs.getUserID() == current.caller) {
    var countOfComReq = current.getValue('u_communication_count');
    countOfComReq = Number(countOfComReq); // Convert to a number
    countOfComReq++;
    current.setValue('u_communication_count', countOfComReq.toString());
}

if (gs.getUserID() == current.assigned_to) {
    var countOfComAssignee = current.getValue('u_communication_count_by_assignee');
    countOfComAssignee = Number(countOfComAssignee); // Convert to a number
    countOfComAssignee++;
    current.setValue('u_communication_count_by_assignee', countOfComAssignee.toString());
}

 

 

Mark the comment as a correct answer and also helpful if this has helped to solve the problem.

Krishna Sharma

View solution in original post

2 REPLIES 2

Brad Bowman
Kilo Patron
Kilo Patron

I don't know that Number() is a valid JavaScript method.  Use parseInt() instead.

Community Alums
Not applicable

Hi @Vijay Baokar 

 

We can use Number() in ServiceNow Scripting. For reference check the below link 

https://www.servicenow.com/community/developer-articles/useful-number-methods-in-javascript/ta-p/232...

 

 

if (gs.getUserID() == current.caller) {
    var countOfComReq = current.getValue('u_communication_count');
    countOfComReq = Number(countOfComReq); // Convert to a number
    countOfComReq++;
    current.setValue('u_communication_count', countOfComReq.toString());
}

if (gs.getUserID() == current.assigned_to) {
    var countOfComAssignee = current.getValue('u_communication_count_by_assignee');
    countOfComAssignee = Number(countOfComAssignee); // Convert to a number
    countOfComAssignee++;
    current.setValue('u_communication_count_by_assignee', countOfComAssignee.toString());
}

 

 

Mark the comment as a correct answer and also helpful if this has helped to solve the problem.

Krishna Sharma