How to add multiple record values to one array value?

sainath3
Mega Guru

Hi Team,

I have two tables,

1.Incident

2.Personal incident,

Below are the screenshots of table list view.

Incident table: Here "Incident IDs" field having multiple incidents

find_real_file.png

Personal Incident table:Here having two fields(Incident number, Comments)

find_real_file.png

Requirement:

Fetch all the comments from Personal incident table to Incident table(Field-name:Personal comments) with respective  Incident IDs field from incident table.

Ex:Incident Id's :INC0009005,INC0009009, So in Personal comments should have two comments :09876,hello

Note:Incident fields are string type in both tables.

 

 

1 ACCEPTED SOLUTION

Anil Lande
Kilo Patron

Hi,

You can write before Update/insert business rule on Incident table and use below script logic:

var comm =[];
var perInc = new GlideRecord('u_personal_incident');
perInc.addQuery('u_incident_number','IN',current.u_incident_ids.toSTring());
perInc.query();
while(perInc.next()){
comm.push(perInc.u_comments.toSTring());
}

current.u_personal_comments = comm.toString();

 

Please make required changes in above script as per your configuration.

 

Thanks,
Anil Lande

Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande

View solution in original post

6 REPLIES 6

Ravi9
ServiceNow Employee
ServiceNow Employee

can u share the code u have tried so far ? 

Hi Ravi,

Here is my code

var inc=GlideRecord('incident');
    inc.addNotNullQuery('u_incident_ids');
    inc.query();
while(inc.next()){
    
    var perinc=GlideRecord('u_personal_incidents');
    perinc.addEncodedQuery('u_incident_numberIN'+inc.u_incident_ids);
    
    perinc.query();
    var arr=[];
var j = 0;
    while(perinc.next()){
        arr[j]=perinc.u_comments.toString();
    //    gs.log(arr[j].length);
    gs.log(arr[j]);

               j++;
    //inc.u_personal_inc=arr[j];
        //inc.update();
    }
    

}

Output:

*** Script: hi
*** Script: 123456
*** Script: 09876
*** Script: hello

Expected Output:
*** Script: hi,123456
*** Script: 09876,hello

Hi,

Try below:

var inc=GlideRecord('incident');
    inc.addNotNullQuery('u_incident_ids');
    inc.query();
while(inc.next()){
    
    var perinc=GlideRecord('u_personal_incidents');
    perinc.addEncodedQuery('u_incident_numberIN'+inc.u_incident_ids.toString());    
    perinc.query();
    var arr=[];

    while(perinc.next()){
        arr.push(perinc.u_comments.toString());
    //    gs.log(arr[j].length);  

              
    
    }
gs.log(arr.toString());
    
//inc.u_personal_inc=arr.toString();
        //inc.update();
}

 

Thanks,
Anil Lande

Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande

If you want to use your own logic then try below:

var inc=GlideRecord('incident');
    inc.addNotNullQuery('u_incident_ids');
    inc.query();
while(inc.next()){    
    var perinc=GlideRecord('u_personal_incidents');
    perinc.addEncodedQuery('u_incident_numberIN'+inc.u_incident_ids);    
    perinc.query();
    var arr=[];
    var j = 0;
    while(perinc.next()){
        arr[j]=perinc.u_comments.toString();   
        
        j++;
    
    }
    gs.log(arr.toString());
//inc.u_personal_inc=arr.toString();
        //inc.update();

}

 

Thanks,
Anil Lande

Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande