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

Hi,

As I said earlier you just need to add record creation code in my above code, try below updated code.

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

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

}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=parseInt(grAudit.u_count)+1;
grAudit.update();
}
}

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 think I am not unable to understand this. I do not require to update the same again and again incident with the counter. I want to create a new record when the count increases. Here is a sample of how my table will look.

Number Assignment group Count

INC1      ABC                        1

INC1       ABC                       2

INC2     XYZ                        1

INC2      XYZ                       2

 

So a new record will be created whole together and the count is updated on that. I am able to insert the update but unable to set the count

I got your exact requirement now. Try below,

var grAudit=new GlideRecord("u_audit");
grAudit.orderByDesc("sys_created_on");
grAudit.addQuery("u_number",current.number);
grAudit.query();
if(grAudit.next()){
var count=parseInt(grAudit.u_count)+1;
var grAudit=new GlideRecord('u_audit');
grAudit.initialize();
grAudit.u_number=current.number;
grAudit.u_assignment_group=current.assignment_group.getDisplayValue();
grAudit.u_count=count;
grAudit.insert();
}
else{
var grAudit=new GlideRecord('u_audit');
grAudit.initialize();
grAudit.u_number=current.number;
grAudit.u_assignment_group=current.assignment_group.getDisplayValue();
grAudit.u_count=0;
grAudit.insert();
}

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

The counter is showing as NaN

Try replacing below line,

var count=parseInt(grAudit.u_count)+1;
var count=parseInt(grAudit.getValue("u_count"))+1;

Only problem is with this line basically we need to convert it to int then assign.

Try debugging further.

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