Need help in setting values from array

Pihu1
Tera Contributor

Hi Community,

 

I need help in setting field values from an array containing strings. I have a requirement to set incident fields short description, on behalf of and state from an array containing string values.

For example, I am passing an array to Script include function as a parameter as below where inc_fields is an array containing values ["7110868edb5325907e04450b1396199b","test created inc","2"]

How to set fields from this array? below is my code

createIncident: function (caller, urgency, description, category, inc_fields) {
        var incDetails = {};
        var incGr = new GlideRecord("incident");
        incGr.initialize();
        incGr.setValue("caller_id", caller);
        incGr.setValue("urgency", urgency);
        incGr.setValue('category', category);
        incGr.setValue("description", description);
        incGr.setValue("u_on_behalf_of", inc_fields[0]);
        incGr.setValue("short_description", inc_fields[1]);
        incGr.setValue("state", inc_fields[0]);
     
        incDetails.sysid = incGr.insert();
        incDetails.incNum = incGr.getValue('number');
 
Thanks in advance.
 
Pihu
6 REPLIES 6

Aman Kumar S
Kilo Patron

just pass the array as the functional parameter, assuming the sequence is correct

createIncident: function (arr) {
var incDetails = {};
var incGr = new GlideRecord("incident");
incGr.initialize();
incGr.setValue("caller_id", arr[0]);
incGr.setValue("urgency", arr[1]);
incGr.setValue('category', arr[2]);
incGr.setValue("description", arr[3]);
incGr.setValue("u_on_behalf_of", arr[4]);
incGr.setValue("short_description", arr[5]);
incGr.setValue("state", arr[]);6

incDetails.sysid = incGr.insert();
incDetails.incNum = incGr.getValue('number');

Best Regards
Aman Kumar

Hi Aman,

 

I don't want to set all the fields from the array, here only inc_fields is a array which contains values and want to set short description , on behalf of and state.

createIncident: function (caller, urgency, description, category, inc_fields) {
        var incDetails = {};
        var incGr = new GlideRecord("incident");
        incGr.initialize();
        incGr.setValue("caller_id", caller);
        incGr.setValue("urgency", urgency);
        incGr.setValue('category', category);
        incGr.setValue("description", description);
        incGr.setValue("u_on_behalf_of", inc_fields[0]);
        incGr.setValue("short_description", inc_fields[1]);
        incGr.setValue("state", inc_fields[0]);
     
        incDetails.sysid = incGr.insert();
        incDetails.incNum = incGr.getValue('number');

@Pihu1 

so inc_fields is your array, can you share how are you populating the array, and how the values looks like. share the code as well.

Best Regards
Aman Kumar

Aniket Chavan
Tera Sage
Tera Sage

Hello @Pihu1 ,

Please give a try to the modified code below and see how it works for you.

createIncident: function (arr) {
    var incDetails = {};
    var incGr = new GlideRecord("incident");
    incGr.initialize();
    incGr.setValue("caller_id", arr[0]);
    incGr.setValue("urgency", arr[1]);
    incGr.setValue('category', arr[2]);
    incGr.setValue("description", arr[3]);
    incGr.setValue("u_on_behalf_of", arr[4]);
    incGr.setValue("short_description", arr[5]);
    incGr.setValue("state", arr[6]); // Corrected the index

    incDetails.sysid = incGr.insert();
    incDetails.incNum = incGr.getValue('number');

    return incDetails;
}

 

Please Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks,

Aniket