Custom Audit table

shams
Kilo Contributor

Hi all!

 

Basically we have unaudited cmdb_ci extended tables . I would like to have a report on all changed fields (status, etc) in CI which have specific support group assigned.

 

I have created a table u_ci_audit, which i will use to create report based on. it contains pretty same fields as history table (field name, old value, new value, CI name)

 

Created business rule to trigger   when cmdb_ci record updated, inserted, deleted and it creates new record in audit table.

 

What is tricky is to create a script to create separate record in u_ci_audit table   for each changed field in CI.

I don't want to track all fields with function .changes() separately.

 

Any ideas will be appreciated

1 ACCEPTED SOLUTION

Bhavesh Jain1
Giga Guru

You can get all the fields of the form and apply changes() in a loop.


Below code is just an example of how you can refer to name/value of each field of a form.


var fields = current.getFields();


for (var i = 0; i < fields.size(); i++) {


  var field = fields.get(i);


  var name = field.getName();


  var value = field.getDisplayValue();


  gs.print(i + ". " + name + "=" + value);


}


View solution in original post

8 REPLIES 8

Bhavesh Jain1
Giga Guru

You can get all the fields of the form and apply changes() in a loop.


Below code is just an example of how you can refer to name/value of each field of a form.


var fields = current.getFields();


for (var i = 0; i < fields.size(); i++) {


  var field = fields.get(i);


  var name = field.getName();


  var value = field.getDisplayValue();


  gs.print(i + ". " + name + "=" + value);


}


Hello!



It perfectly worked for me, so here is the result code



var fields = current.getFields();


for (var i = 0; i < fields.size(); i++) {



var field = fields.get(i);


  var name = field.getName();


  var dispname = field.getLabel();


  var value = field.getDisplayValue();



if (current[name].changes()){


var ciaudit = new GlideRecord('u_ci_audit');


ciaudit.initialize();


ciaudit.u_ci = current.sys_id; //reference to cmdb_ci


ciaudit.u_changed_field=dispname;


ciaudit.u_new_value=value;


ciaudit.u_old_value=previous[name];


ciaudit.insert();


}


}



thank you!


I believe there is a simpler way to determine the fields that changed.   This comes out of the metric events business rule on task, it appears to give a list of what fields changed.



var gru =   new Packages.com.glide.script.GlideRecordUtil.get(current)


var fieldsChanged = gru.getChangedFieldNames();


@Bhavesh Jain1 I have gone through your solution It worked but I want to do it in a different way as follows can you guide me accordingly?

 

I want to do Custom Audit based on the requirements with 2 interfaces.

The idea is to have an interface where they can configure certain fields of the mentioned entities
which they want audited. Whenever any new records get created/existing are modified, the
system should check if the fields listed under the configuration interface have changed. If so,
then the details should be recorded, basic attributes being:
- Entity
- Record Identifier
- Field Changed
- Previous Value
- New Value
- Modified by
- Modified on