I AM UNABLE TO SAVE THE BELOW CODE DUE RED LINE ERROR

mani55
Tera Contributor

I AM UNABLE TO SAVE THE BELOW CODE DUE RED LINE ERROR  LET ME KNOW MY MISTAKE

 

 

mani55_1-1754396253702.png

 

6 REPLIES 6

 // Check service account

     if (request_body.account_serv == null || request_body.account_serv == '') {
         api_catalog.error = true;
         api_catalog.message = "Mandatory to enter service account.";
     } else {
         var grComputer = new GlideRecord('u_alm_service');
         grComputer.addEncodedQuery('model=38f6d0d297ea26102bfa71300153af12^install_statusIN2,1');
         grComputer.setLimit(1);
         grComputer.query();
         if (grComputer.next()) {

             var diskGr = new GlideRecord('u_cmdb_ci_sc_office_365_for_service_account');
             diskGr.addQuery('u_service_account.name', request_body.account_serv);
             diskGr.setLimit(1);
             //gs.log('the user account' + request_body.account_serv);
             diskGr.query();
             if (diskGr.next()) {
                 api_catalog.error = true;
                 //gs.log('the  account' + diskGr.u_service_account);
                 api_catalog.message = request_body.account_serv + ' already has an active service for Office365 for Generic and Service accounts ' + grComputer.u_number;

             } else {
                 //gs.log('the  account1' + diskGr.u_service_account);
                 api_catalog.test_and_SetVariable_Reference("sys_user", "user_name", "account_serv", request_body.account_serv, "sys_id", 'u_employee_type=Service Account^active=true^user_name!=S-TCF', 'Invalid service account.');
             }
         }
     }

iekosmadakis
Mega Sage

Hello @mani55 !
The issue is in this part of the code:

 

while (diskGr.next()) {
…
} else { // <-- this “else” has nothing to pair with


JavaScript (and therefore ServiceNow server-side scripting) only allows else to follow an if block. There is no while … else construct. Thus, the script editor shows a red underline because the parser cannot match that else to any preceding if.

 

Because diskGr.setLimit() ensures at most one record is returned, iterating with a while loop is unnecessary. I suggest to simply replace the while(diskGr.next()) with if(diskGr.next()). This adjustment keeps the logic intact while eliminating the stray else that triggered the syntax error, allowing the script to save cleanly.

 

Please consider marking my answer as helpful and accepting it as the solution if it assisted you in any way.