in reply to Re^3: Need help with node names for creating header line
in thread Need help with node names for creating header line

OK, took out extraneous lines. Still getting following error: perl CMDB_Sample.pl Can't call method "text" on an undefined value at CMDB_Sample.pl line 46.
#!/usr/bin/perl use strict; use warnings; use 5.014; use MIME::Base64; use Data::Dumper; use XML::Twig; use REST::Client; # Just in case we want to request JSON instead of XML from CMDB #use JSON; Just in case we want to request JSON instead of XML from CM +DB # Set the request parameters my $host = 'https://staplessb.service-now.com'; # Eg. User name="admin", Password="admin" for this code sample. my $user = 'USERNAME'; my $pwd = 'PASSWORD'; my $client = REST::Client->new(host => $host); my $encoded_auth = encode_base64("$user:$pwd", ''); my $header = 'checked_in,change_request,po_number,correlation_id,cfg_a +uto_provider,supported_by,u_service_catalogtrue,first_discovered,u_oe +m_part_numbe,sent_for_repair,owned_by,gl_account,managed_by,asset,u_p +roject_id,maintenance_schedule,u_chgfalse,category,delivery_date,inst +all_status,u_maintenance_window,virtualfalse,u_sub_class,dns_domain,u +_incident,u_parts_software_needed,change_control,checked_out,u_decomm +ission_stamp,purchase_date,order_date,u_template,skip_sync,lease_id,u +_sevice_options,vendor,sys_id,u_maint_contract_sla,sys_created_by,u_s +la_support_tie,subcategory,start_date,comments,location,link,value,un +verified,justification,u_disposal_date,sys_tags,sys_domain,link,value +,sys_mod_count,cost_cc,u_total_purchase_cost,monitor,sys_updated_on,w +arranty_expiration,invoice_number,fqdn,cost,u_retail_grp,u_server_typ +e,ip_address,u_manufacture_date_dummy,last_discovered,model_id,link,v +alue,manufacturer,u_incfalse,company,due,cfg_auto_management_server,u +_proposed,u_change,u_work_notes,u_store_location,asset_tag,discovery_ +source,u_application_id,assignment_group,can_print,u_problem,departme +nt,u_manufacture_date,support_group,link,value,u_exclusion,sys_create +d_on,u_display_name,u_alias,cost_center,short_description,sys_updated +_by,name,u_environment,u_function,due_in,u_change_approver,install_da +te,assigned,u_active_stamp,u_retail_group,serial_number,u_work_notes_ +_asset_,repair_contract_id,assigned_to,mac_address,model_number,sched +ule,u_ci_id,returned_from_repair,ng_assignment_flag,u_procurement_pro +duct_number,sys_class_name,attributes,u_location_stamp,fault_count,cf +g_auto_change'; $client->GET("/api/now/table/cmdb_ci?sysparm_limit=5&sysparm_fields=($ +header)", {'Authorization' => "Basic $encoded_auth", 'Accept' => 'application/xml'}); my $field= $ARGV[0] || 'u_ci_id'; my $input_xml = $client->responseContent(); my $twig = new XML::Twig; $twig->xparse($input_xml); my $root = $twig->root(); my @data = (); my @cols = split ',',$header; unshift @cols, $field; # added for sort for my $record ($root->children) { my @tmp=(); for my $col (@cols){ push @tmp,$record->first_child($col)->text; } push @data,[ @tmp ]; } my @sorted = sort { $b->[0] cmp $a->[0] } @data; print $header."\n"; for (@sorted){ print join ',',@$_[1..$#cols]; # exclude sort col print "\n"; } __DATA__ <?xml version="1.0" encoding="UTF-8"?> <response> <result> <checked_in></checked_in> <change_request></change_request> <po_number></po_number> <correlation_id></correlation_id> <cfg_auto_provider></cfg_auto_provider> <supported_by></supported_by> <u_service_catalog>true</u_service_catalog> <first_discovered></first_discovered> <u_oem_part_numbe></u_oem_part_numbe> <sent_for_repair></sent_for_repair> <owned_by></owned_by> <gl_account></gl_account> <managed_by></managed_by> <asset></asset> <u_project_id></u_project_id> <maintenance_schedule></maintenance_schedule> <u_chg>false</u_chg> <category></category> <delivery_date></delivery_date> <install_status></install_status> <u_maintenance_window></u_maintenance_window> <virtual>false</virtual> <u_sub_class></u_sub_class> <dns_domain></dns_domain> <u_incident></u_incident> <u_parts_software_needed></u_parts_software_needed> <change_control></change_control> <checked_out></checked_out> <u_decommission_stamp></u_decommission_stamp> <purchase_date></purchase_date> <order_date></order_date> <u_template></u_template> <skip_sync></skip_sync> <lease_id></lease_id> <u_sevice_options></u_sevice_options> <vendor></vendor> <sys_id></sys_id> <u_maint_contract_sla></u_maint_contract_sla> <sys_created_by></sys_created_by> <u_sla_support_tie></u_sla_support_tie> <subcategory></subcategory> <start_date></start_date> <comments></comments> <location> <link></link> <value></value> </location> <unverified></unverified> <justification></justification> <u_disposal_date></u_disposal_date> <sys_tags></sys_tags> <sys_domain> <link></link> <value></value> </sys_domain> <sys_mod_count></sys_mod_count> <cost_cc></cost_cc> <u_total_purchase_cost></u_total_purchase_cost> <monitor></monitor> <sys_updated_on></sys_updated_on> <warranty_expiration></warranty_expiration> <invoice_number></invoice_number> <fqdn></fqdn> <cost></cost> <u_retail_grp></u_retail_grp> <u_server_type></u_server_type> <ip_address></ip_address> <u_manufacture_date_dummy></u_manufacture_date_dummy> <last_discovered></last_discovered> <model_id> <link></link> <value></value> </model_id> <manufacturer></manufacturer> <u_inc>false</u_inc> <company></company> <due></due> <cfg_auto_management_server></cfg_auto_management_server> <u_proposed></u_proposed> <u_change></u_change> <u_work_notes></u_work_notes> <u_store_location></u_store_location> <asset_tag></asset_tag> <discovery_source></discovery_source> <u_application_id></u_application_id> <assignment_group></assignment_group> <can_print></can_print> <u_problem></u_problem> <department></department> <u_manufacture_date></u_manufacture_date> <support_group> <link></link> <value></value> </support_group> <u_exclusion></u_exclusion> <sys_created_on></sys_created_on> <u_display_name></u_display_name> <u_alias></u_alias> <cost_center></cost_center> <short_description></short_description> <sys_updated_by></sys_updated_by> <name></name> <u_environment></u_environment> <u_function></u_function> <due_in></due_in> <u_change_approver></u_change_approver> <install_date></install_date> <assigned></assigned> <u_active_stamp></u_active_stamp> <u_retail_group></u_retail_group> <serial_number></serial_number> <u_work_notes__asset_></u_work_notes__asset_> <repair_contract_id></repair_contract_id> <assigned_to></assigned_to> <mac_address></mac_address> <model_number></model_number> <schedule></schedule> <u_ci_id></u_ci_id> <returned_from_repair></returned_from_repair> <ng_assignment_flag></ng_assignment_flag> <u_procurement_product_number></u_procurement_product_number> <sys_class_name></sys_class_name> <attributes></attributes> <u_location_stamp></u_location_stamp> <fault_count></fault_count> <cfg_auto_change></cfg_auto_change> </result> </response>';

Replies are listed 'Best First'.
Re^5: Need help with node names for creating header line
by poj (Abbot) on Dec 30, 2015 at 20:16 UTC

    Are these in the header string correct ?

    u_service_catalogtrue u_chgfalse u_incfalse virtualfalse
    poj

      No, just forgot to strip that. The problem is that some of the fields populat and others don't from the CMDB download, and this is just starting code to dump all the records, and automate their population later. So I just need a CSV file with a proper header.