The CreatorCon Call for Content is officially open! Get started here.

How to create temporary table records using parent array

AnilM99
Tera Expert

Hi Team,

my requirement is:

I am using rest message and get 3 arrays, and push 3 array to 1 parent array this is working good.

now my challenge is how to create temporary table records using parent array

example: 

var responsebody = [];

var response = [{"id":"c5864d0052db4ei0a481bbp0a1892fv8","name":"Test ServiceNow"}]

var response1 = [{"id":"d2454d0052db4ea0a481bbb0a2892fd5","name":"Test ServiceNow1"}]

var response2 = [{"id":"89d5d0052asjdi251bbb0a2asiome9axd3","name":"Test ServiceNow2"}]

 

responsebody.push(response);

responsebody.push(response1);

responsebody.push(response1);

 

now how to create temporary table record using "responsebody"

 

thanks

Anil

5 REPLIES 5

Its_Azar
Tera Guru
Tera Guru

Hi there @AnilM99 

you'll need to loop through the array and insert records into the temporary table.

like this

var responsebody = [];

var response = [{"id":"c5864d0052db4ei0a481bbp0a1892fv8","name":"Test ServiceNow"}];

var response1 = [{"id":"d2454d0052db4ea0a481bbb0a2892fd5","name":"Test ServiceNow1"}];

var response2 = [{"id":"89d5d0052asjdi251bbb0a2asiome9axd3","name":"Test ServiceNow2"}];

responsebody.push(response);

responsebody.push(response1);

responsebody.push(response2);


for (var i = 0; i < responsebody.length; i++) {
    var tempRecord = new GlideRecord('your_temporary_table_name');
    tempRecord.initialize(); 
    tempRecord.setValue('field_name', responsebody[i][0].name); 
    tempRecord.setValue('another_field_name', responsebody[i][0].id); 
    tempRecord.insert(); /
}

 

If this helps kindly accept the response thanks much.

☑️ If this helped, please mark it as Helpful or Accept Solution so others can find the answer too.




Kind Regards,

Mohamed Azarudeen Z

Developer @ KPMG

 Microsoft MVP (AI Services), India

Hi @Its_Azar thanks for the replay,

Your code is working fine but small correction in my script

This script is returning only first value of every Array -- responsebody[i][0]. name

The Actual  script is as the below:

var responsebody = [];

var response = [{"id":"c5864d0052db4ei0a481bbp0a1892fv8","name":"Test ServiceNow"},{"id":"c5864d0052db4ei0a481bbp0a1892fv8","name":"Testing ServiceNow"},{"id":"c5864d0052db4ei0a481bbp0a1892fv8","name":"Tested ServiceNow"}]

 

var response1 = [{"id":"d2454d0052db4ea0a481bbb0a2892fd5","name":"Test ServiceNow1"},{"id":"d2454d0052db4ea0a481bbb0a2892fd5","name":"Testing ServiceNow1"},{"id":"d2454d0052db4ea0a481bbb0a2892fd5","name":"Tested ServiceNow1"}]

 

var response2 = [{"id":"89d5d0052asjdi251bbb0a2asiome9axd3","name":"Test ServiceNow2"},{"id":"89d5d0052asjdi251bbb0a2asiome9axd3","name":"Testing ServiceNow2"},{"id":"89d5d0052asjdi251bbb0a2asiome9axd3","name":"Tested ServiceNow2"}]

 

 

Please help on the same.

 

Thanks Anil

Hi @AnilM99 

Could you share screenshot of your script ...

Here encountering an issue where only the first object of each array in responsebody is being processed. This happens because you're accessing the first element [0] of each object in the array.
However you can try this logic .

var responsebody = [];

var response = [
    {"id":"c5864d0052db4ei0a481bbp0a1892fv8","name":"Test ServiceNow"},
    {"id":"c5864d0052db4ei0a481bbp0a1892fv8","name":"Testing ServiceNow"},
    {"id":"c5864d0052db4ei0a481bbp0a1892fv8","name":"Tested ServiceNow"}
];

var response1 = [
    {"id":"d2454d0052db4ea0a481bbb0a2892fd5","name":"Test ServiceNow1"},
    {"id":"d2454d0052db4ea0a481bbb0a2892fd5","name":"Testing ServiceNow1"},
    {"id":"d2454d0052db4ea0a481bbb0a2892fd5","name":"Tested ServiceNow1"}
];

var response2 = [
    {"id":"89d5d0052asjdi251bbb0a2asiome9axd3","name":"Test ServiceNow2"},
    {"id":"89d5d0052asjdi251bbb0a2asiome9axd3","name":"Testing ServiceNow2"},
    {"id":"89d5d0052asjdi251bbb0a2asiome9axd3","name":"Tested ServiceNow2"}
];

responsebody.push(response);
responsebody.push(response1);
responsebody.push(response2);

for (var i = 0; i < responsebody.length; i++) {
    for (var j = 0; j < responsebody[i].length; j++) {
        var tempRecord = new GlideRecord('your_temporary_table_name');
        tempRecord.initialize(); 
        tempRecord.setValue('field_name', responsebody[i][j].name); 
        tempRecord.setValue('another_field_name', responsebody[i][j].id); 
        tempRecord.insert();
    }
}



If this solution resolves your query, kindly mark it as the accepted solution and give it a thumbs up.

Thanks,
Subhashis Ratna

Hi @AnilM99 

Have you tried this updated logic ?

Thanks
Subhashis Ratna