Having issues with a script

Lindsey6
Giga Contributor

Hello,

I am currently working on the training module (Integrating with ServiceNow; Exercise: Create an inbound web service) and the script that is provided is invalid. I am looking for some help/guidance on how to fix it so I can move forward with this training.

Error: "Could not save record because of a compile error: JavaScript parse error at line (27) column (2) = syntax error (<refname>;line 27)

//Convert source field to string
(function runTransformScript(source, map, log, target /*undefined onStart*/ ) {
      var name = source.u_name.toString();

//Split names anywhere there is a space
      var split_names = name.split (" ");

//Find the number of names
      var num_names = split_names.length;

//If there is only one name map it to the last name
          if (num_names == 1) {
              target.last_name = split_names[0];
}

//if there are two names map to first and last name      
          if (num_names == 2) {      
              target.first_name = split_names[0];
          target.last_name = split_names[1];
}

//if there are more than 3 names combine into one middle name
      if (num_names >= 3) {
          target.first_name = split_names.shift();
      target.last_name = split_names.pop();
          var middle_name = split_names.join(" ");
})(source, map, log, target);

Thank you,

Lindsey

8 REPLIES 8

Rajesh Mushke
Mega Sage
Mega Sage

Hi Herman,



Remove space between split and ()



replace below code at your second line



      var split_names = name.split(" ");




Thanks,
Rajashekhar Mushke
Rising star : 2022 - 2024
Community Leader -2018
Connect me on LinkedIn : Rajashekhar Mushke

var CustomCatalogUtils = Class.create();
CustomCatalogUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    var ans = '';
    var gr = new GlideRecord('sc_req_item');
    gr.addEncodedQuery('requested_for=62826bf03710200044e0bfc8bcbe5df1^stateNOT IN4,7^cat_item=4cc49ffec3845210cb85b34ed401318f');
    gr.query();
    if (gr.next()) {
        ans = 'records is found' + gr.number;
    } else {
        ans = 'There is no existing records';

    }
    type: 'CustomCatalogUtils'
});
 
in above code while saving it give me below error can you please help.
Could not save record because of a compile error: JavaScript parse error at line (3) column (8) problem = invalid property id (<refname>; line 3)

Lindsey6
Giga Contributor

Hi Rajesh,



What line do you want me to remove the space between splint and () ?



I also tried replacing line # 2 with the code you provided and it brought up a lot more errors.



Thanks,


Lindsey


Shiva Thomas
Kilo Sage

Hi Lindsey,



Rajesh answer is not correct. You were missing a bloc closure at the end: }



Here is the corrected version:


//Convert source field to string


(function runTransformScript(source, map, log, target /*undefined onStart*/ ) {


  var name = source.u_name.toString();



  // Split names anywhere there is a space


  var split_names = name.split(" ");



  // Find the number of names


  var num_names = split_names.length;



  // If there is only one name map it to the last name


  if (num_names == 1) {


      target.last_name = split_names[0];


  }



  // if there are two names map to first and last name      


  if (num_names == 2) {      


      target.first_name = split_names[0];


      target.last_name = split_names[1];


  }



  // if there are more than 3 names combine into one middle name


  if (num_names >= 3) {


      target.first_name = split_names.shift();


      target.last_name = split_names.pop();


      var middle_name = split_names.join(" ");


  }


})(source, map, log, target);



Tip: I suggest you download a dedicated code editor to get line and column numbers. I use Atom, a free editor that runs on Windows and MacOs.