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
ServiceNow Employee
ServiceNow Employee

If you dont need to insert, remove this part of script include:


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

I have done that meanwhile can you help me I have to insert vmname1 , vmname2 , vmname3 into that same table but when catalog: server provisioning RITM state : closed complete then only values should get save in the table and even if any data is updated in RITM the vmnames should not get stored again in the table .


I am using business rule


When: After   Type : Insert , Update


conditions : Item is server provisioning and


                                State is closed complete.




var a = new GlideRecord('u_vm_info');


                      a.addQuery('u_vm1 ','current.variables.vm_name');


                      a.addQuery('u_vm2 ','current.variables.vm_name2');


                      a.addQuery('u_vm3 ','current.variables.vm_name3');


                      //a.addQuery('u_flag',true);


                      //a.query();



                     


if (a.next())


                      {


                        }


else


{                      


//var gr = new GlideRecord('u_vm_info');


                                              a.initialize();


                                              a.u_vm1 = current.variables.vm_name;


                                              a.u_vm2 = current.variables.vm_name2;


                                              a.u_vm3 = current.variables.vm_name3;


                                              a.u_flag=true;


                                              a.insert();


}




But having issues once the values are duplicating and other time new values are not getting stored ..Can you check and guide me deepakgarg


I tried the code which you provided please check once:



Script Include:


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 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);  


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())


  f1 = false;


   


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


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


  gd.addOrCondition('u_vm2', s2);


  gd.addOrCondition('u_vm3', s2);


  if(s2 != '') {


  gd.addOrCondition('u_vm1', s2);


  gd.addOrCondition('u_vm2', s2);


  gd.addOrCondition('u_vm3', s2);


}


if(s3 != '') {


  gd.addOrCondition('u_vm1', s3);


  gd.addOrCondition('u_vm2', s3);


  gd.addOrCondition('u_vm3', s3);


}  


  gp.query();


  if(gp.next())


f2 = false;


   


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


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


  ge.addOrCondition('u_vm2', s3);


  ge.addOrCondition('u_vm3', s3);


  if(s2 != '') {


  ge.addOrCondition('u_vm1', s2);


  ge.addOrCondition('u_vm2', s2);


  ge.addOrCondition('u_vm3', s2);


}


if(s3 != '') {


  ge.addOrCondition('u_vm1', s3);


  ge.addOrCondition('u_vm2', s3);


  ge.addOrCondition('u_vm3', s3);


}  


  gq.query();


  if(gq.next())


  f3 = false;




  //return false;


  /*gr.initialize();


  gr.u_vm1 = s1;


  gr.u_vm2 = s2;


  gr.u_vm3 = s3;


    gr.insert();


*/




// return true;


  },



  type: 'CheckVMDuplicate'


});




Client script ( in this please tell me how to check in client script. If f1 is false, then, s1 is duplicate and so on for   s2 , s3 )



  function onSubmit() {


  //Type appropriate comment here, and begin script below


  var num = g_form.getValue('number_of_vms');


  var s1 = g_form.getValue('vm_name');


  var s2 = g_form.getValue('vm_name2');


  var s3 = g_form.getValue('vm_name3');


   


  var ga = new GlideAjax('CheckVMDuplicate');


  ga.addParam('sysparm_name', 'checkDuplicate');


   


  if(num == 1 && s1 == '') {


  alert('value is empty / duplicate values');


  return false;


    }


  else if(num == 2 && (s1 == '' || s2 == '' || s1 == s2)) {


  alert('value is empty / duplicate values');


  return false;


  }


  else if(num == 3 && (s1 == '' || s2 == '' || s3 == '' || s1 == s2 || s2 == s3 || s1 == s3)) {


  alert('value is empty / duplicate values');


  return false;


  }


    ga.addParam('sysparm_s1', s1);


  ga.addParam('sysparm_s2', s2);


  ga.addParam('sysparm_s3', s3);


  ga.getXMLWait();  



  //alert(ga.getAnswer());//Remove this line


  if(ga.getAnswer("f1") == 'false')                                                         // is this syntax correct ???


  {  


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


      return false;


  }


  else if(ga.getAnswer("f2") == 'false')


  {


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


      return false;


  }


  else if(ga.getAnswer("f3") == 'false')


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


      return false;  


   


}




  }


deepakgarg
ServiceNow Employee
ServiceNow Employee

It should be something like:



Script Include:


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 f1 = true, f2= true, f3 = true;


//queries and check if s1 is duplicate


  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;



//queries and check if s2 is duplicate


if(s2 != '') {


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


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


  gd.addOrCondition('u_vm2', s2);


  gd.addOrCondition('u_vm3', s2);


  gp.query();


  if(gp.next())


f2 = false;


}



//queries and check if s3 is duplicate


if(s3 != '') {


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


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


  ge.addOrCondition('u_vm2', s3);


  ge.addOrCondition('u_vm3', s3);


  gq.query();


  if(gq.next())


  f3 = false;


}


var result = this.newItem('result');


result.setAttribute('f1', f1);


result.setAttribute('f2', f2);


result.setAttribute('f3', f3);


return result;


  },



  type: 'CheckVMDuplicate'


});


Now, you can use the result in client script, to check which is duplicate and which is not.


Whatever was working before has stopped working after the I added f1 , f2 ,f3 in script include . Please once validate the code and provide both client script code and script include code.



deepakgarg please help me on this .