Refname Syntax error in Script Include

StandardSanders
Giga Guru

I am getting the following error:
"Could not save record because of a compile error: JavaScript parse error at line (14) column (24) problem = syntax error (<refname>; line 14)"

Here is my code:

 var Search = Class.create();
 Search.prototype = {
    initialize: function authenticate() {
  const url = 'instanceurl';
  const username = 'admin';
  const password = 'password';
  const requestOptions = {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ username:username, password:password })};
 
		return fetch(url, requestOptions)
      .then(response => response.json()).catch(e => console.error(e))
      .then(data => {
      const accessToken = data.access_token;
      return accessToken;
    });
}
}
// Create query function to query ServiceNow for relevant articles and data
 function queryServiceNow(accessToken) {
  const url = 'instanceurl';
  const query = 'myQuery';
  const requestOptions = {
    method: 'post',
    type: 'Search'
}}

 

4 REPLIES 4

Sai Shravan
Mega Sage

Hi @StandardSanders ,

The code you provided seems incomplete, and there is a syntax error on line 21 where type: 'Search' is not properly formatted. It seems that a value is missing after the colon.

 

Moreover, I noticed that your initialize method returns but it's not clear how or where it's being used. Additionally, the authenticate method is named incorrectly, as it appears to be responsible for authenticating the user with the ServiceNow API rather than initializing the Search class.

 

Here's a corrected version of your code that should address the syntax error on line 21 and the naming issue:

var Search = Class.create();
Search.prototype = {
  initialize: function() {
    const url = 'instanceurl';
    const username = 'admin';
    const password = 'password';
    const requestOptions = {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({ username:username, password:password })
    };
 
    this.accessToken = fetch(url, requestOptions)
      .then(response => response.json())
      .then(data => {
        return data.access_token;
      })
      .catch(e => console.error(e));
  },
  queryServiceNow: function() {
    const url = 'instanceurl';
    const query = 'myQuery';
    const requestOptions = {
      method: 'post',
      type: 'search'
    };
    
    // TODO: Add code to query ServiceNow using the access token
  }
};

In this updated code, the initialize method initializes the Search object and obtains an access token from the ServiceNow API. The accessToken is stored as a property of the Search object, and can be accessed by other methods. The queryServiceNow method is a placeholder for your actual query code, and currently just sets up the request options. Note that type: 'search' has been changed to lowercase to match the expected value for the requestOptions object.

 

Please note that this code is just a suggestion and may need further adjustments to fit your specific use case.

Regards,
Shravan

Regards,
Shravan
Please mark this as helpful and correct answer, if this helps you

Thanks, I gave this a shot just to see if I could save it and I get the same error on the same line:

.then(response => response.json())



StandardSanders_0-1676654968484.png

 

-O-
Kilo Patron
Kilo Patron

Where are you executing this code (scoped or global)?

Global on the Global domain (I'm on a Domain Sep instance) and in Script Includes.