create record from client script in workflow

daniellethomson
Tera Expert

Scripting isn't my strong suit and I'm sure it's an easy fix but I can't get it. I need to crate a record on the cmdb_ci_server table from within a workflow. I can get the record to create but it's not inputting the values from the variables. Instead it is just inputting what is between the ' ' or for the value of sys_class_name is is just inputting 'server' instead of ESX Server.

Any help would be great! Thanks!

var server = new GlideRecord("cmdb_ci_server");

server.initialize();

server.setValue('ip_address','current.variables.ip_address');

server.setValue('name', 'current.variables.server_name');

server.setValue('sys_class_name', 'current.variables.class');

server.insert();

1 ACCEPTED SOLUTION

Hi Danielle,



I'm Glad that it helped you. If i have answered your question, would you mind marking the Answer Correct and close the thread?



In this case, you can use the below script:


if(current.variables.class == 'ESX Server') {


var server = new GlideRecord("cmdb_ci_esx_server");


server.initialize();


server.ip_address = current.variables.ip_address;


server.name = current.variables.server_name;


server.sys_class_name = current.variables.class;


server.insert();


}


else if(current.variables.class == 'Windows'){


//Do your logic


//You can follow the first If() loop


}


else if(current.variables.class == 'Linux'){


//Do your logic


//You can follow the first If() loop


}



I hope this helps. Please mark correct/helpful based on impact


View solution in original post

13 REPLIES 13

Aka Guglielmo
ServiceNow Employee
ServiceNow Employee

try this



var server = new GlideRecord("cmdb_ci_server");


server.initialize();


server.setValue('ip_address',current.variables.ip_address);


server.setValue('name', current.variables.server_name);


server.setValue('sys_class_name', current.variables.class);


server.insert();


amlanpal
Kilo Sage

Hi Danielle,



It seems that you know the logic, just mistakenly used wrong syntax. All you need to do is to try the script provided by wly and I believe this will work.



Let me tell you why, when we do push any static value we do use ''. Now as those values of the variables are not static, rather coming from the currently raised Requested Item, you only need to call current.variables.variable_name. In all, this will ended up as the same below code which William has provided earlier:



var server = new GlideRecord("cmdb_ci_server");


server.initialize();


server.setValue('ip_address',current.variables.ip_address);


server.setValue('name', current.variables.server_name);


server.setValue('sys_class_name', current.variables.class);


server.insert();



I hope this helps. Please mark correct/helpful based on impact


Thank you for the explanation but now I'm receiving an error "missing name after . operator" line 5


Hi Danielle,



Could you please provide the snapshot of the issue and your full code for better understanding?


Also I don't think that OOB any field exists in 'cmdb_ci_server' table named 'sys_class_name'