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

custom table name : u_vm_info


field names in custom table(VM means vmname): VM1


                                                                                                                                            VM2


                                                                                                                                            VM3



field names in catalog form : no_of_vms


                                                                                  vm_name1


                                                                                  vm_name2


                                                                                  vm_name3


deepakgarg
ServiceNow Employee
ServiceNow Employee

Create an onSubmit Catalog Client script on catalog item as:


Screen Shot 2016-10-14 at 2.08.59 PM.png



function onSubmit() {


  //Type appropriate comment here, and begin script below


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


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


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


  if(s1 == s2 || s2 == s3 || s1 == s3) {


  alert('duplicate values');


  return false;


  }


  var ga = new GlideAjax('CheckVMDuplicate');


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


  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;


}




Now, create a Script include as:


Screen Shot 2016-10-14 at 2.10.55 PM.png


Client Callable: true


Script:


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('VM1', s1);//Make sure your field names are VM1, VM2, VM3. not titles.


  gc.addOrCondition('VM2', s1);


  gc.addOrCondition('VM3', s1);


  gc.addOrCondition('VM1', s2);


  gc.addOrCondition('VM2', s2);


  gc.addOrCondition('VM3', s2);


  gc.addOrCondition('VM1', s3);


  gc.addOrCondition('VM2', s3);


  gc.addOrCondition('VM3', s3);


  gr.query();


  if(gr.next())


  return false;


  return true;


  },



  type: 'CheckVMDuplicate'


});



Try this and let me know.


Its not working whatever value I gave its showing alert false and values are not getting saved in the table.


I checked the table values those were


u_vm1 ,u_vm2 , u_vm3



I made some changes in field name


client script:


    function onSubmit() {  


  //Type appropriate comment here, and begin script below  


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


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


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


  if(s1 == s2 || s2 == s3 || s1 == s3) {  


  alert('duplicate values');  


  return false;  


  }  


  var ga = new GlideAjax('CheckVMDuplicate');  


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


  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;  


}  



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


  gc.addOrCondition('u_vm1', s2);  


  gc.addOrCondition('u_vm2', s2);  


  gc.addOrCondition('u_vm3', s2);  


  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'  


});  


deepakgarg
ServiceNow Employee
ServiceNow Employee

It wont save the values in the table. It will only validate values before submit. If the values are fine, it will submit the form. To insert those values in table, you need to write a Business rule on the request table, that will insert the values into the table.


First check, if the validation is happening correctly.


deepakgarg
ServiceNow Employee
ServiceNow Employee

Can you please check first, that validation of values is working fine? If a value is already in the table 'u_vm_info', then only it should return false. Else it should return true;


If it doesn't work, can you please tell what values you filled and what alert it is giving. It's hard to debug without knowing the output and correct fields and names.