The CreatorCon Call for Content is officially open! Get started here.

Set record counter

tanz
Tera Expert

I have a customised table u_audit which inserts the INC record number whenever the INC is updated.For this I have created a customised string field(u_number) which captures INC number and I have created a customised field (u_count) on the audit table and I want to insert the counter(1,2,3,4,5...) on basis of insertion of INC record. For examples: when INC record is updated for 1st time a record should be inserted in u_audit table which populates u_number as INC number and u_count should be 1 and so on. Please can you let me know how to achieve this.

 

1 ACCEPTED SOLUTION

If my answers helped you to resolve your queries then please mark them as correct or helpful based on the impact.

Thanks and Remarks

Abhijit

By marking my response as correct or helpful, you contribute to helping future readers with similar issues.
Regards,
Abhijit
ServiceNow MVP

View solution in original post

30 REPLIES 30

The script is not working for me. I have written on OnBefore insert/update BR to insert the record. The record is getting inserted however the count is still showing as zero and increment is not happening. Please can you help me with the script

Can you please share what you have done?

 

Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande

 

Hi,

Try below code,

if(current.operation()=="insert"){

//Record insertion code here

}else if(current.operation()=="update"){

var grAudit=new GlideRecord("u_audit");

grAudit.addQuery("u_number",current.number);

grAudit.query();

if(grAudit.next()){

grAudit.u_count=grAudit.u_count+1;

grAudit.update();

}
}

As you have mentioned insertion is working so I am assuiming that you have record insertion code that you can paste in second line where I have commented the same.

Let me know if you have any further queries.

Please mark this as Correct or Helpful if it helps.

Thanks and Regards,
Abhijit

By marking my response as correct or helpful, you contribute to helping future readers with similar issues.
Regards,
Abhijit
ServiceNow MVP

I am not sure how this will be on update but I have tried the below script OnBefore insert/update on Incident table with condition state is OnHold which does not work. I want to just increase the counter.

var count=0;
var gr=new GlideRecord('u_audit');
gr.initialize();
gr.u_number=current.number;
gr.u_assignment_group=current.assignment_group.getDisplayValue();
gr.u_count=count++;
gr.insert();

Would like to display the record number for a particular INC number. Please can you help me with this.

Hi,

you need to update it like below:

var count=0;
if(current.operation()=="insert"){
var gr=new GlideRecord('u_audit');
gr.initialize();
gr.u_number=current.number;
gr.u_assignment_group=current.assignment_group.getDisplayValue();
gr.u_count=count++;
gr.insert();

}else{

var gr=new GlideRecord('u_audit');
gr.addQuery('u_number',current.number);
gr.query();
if(gr.next()){
count = parseInt(gr.u_count,10);
gr.u_count=count++;
gr.update();
}


}

 

Thanks,

Anil Lande

Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande