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

Can you help me out with the code for this 2nd point



2 .Inside the script glide query to cmdb_ci_vm table to find out with same name and IP address record already present in the table or not for which you are inserting/updating.


Please provide me the field name of "VMname" and "IP address" fields you want to consider.



Thanks,


Mihir


There's some change in the requirement now .


1. So I have a catalog item where there's a dropdown field VM -> 1,2,3 When user chooses 1 , then VM1,   if user chooses 2, then VM2   till 3 .


2. On this catalog I want a script that after clicking submit it should check if VM1 , VM2 ,VM3 names are duplicated and also should check from custom table where already there are records saved for vm name . If these none of the values are same then data should get saved in that same custom table.


3. Otherwise it should throw an error saying "please provide unique values for VM name "



Did you understood the requirement 553931 . Please guide how to write code for this.


deepakgarg do you know answer for this.


deepakgarg
ServiceNow Employee
ServiceNow Employee

Hi Aastha,



Can you please clarify a bit. What i understood is:


There is a catalog item, having 1 Select Choice variable, with choices, VM1, VM2 & VM3.


Now, when you submit this record, you want to check if there is an existing record with VM1 & user selected VM1 choice, then it should throw an error. Am i correct?



If not, then, with what am i supposed to check duplicate records?


Are there more than one Select Choice variables on form, to check for same value?


If you want to add only 1 record in a single table at a time, then you can also use record producer for the same.


capturedoubt.png



See I have this first Variable : No_of VMs . If there user select No.of VMs ->1( VM Name 1 )will be seen if No.of VMs->2 then (VM 2   Name 2) similarly VM Name 3 on selection of No.of VMs -> 3



I have created a custom table with dummy data for VM 1 , VM 2 , VM 3


find_real_file.png



Now as I have entered values -> vmtest1         vmtest2     vmtest3 (in first screenshot )


After end user fills value like the above three value and he clicks submit . Script should first see no.of.vms field if its 1 then only 1 VM value similarly for if 3 no of VMs is selected then three values should be compared with all the existing values in the custom table . If the values are repeated it should show an alert "Please enter unique values ") otherwise it should save values entered in VM name in that custom table .



Now its clear deepakgarg   ?