How to copy string field value with comma separated to different fields?

HARI KISHAN GVS
Mega Sage

Hi Team,

i have a string field called 'address_attribute' which is populated from azure Integration with value like (street address, State, City, zip, Office Address1, Office Address2)

i need to set the values of fields (Street address, state,city,zip, office address, office address 1) from the address_attribute field.

HARIKISHANGVS_0-1682433907415.png

in above screenshot, i want to set the value of fields state and office address to their respecctive value.

Thank you,

Hari Kishan.

1 ACCEPTED SOLUTION

Upsilon
Giga Guru

Hi, 

You can do it by split 

var array = current.address_attribute.split(",");// If server script

var array = g_form.getValue ("address_attribute").split(",");// If client script



var street =array[0];

var zip =array[3];....



//Then you have just to update your field by current.zip// or g_form.setValue it depends if your are on client or server side

I hope it's helpful

Regards,

View solution in original post

2 REPLIES 2

Upsilon
Giga Guru

Hi, 

You can do it by split 

var array = current.address_attribute.split(",");// If server script

var array = g_form.getValue ("address_attribute").split(",");// If client script



var street =array[0];

var zip =array[3];....



//Then you have just to update your field by current.zip// or g_form.setValue it depends if your are on client or server side

I hope it's helpful

Regards,

DUGGI
Giga Guru

@HARI KISHAN GVS 

 

To set the values of fields (Street address, state, city, zip, office address, office address 1) from the 'address_attribute' field, you can create a Business Rule that triggers on insert or update.

Here's how you can do that:

  1. Create a new Business Rule:

a. In your ServiceNow instance, navigate to System Definition > Business Rules.

b. Click on "New" to create a new Business Rule.

d. Set the "When to run" options as follows:

  • Check the "When" checkboxes for "before" and "insert" as well as "update."
  • Check the "Order" checkbox and set the value to "100."
  1. In the "Advanced" tab, paste the following script:
(function executeRule(current, previous /*null when async*/) {
    var addressAttribute = current.address_attribute.toString();

    // Split the address_attribute string into an array
    var addressComponents = addressAttribute.split(',');

    // Assuming the address components are ordered as: Street, State, City, Zip, Office Address1, Office Address2
    current.street_address = addressComponents[0].trim();
    current.state = addressComponents[1].trim();
    current.city = addressComponents[2].trim();
    current.zip = addressComponents[3].trim();
    current.office_address = addressComponents[4].trim();
    current.office_address_1 = addressComponents[5].trim();
})(current, previous);

  1. Click on "Submit" to create the Business Rule.

This Business Rule will parse the 'address_attribute' field and set the values of the Street address, state, city, zip, office address, and office address 1 fields accordingly.

 

Please note that you might need to adjust the script according to the actual field names in your instance if they differ from the standard field names. Also, ensure the address components in the 'address_attribute' field are ordered as shown in the script, or modify the script to accommodate the order in which they appear.