Welcome to Community Week 2025! Join us to learn, connect, and be recognized as we celebrate the spirit of Community and the power of AI. Get the details  

How to pass description value to objects and then push to array

Charan123
Tera Expert

Hi Team,

convert description value into objects and then push to array.

Charan123_0-1724080929643.png

In the format of [ {Incident : INC0010014, Affected User : Abraham Lincoln, Priority : 2 - High, Assignment Group : Application Development},{ Incident : INC0010020, Affected User : David Miller, Priority : 1 - Critical, Assignment Group : Service Desk}, {....}]

Please share your inputs how to achieve this.


Thanks
Charan

6 REPLIES 6

Najmuddin Mohd
Mega Sage

Hi @Charan123 ,
Below is the code.

unction getMultipleIncidentDetails() {
    var incidentDetailsArray = []; 


        
            var details1 = {
                'Incident' : 'INC001000',
				'Affected User' : 'Abraham Lincoln',
				'Priority' : '1 - Critical',
				'Assignment Group' : 'Application Developement',
            };

			var details2 = {

				'Incident' : 'INC001002',
				'Affected User' : 'Test 2',
				'Priority' : '3 - Moderate',
				'Assignment Group' : 'Service Desk',

			};
            incidentDetailsArray.push(details1); 
			incidentDetailsArray.push(details2);
    
    return incidentDetailsArray; 
}

// Example usage of the function

var ans = getMultipleIncidentDetails(); 
gs.info(JSON.stringify(ans));
OUTPUT:

*** Script: [{"Incident":"INC001000","Affected User":"Abraham Lincoln","Priority":"1 - Critical","Assignment Group":"Application Developement"},{"Incident":"INC001002","Affected User":"Test 2","Priority":"3 - Moderate","Assignment Group":"Service Desk"}]

Run it in the scripts - background to validate the output.

If this information helps, kindly mark this post as Helpful and Accept the soultion.

Regards,
Najmuddin.

Hi @Najmuddin Mohd ,
Thanks for the reply, I didn't want manually to enter those values( incident,affected user..) and values will be differ for description field in every form.
In the script 
var descriptionValue = g_form.getValue('description');
here descriptionValue --> need to convert objects and then push to array

Hi @Charan123 ,

 

 

var text = "Incident : INC001000 Affected User : Abraham Lincoln Priority : 2 - High Assignment Group : Application Developement Incident : INC001002 Affected User : Test Priority : 3 - Moderate Assignment Group : Service Desk "; // string variable




// Split the text at each "Incident" keyword
var incidentsArray = text.trim().split(/(?=Incident)/);

//gs.info(typeof(incidentsArray))

var ans = JSON.stringify(incidentsArray);
// gs.info(ans);

//gs.info(incidentsArray.length);



var finalArray = [];

for(var i = 0; i < incidentsArray.length; i++){

var text1 = incidentsArray[i];


// Regex to match the incident details
var regex = /Incident\s*:\s*(INC\d+)\s*Affected User\s*:\s*([A-Za-z\s]+)\s*Priority\s*:\s*([^\n]+)\s*Assignment Group\s*:\s*([A-Za-z\s]+)/g;

var match;

// Loop through each match
if ((match = regex.exec(text1)) !== null) {
    // Create an object for each match and push it to the array
    finalArray.push({
        "Incident": match[1],
        "Affected User": match[2].trim(),
        "Priority": match[3].trim(),
        "Assignment Group": match[4].trim()
    });
}


}
gs.info((JSON.stringify(finalArray)));

 

 


First the entire data is divided by incidentsArray. Then, for every incidentArray is matched with the regex and pushed into the final array.

 

Run in the scripts - background to validate the output.

 

 

 

*** Script: [{"Incident":"INC001000","Affected User":"Abraham Lincoln","Priority":"2 - High","Assignment Group":"Application Developement"},{"Incident":"INC001002","Affected User":"Test","Priority":"3 - Moderate","Assignment Group":"Service Desk"}]

 

 




If this information helps you, kindly mark this as Helpful and Accept the solution.

Regards,
Najmuddin.

Hi @Charan123 ,

Did the new code work ?

 

Regards,
Najmuddin.