save values from field to table

aastha3
Giga Contributor

Hi ,

I am looking for a code to save 2 values in the Virtual Machine CI form (cmdb_ci_vm table). Form will have fields VMname1 , IPAddress1 Similarly VMname2 , IpAddress2 ,VMname3 IpAddress3 and have to save the data filled in these fields in a table similarly with same field names.

If any of these user entered data repeats then it should throw an error .

VMname or IPAddress should not be duplicated.

Do guide me what to do.

1 ACCEPTED SOLUTION

deepakgarg
ServiceNow Employee
ServiceNow Employee

Now, update your script include to:



var CheckVMDuplicate = Class.create();


CheckVMDuplicate.prototype = Object.extendsObject(AbstractAjaxProcessor, {



  checkDuplicate : function() {


  var s1 = this.getParameter('sysparm_s1');


  var s2 = this.getParameter('sysparm_s2');


  var s3 = this.getParameter('sysparm_s3');



  var gr = new GlideRecord('u_vm_info');//your table name u_vm_info


  var gc = gr.addQuery('u_vm1', s1);//Make sure your field names are VM1, VM2, VM3. not titles.


  gc.addOrCondition('u_vm2', s1);


  gc.addOrCondition('u_vm3', s1);


if(s2 != '') {


  gc.addOrCondition('u_vm1', s2);


  gc.addOrCondition('u_vm2', s2);


  gc.addOrCondition('u_vm3', s2);


}


if(s3 != '') {


  gc.addOrCondition('u_vm1', s3);


  gc.addOrCondition('u_vm2', s3);


  gc.addOrCondition('u_vm3', s3);


}


  gr.query();


  if(gr.next())


  return false;


gr.initialize();
gr.u_vm1 = s1;
gr.u_vm2 = s2;
gr.u_vm3 = s3;
gr.insert();

  return true;


  },



  type: 'CheckVMDuplicate'


});



Add these lines, it will insert the values in the table also.


View solution in original post

41 REPLIES 41

deepakgarg I have been told to put alert as ("Please enter unique values in VM1") same way   ("Please enter unique values in VM2") similarly for VM3


so I tried this at the end but only working for VM1 not for other two.


if(ga.getAnswer(s1) == 'false')


  {


        alert('Please insert unique values in VM1');


      return false;


  }


  else if(ga.getAnswer(s2) == 'false')


  {


  alert('Please insert unique values in VM2');


  return false;


  }


  else if(ga.getAnswer(s3) == 'false')


  {


            alert('Please insert unique values in VM3');


  return false;


  }




Is this wrong ? If you can help deepakgarg   . Refer the code which I marked correct


deepakgarg
ServiceNow Employee
ServiceNow Employee

Hi Astha,



For that, you might need to query thrice, once for each variable, in Script include and return the results appropriately for the same.


var gc = gr.addQuery('u_vm1', s1);//Make sure your field names are VM1, VM2, VM3. not titles.


  gc.addOrCondition('u_vm2', s1);


  gc.addOrCondition('u_vm3', s1);


if(s2 != '') {


  gc.addOrCondition('u_vm1', s2);


  gc.addOrCondition('u_vm2', s2);


  gc.addOrCondition('u_vm3', s2);


}


if(s3 != '') {


  gc.addOrCondition('u_vm1', s3);


  gc.addOrCondition('u_vm2', s3);


  gc.addOrCondition('u_vm3', s3);


}


  gr.query();


  if(gr.next())


  return false;



above same code has to be written thrice with changes in var gc = gr.addQuery('u_vm1', s2);   var gc = gr.addQuery('u_vm1', s3); right ??


and how to make changes in Client script deepakgarg


deepakgarg
ServiceNow Employee
ServiceNow Employee

Yeah, try something like:



var f1 = true, f2= true, f3 = true;


var gr = new GlideRecord('u_vm_info');//your table name u_vm_info


  var gc = gr.addQuery('u_vm1', s1);//Make sure your field names are VM1, VM2, VM3. not titles.


  gc.addOrCondition('u_vm2', s1);


  gc.addOrCondition('u_vm3', s1);


gr.query();


  if(gr.next())


f1 = false;



Similarly, do for s2 & s3:


var gr = new GlideRecord('u_vm_info');//your table name u_vm_info


  var gc = gr.addQuery('u_vm1', s2);//Make sure your field names are VM1, VM2, VM3. not titles.


  gc.addOrCondition('u_vm2', s2);


  gc.addOrCondition('u_vm3', s2);


gr.query();


  if(gr.next())


f2 = false;



var gr = new GlideRecord('u_vm_info');//your table name u_vm_info


  var gc = gr.addQuery('u_vm1', s3);//Make sure your field names are VM1, VM2, VM3. not titles.


  gc.addOrCondition('u_vm2', s3);


  gc.addOrCondition('u_vm3', s3);


gr.query();


  if(gr.next())


f3 = false;



Now, insert only if all of f1, f2, f3 are true;


else return all 3 results, and check in client script. If f1 is false, then, s1 is duplicate and so on.


Now I dont have to insert from onsubmit they have asked me to remove the insert part now only duplication part needs to be check and 3 alert should come up for vm1 vm2 vm3