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

Hi Ashutosh,



no i dont get an error, the query returns the value and passes it. i get an error on the workflow because the server name will exist.


it is tricky case.


i would like to know if is best to create a temp table to store the returned value then increment it there,


?!


the script include is called by a catalog script. to set the server name.


Hi Henry,



Now i understand,



Suppose you are submitting two request then due to duplication you are getting error in workflow.



Now one last question: What will happen at VM side if we pass same server name twice thrice. Will it create new server for them?



Also, What i suggest you is when you get response from VM check the server name if present then increment it by 1 and save it.



Thanks,


Hi,


the server name is created By Jenkins,


I passe the parameters to jenkins form the request form i.e server name. then jenkins is the one that queries vcenter and confirms that the server does not exist.If he server exist the job fails.



Reg


Henry


so   the hops are:


servicenow >jenkins>vcenter> servicenow


servicenow passes the parameters to jenkins>jenkins runs a job >creates vm in vcenter>>>> servicenow pull the server name via discovery