TypeScript GlideRecord's Type Parameter

UIB Whisperer
Tera Expert

Hey there!

I've converted an existing application still in development to a NowSDK version of it and I've ran into a problem when it comes to declaring the type parameter of the GlideRecord class. I want to be able to dot-walk into the GlideElement objects of the GlideRecord one like in the example below which I've invented.

 

import {
	GlideAggregate,
	GlideDateTime,
	GlideRecord,
	GlideScopedEvaluator,
} from '@servicenow/glide'

export default class AccessRequestAPI {
	endpoint_gr: GlideRecord<HTTPMethod>
	rest_message_gr: GlideRecord<RESTMessage>
	constructor(endpoint_gr: GlideRecord<HTTPMethod>) {
		this.endpoint_gr = endpoint_gr;
		
		this.rest_message_gr = this.endpoint_gr.rest_message.getRefRecord(); // Returns String -> Error cause not a valid type
	}
}

 

 I know I can use the "any" type and TypeScript will hide the message when the transpiler is set to not strict but having it as strict is a best practice and I want to know if there's a way that I'm not seeing to achieve this.

 

Thanks in advance!

1 ACCEPTED SOLUTION

Venkat79
ServiceNow Employee

Hi @UIB Whisperer,

We donot support dot-walking for table fields or other types in GlideRecord yet.

As of now, the sole purpose of the types in "@servicenow/glide" package is to provide intelli-sense in the IDE and to make modules aware of the Glide APIs available at runtime.

And types for "RestMessageV2" are available under the "sn_ws" namespace, which can be imported in modules using

import { RestMessageV2 } from "@servienow/glide/sn_ws"


Thanks

View solution in original post

7 REPLIES 7

New Developer_S
Giga Sage

@UIB Whisperer 

When using ServiceNow's NowSDK in TypeScript, the GlideRecord type parameter allows you to specify the table schema for dot-walking into the record fields. In your code, the issue may arise because getRefRecord() returns a GlideRecord but does not carry over the type information to the returned record.

Can you try below code:

import {
GlideAggregate,
GlideDateTime,
GlideRecord,
GlideScopedEvaluator,
} from '@servicenow/glide';
 
export default class AccessRequestAPI {
endpoint_gr: GlideRecord<HTTPMethod>;
rest_message_gr: GlideRecord<RESTMessage>;
 
constructor(endpoint_gr: GlideRecord<HTTPMethod>) {
this.endpoint_gr = endpoint_gr;
 
// Cast the result of getRefRecord() to GlideRecord<RESTMessage>
this.rest_message_gr = this.endpoint_gr.rest_message.getRefRecord() as GlideRecord<RESTMessage>;
}
}

we are Explicitly casting  the result of getRefRecord() to the desired GlideRecord type.

 

Hope this will sort you issue, if yes ,mark as solution accepted and or hit helpful.

 

Hi @New Developer_S, thank you so much for your response. This was my initial approach but I could not figure out where to import the type of tables like HTTPMethod or RESTMessage.

Ravi Gaurav
Giga Sage

Hi @UIB Whisperer 

 

To summarize, you want to:

--> Use type parameters with the GlideRecord class to enable dot-walking into GlideElement objects

--> Avoid using the any type and instead find a more specific type that works with your code.

import { GlideRecord } from '@servicenow/glide';

interface TypedGlideRecord<T> extends GlideRecord {
[key: string]: GlideElement<T>;
}

export default class AccessRequestAPI {
endpoint_gr: TypedGlideRecord<HTTPMethod>;
rest_message_gr: TypedGlideRecord<RESTMessage>;

constructor(endpoint_gr: TypedGlideRecord<HTTPMethod>) {
this.endpoint_gr = endpoint_gr;
this.rest_message_gr = this.endpoint_gr.rest_message.getRefRecord() as TypedGlideRecord<RESTMessage>;
}
}

Hi @Ravi Gaurav, thank you for your response. I believe that the approach that you're taking would require me to explicitly define each of the attributes that a table has and the attribute/column type which not something I'm looking for.