Auto-Populate new field

Joseph Navarro1
Tera Contributor

Hello, I am trying to add a new field for Task_time_worked labeled account that pulls the Account info from the relevant field in the Task table (Account for a Case, Company for Tasks, etc.). What would be the best way to go about automating this?

4 REPLIES 4

Tony Chatfield1
Kilo Patron

Hi, based on the details you have provided, I would look at a before insert BR on task_time_worked you should be able to dot.walk\map your account\company field from your task record.

Thank you! That has gotten me on the right track. Is there a way to apply this logic to all previous task_time_worked entries (as this is a new field)?

Hi, you indicated in your first post that you intended to add a new field to task_time_worked , which I assume would be a reference to core_company table (so you can populate company or account).
Once the field was added, and your BR was populating to field for new records, you would need to run a script across all existing records in order to populate the field for existing records.

As case.account and task.company are different reference fields, your scripts will need to check the type of task related to your task_time_worked record so that you can identify which field to map.

Examples of beforeInsert BR for 'task' and a background script to update existing records that should deliver your requirement. (note both untested).

//Some platforms extend the Case table
//and so you need to identify any tasks extended from case as they use the Account field not the company field.

var accountTypes = ['sn_customerservice_case', 'sn_customerservice_random_name'];

if(accountTypes.toString().indexOf(current.task.sys_class_name) != -1) {
    current.yourNewField = current.task.account;
} else {
//Default for tasks that use the 'Company' field
current.yourNewField = current.task.company;
}

 

//background script
var accountTypes = ['sn_customerservice_case', 'sn_customerservice_random'];

var recUpdate = new GlideRecord('task_time_worked');
recUpdate.addEncodedQuery('yourNewField=NULL');
recUpdate.query();

while(recUpdate.next()) {

if(accountTypes.toString().indexOf(recUpdate.task.sys_class_name.toString()) != -1) {
    recUpdate.yourNewField = recUpdate.task.account.toString();
} else {
//Default for tasks that use the 'Compnay' field
recUpdate.yourNewField = recUpdate.task.company.toString();
}

recUpdate.update();
}

 

Andrew_TND
Mega Sage
Mega Sage

Hi @Joseph Navarro1 

 

As the ilIncident table is a child of TASK when implementing the business rule it should be completely no code as you should be able to just dot walk the values. 

Please mark as helpful or if it’s resolved the issue, CORRECT!