- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-15-2024 09:02 PM
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!
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-16-2024 05:20 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-15-2024 10:45 PM
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:
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-16-2024 03:40 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-15-2024 11:36 PM
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>;
}
}
If you found my response helpful, I would greatly appreciate it if you could mark it as "Accepted Solution" and "Helpful."
Your support not only benefits the community but also encourages me to continue assisting. Thank you so much!
Thanks and Regards
Ravi Gaurav | ServiceNow MVP 2025,2024 | ServiceNow Practice Lead | Solution Architect
CGI
M.Tech in Data Science & AI
ï”— YouTube: https://www.youtube.com/@learnservicenowwithravi
ï”— LinkedIn: https://www.linkedin.com/in/ravi-gaurav-a67542aa/
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-16-2024 03:41 PM
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.