Auto-create Related List Record on Creation of Parent Record

michaelcory
Giga Expert

Thought this was going to be easy, but finding it's not.

Parent table = Demand (dmn_demand)

Related LIst Table = u_dmn_resources_operational

Reference field on Related Table to create relationship = Demand (u_demand)

I simply want to auto-create a record on the Related List table and set a field value (Resource Team) on Related List when a New Demand is created.   The Resource Team (u_resource_team) field on the Related list references a custom table.   Need the Resource Team to be set to one of the table values.   Trying to use the sys id of the table value to set.   Not as concerned about setting the field value yet, since I can't even get the Related LIst record to auto-generate.

Using the following BR (Insert & Update) to try and auto-create the Related LIst record on creation of New Demand, but not working.   Also tried a workflow (run script) but can't get that to work either.

function onAfter(current, previous) {

    //This function will be automatically called when this rule is processed.

var gr = new GlideRecord('u_dmn_resources_operational');

gr.addQuery('u_demand',current.getValue('sys_id'));//reference field on related list table

gr.initialize();

gr.u_resource_team = '300c2a49131a2200efb879812244b0ba';//Tech-IL Infrastructure

gr.insert();

}

1 ACCEPTED SOLUTION

James_Neale
Mega Guru

You are missing a few important bits from your script.



function onAfter(current, previous) {


  //This function will be automatically called when this rule is processed.


  var gr = new GlideRecord('u_dmn_resources_operational');


  gr.addQuery('u_demand',current.sys_id);//reference field on related list table


  gr.query();


  if (!gr.next()) {


      gr.newRecord();


      gr.u_demand = current.sys_id;


      gr.u_resource_team = '300c2a49131a2200efb879812244b0ba'; //Tech-IL Infrastructure


      gr.insert();


  }


}



A quick point on good practice as well - you shouldn't be using hard-coded sys_id's in any script (the resource team in this case). Instead add a system property and use that. E.g.



gr.u_resource_team.setDisplayValue(gs.getProperty('my_company.demand.default.resource.team'));



Cheers,


James


View solution in original post

2 REPLIES 2

James_Neale
Mega Guru

You are missing a few important bits from your script.



function onAfter(current, previous) {


  //This function will be automatically called when this rule is processed.


  var gr = new GlideRecord('u_dmn_resources_operational');


  gr.addQuery('u_demand',current.sys_id);//reference field on related list table


  gr.query();


  if (!gr.next()) {


      gr.newRecord();


      gr.u_demand = current.sys_id;


      gr.u_resource_team = '300c2a49131a2200efb879812244b0ba'; //Tech-IL Infrastructure


      gr.insert();


  }


}



A quick point on good practice as well - you shouldn't be using hard-coded sys_id's in any script (the resource team in this case). Instead add a system property and use that. E.g.



gr.u_resource_team.setDisplayValue(gs.getProperty('my_company.demand.default.resource.team'));



Cheers,


James


michaelcory
Giga Expert

Thanks James for the quick reply and the good practice tip.   Script works perfectly!   Thanks again