how to lock a record on query

henry_diaxion
Kilo Contributor

Hi,

I ma facing a challenge at the moment and would like some guidance.

scenario

i am querying   the server table, the query returns the last server name, then i am increasing the value plus one then passing   it to as a field value.

all good here.

if a second user submits a request he/she will get the same value on query then the request will fail.

My script include is doing the following:

I am passing a   variable i.e; 'apps'

var essUniqueserverName = Class.create();

essUniqueserverName.prototype = Object.extendsObject(AbstractAjaxProcessor, {

test: function(){

                                           

                                              var pref=this.getParameter('sysparm_type_env');

                                                                      gs.log(pref);

                                           

                                              //return pref;

                                           

                      var indexingRecord = new GlideRecord('tableName');

                                           

indexingRecord.addQuery('name','STARTSWITH', pref);

indexingRecord.orderByDesc('name');

//indexingRecord.chooseWindow(1,1);

indexingRecord.query();

var rCount=indexingRecord.getRowCount();

var lastValue = '';

// if count = 0 the   return value+ 0001

if (rCount == 0){

                   

                      return   pref + '0001'; //

}

                                           

                   

else if (indexingRecord.next()){

                   

lastValue=   indexingRecord.name;

                                           

//gs.info(indexingRecord.name);

                   

                   

                      //return indexingRecord.name;

}

                                              var numb= lastValue.substring(4,8); //get the last 4 digits

                                              var numt=Number(numb)+1; // increment by 1

                                              if (numt<10 ){

                                                                   

                                                                      return pref+'000'+numt;

                                              } else if(numt<100){

                                                                      return pref+'00'+numt;

                                              } else if (numt<1000) {

                                                                   

                                                                      return pref+'0'+numt;

                                              } else   {

                                                                   

                                                                      return pref+numt;// return value

                                              }

                   

                   

                                           

                   

},

                   

      type: 'essUniqueserverName'

});

Now the challenge is, if a second user submits a request this script run and get the same value. so the first request will success and the second will fail.

Any ideas on how i can prevent this?

The models is this

  • query table for the last server name
  • get servername increment by one
  • pass the server name to external app.
  • server get build in vcenter then pulled into the server table via api
1 ACCEPTED SOLUTION

Ashutosh Munot1
Kilo Patron
Kilo Patron

HI Henry,



This will always pull last server name right.



So this will always fetch the same name whenever you will query it.



You want same name always and then add count to it and then send to Venter?



Thanks


View solution in original post

10 REPLIES 10

Can you please try like,



var essUniqueserverName = Class.create();


essUniqueserverName.prototype = Object.extendsObject(AbstractAjaxProcessor, {


test: function(){


var pref = this.getParameter('sysparm_type_env');


var indexingRecord = new GlideRecord('tableName');


indexingRecord.addQuery('name','STARTSWITH', pref);


indexingRecord.orderByDesc('sys_created_on');


indexingRecord.setLimit(1);


indexingRecord.query();


var rCount = indexingRecord.getRowCount();


if (rCount == 0){


return   pref + '0001';


}


else if (indexingRecord.next()){


var lastValue =   indexingRecord.name;


var numb = lastValue.substring(4,8); //get the last 4 digits


var numt = Number(numb) + 1; // increment by 1


if (numt < 10 )


return pref + '000' + numt;


else if(numt < 100)


return pref + '00' + numt;


else if (numt < 1000)


return pref + '0' + numt;


else


return pref + numt;// return value


}


},


type: 'essUniqueserverName'


});