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

I tried putting values in VMname 1   -> vm1   and VMname2 -> vm2 and dummy values entered in the custom table is


find_real_file.png


Still its showing False even when I am inserting unique values in catalog form



Check this


find_real_file.png


deepakgarg
ServiceNow Employee
ServiceNow Employee

Got it. We are not taking care of empty values here. Try putting on all the 3 values on catalog item and it will submit.


Let me update the scripts for the same.


Yes working when I enter 3 values for 2 unique values its giving alert "true" and then moves to request ticket


While giving repeated values its giving an alert "false" and then not moving towards request ticket page


Change your client script to:


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


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


  return false;


}



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


  return true;


  },



  type: 'CheckVMDuplicate'


});



Try this. It will validate correctly, if i am not missing anything. Its just a dummy code. You can check if you can optimise it a bit for your use case.


Its working with the above code now the only task is remaining is that how to save those values in the table .