How to make a GlideRecord read only?

Daniel Oderbolz
Kilo Sage

Hi

I have a situation where I pass a GlideRecord to a cutomer-defined function to perform some check.
In order to avoid the customer to mess with the GlideRecord (intentionally or uninitentionally), I would like to make the GlideRecord read-only before passing it to the function.

Is this possible?

Many thanks for your insights!

Cheers
Daniel


If this answer was helpful, I would appreciate if you marked it as such - thanks!

Best
Daniel
1 ACCEPTED SOLUTION

Hello Daniel,

 

I am not inclined towards implementation of this use case. if customer wants any data, then I would have passed the JSON or any other objects with data. Surely will not pass GlideRecord object and then worry about update on the database.

but if you are up with the requirement implementation, then below are my suggestions.

 

I don't think we can make GlideRecord Object read only. If you are passing GlideRecord object of particular table, you can have a before insert/update BR on the table to identify if update is from that script and then abort the action. here you need to find a way to identify the if he action is from those script.

 

another way to ensure no database actions on the GlideRecord (for any table) would be to set the abort action to true and then pass the object.

setAbortAction(true);

 

i.e before passing the GlideRecord object, use gr.setAbortAction(true); so that even if gr.update() executed from another end, it will not update the records in database. 

 

Note: but if another end before gr.update(), if they use serAbortAction(false) then they can update the database record. but it is TRUST that they should not be update intentionally with this kind.

 

Thanks,

ALi

If I could help you with your Query then, please hit the Thumb Icon and mark my answer as Correct!!

Thank you,
Ali

View solution in original post

5 REPLIES 5

Dear Ali

Thanks for your suggestion.
You are right that it is more prudent to implicitly create a JS Object (does not even need to be JSON) and pass this.
So, I will write a function like this:

/**
 * Given any GlideRecord creates a JS Array of Objects with the fields (read only)
 * Note that dot-walking is not possible, references are given as sys_ids
 *
 * @function GlideRecordToReadOnlyObj
 * @param {gr:any} grAny - Glide Record of any table
 * @returns {object} - an object where each field of the Glide Record is a property 
 * @author Daniel C. Oderbolz
 */
function GlideRecordToReadOnlyObj(grAny)
{
	var result = [];

	while (grAny.next()) 
	{
		var record = {};

		//get all the fields for this record
		var fields = grAny.getFields();
		for (var i = 0; i < fields.size(); i++) 
	    {
			var glideElement = fields.get(i);
			strFieldName = glideElement.getName();

			//Store field
			record[strFieldName] = grAny.getValue(strFieldName);
			
		} // each field
		
		result.push(record);

	} // each row;

	return result;
}

Having said that, I also like the idea of the abort action!

Thanks a lot & Best
Daniel


If this answer was helpful, I would appreciate if you marked it as such - thanks!

Best
Daniel