Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

How can I generate a History Set record using a script?

SoniShaily
Tera Contributor

Hi everyone,

I'm working within a scoped application and trying to programmatically generate a History Set record. I found the GlideHistorySet class in the documentation, but I'm not entirely sure about the correct usage or best practices for implementing it in a script.

Has anyone successfully done this before? I'd appreciate any guidance or examples.

Thanks in advance!

1 ACCEPTED SOLUTION

Rushi Savarkar
Mega Sage

Hello @SoniShaily 

 

You're on the right track using the GlideHistorySet API. There are a couple of points to keep in mind for clarity and reliability. Also, .refresh() is typically only needed for UI refreshes and may not be necessary in all cases.

Here’s a simplified and corrected script you can use:

// Load the record you want to generate history for

var record = new GlideRecord(tableName);

if (record.get(documentKey)) {

    try {

        // Create a history set object and generate the history

        var historySet = new GlideHistorySet(tableName, documentKey);

        historySet.generate();

 

        // Optional: refresh the history set (useful for UI updates)

        // historySet.refresh(); // Only if needed

    } catch (e) {

        gs.info("Error generating history set: " + e.message);

    }

}

Let me know if you're working with a specific table or use case—happy to help further!

If my response helped you, please accept the solution and mark it as helpful.
Thank You!

View solution in original post

2 REPLIES 2

shubhamseth
Giga Sage

@SoniShaily 

 

 

Use 

GlideSysHistoryLine

 (Manually log record access)

 

 

If you want to track record access or build an audit log:

 

var historyLine = new GlideRecord('sys_history_line');
historyLine.initialize();
historyLine.setValue('documentkey', 'incident:' + recordSysId);
historyLine.setValue('tablename', 'incident');
historyLine.setValue('user', gs.getUserID());
historyLine.setValue('viewed', new GlideDateTime());
historyLine.insert();


Safe, auditable, and scoped-compatible.

You can query these records to build a “recently accessed” view or custom history tracking.

 

 

✔️ If this solves your issue, please mark it as Correct.


✔️ If you found it helpful, please mark it as Helpful.



Shubham Jain


Rushi Savarkar
Mega Sage

Hello @SoniShaily 

 

You're on the right track using the GlideHistorySet API. There are a couple of points to keep in mind for clarity and reliability. Also, .refresh() is typically only needed for UI refreshes and may not be necessary in all cases.

Here’s a simplified and corrected script you can use:

// Load the record you want to generate history for

var record = new GlideRecord(tableName);

if (record.get(documentKey)) {

    try {

        // Create a history set object and generate the history

        var historySet = new GlideHistorySet(tableName, documentKey);

        historySet.generate();

 

        // Optional: refresh the history set (useful for UI updates)

        // historySet.refresh(); // Only if needed

    } catch (e) {

        gs.info("Error generating history set: " + e.message);

    }

}

Let me know if you're working with a specific table or use case—happy to help further!

If my response helped you, please accept the solution and mark it as helpful.
Thank You!