Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Inserting multiple records using for loop issue

Aditi Jain 9
Tera Contributor

Hi All,
I want to insert multiple demand records in dmn_demand table. I am using for-loop for the insertion in the workflow run script. But it is creating 1000s of records. Even if hardcord i<5 then it should create 5 records, but its creating 1000s of records.  Below is the script:

var scope = [];
workflow.scratchpad.dmnID = [];
var count = 0;
scope = current.variables.scope.toString().split(',');
for (var i = 0; i < scope.length; i++) {
    count++;
  
    var dmn = new GlideRecord('dmn_demand');
    dmn.initialize();
    dmn.short_description = current.variables.name_of_request_project_name;
    if (scope[i] == '3d33163adba335102b1bfae4e29619a8') { //Customer
        dmn.portfolio = 'eae5dba747723d54c72639e5536d43f0'; //MDM - Customers
    } else if (scope[i] == 'c843523adba335102b1bfae4e29619a7') { //Channel Partner
        dmn.portfolio = 'eae5dba747723d54c72639e5536d43f0'; //MDM - Customers
    } else if (scope[i] == '4c63123adba335102b1bfae4e296190e') { //Direct Materials
        dmn.portfolio = '95b1572e4744c214c72639e5536d4363'; //MDM - Direct Materials
    } else if (scope[i] == 'e1839a3adba335102b1bfae4e2961931') { //Indirect Materials
        dmn.portfolio = '3677479697ac4e5022b1ff000153af84'; //MDM - Indirect Materials
    } else if (scope[i] == '5873d27adba335102b1bfae4e2961944') { //Packaging
        dmn.portfolio = '191d693997d40ed422b1ff000153af75'; //MDM - Packaging Materials
    } else if (scope[i] == 'ec53123adba335102b1bfae4e296190c') { //Vendor
        dmn.portfolio = '7fd00135979ccad422b1ff000153af87'; //MDM - Vendors
    }

    //dmn.portfolio = '4edec9c31b0ca810a18cdd3bdc4bcb43'; //IT User Experience
    dmn.primary_program = '2989ae291ba56850a18cdd3bdc4bcb37'; //ServiceNow
    dmn.u_requested_by = current.variables.u_requested_by;
    dmn.u_service_organization = '6d3c0837db2b1f00f23c349f9d9619c6'; //User Experience
    dmn.category = 'strategic'; //Strategic
    dmn.type = 'project'; //Project
    //dmn.demand_manager = current.variables.key_contact;
    dmn.demand_manager = 'aba8112ddb46f784c54c3ccb7c961913'; //Shaik, Shearu
    dmn.start_date = current.variables.start_date;
    dmn.requested_by = current.variables.end_date;
    dmn.assumptions = "List Initiative = " + current.variables.list_initiative + "<br></br>" +
        "List Tier One IT Project = " + current.variables.list_tier_one_it_project;
    dmn.in_scope = "Scope = " + current.variables.scope.getDisplayValue() + "<br></br>" +
        "Update Method = " + current.variables.update_method.getDisplayValue() + "<br></br>" +
        "Add Details = " + current.variables.add_details + "<br></br>" +
        "Number Of Records = " + current.variables.number_of_records.getDisplayValue() + "<br></br>" +
        "Type of Request = " + current.variables.type_of_request_2.getDisplayValue();
    dmn.parent = current.sys_id;
    var dmnID = dmn.insert();
 
    workflow.scratchpad.dmnID.push(dmnID);
}
Please let me know if anyone has any idea about it.
1 REPLY 1

Aicha_Fall
Tera Contributor

Hello. I'm having the same issue were you able to solve it?

 

Thank you