Useful Scripts

 Get the content of attachment as a string

var attachment  =  new GlideSysAttachment(); 
var attachmentFile =  attachment.getAttachments('TABLE_NAME',SYS_ID); 
if (attachmentFile.next()){ 
var attachmentData =  attachment.getBytes(attachmentFile); 
var  content = String(Packages.java.lang.String(attachmentData)); 
}


 RecordFieldGetter

RecordFieldGetter is a script include OOB, containing only one function to get a single field value 
of a record from client side.
Ex: var ga = new GlideAjax(‘RecordFieldGetter’);
ga.addParam(‘sysparm_name, ‘getValue’);
ga.addParam(‘sysparm_table’, 'sys_user');
ga.addParam(‘sysparm_sys_id', g_user.userlD);
ga.addParam(‘sysparm_field_name’, 'user_name’);
ga.getXMLAnswer(function(table) {
alert(table);
})); 


Trick to update bulk Incident records without updating the updated by?

Use autoSysFields(false) 

👉🏻 this will disables the update to the fields sys_updated_by, sys_updated_on, sys_mod_count, sys_created_by, and sys_created_on. 

This is often used for manually updating field values on a record while leaving historical information unchanged. 

Please Note: This will not work on scoped apps. 

Script- 

var inc = new GlideRecord('incident'); 

inc.addQuery('state', 1); 

inc.query(); 

while (inc.next()) 

inc.autoSysFields(false); // Do not update sys_updated_by, sys_updated_on, sys_mod_count, sys_created_by, and sys_created_on 

inc.setValue('state', 2); 

inc.update(); 

}


Get user session IP address information

'GlideTransaction.get().getRemoteAddr()'  fetch the IP address when session switch happens.

'gs.getSession().getClientIP()'  avoid using this as it will give wrong IP if session switch happen


Forcing update business rules to run on records post BR creation 

var nst = new GlideRecord(TABLE);

 nst.query();

while (nst.next()){    

nst.setForceUpdate(true); //This forces the record to register an update even if no data changes

    nst.autoSysFields(false); //This prevents the sys_updated_on and sys_updated_by from being altered as you are not altering them but forcing a BR    

nst.setWorkflow(false); //This prevents workflow from running against the new BR or new ones being generated    

nst.update(); //Finishes the trigger.

}



Comments

Popular Posts