I need alert opened date should be populate on detection time filed in the incident

ashok17
Tera Contributor

I need alert opened date should be populate on detection time filed in the incident, Please suggest how to achieve this requirement and i have tried below After Business Rule on incident table but not works:

 

if (!current.u_detection_time && current.u_alert) {
var alertRec = new GlideRecord('u_alert');
if (alertRec.get(current.u_alert)) {
current.u_detection_time = alertRec.u_opened_at;
}
}

22 REPLIES 22

Hi @ashok17 ,
The script is working and I verified on my end.

Rakesh_M_0-1777538786543.png

Rakesh_M_1-1777538808125.png

 


Make sure the business rule runs after insertion and verify the filed names.
Add log messages and check where it is failing.

(function executeRule(current, previous /* null when async */ ) {
    // Get the reference to the related Incident record from the Alert
	gs.log("Sync Detection time BR Executed.","syncBR");
    var incidentSysID = current.u_task; // reference field on Alert table
	gs.log("Incident sysID:"+incidentSysID,"syncBR");

    if (incidentSysID) {
        var incGr = new GlideRecord('incident');
        if (incGr.get(incidentSysID)) {
            // Set Detection Time on Incident
            incGr.u_detection_time = current.u_opened_at; // alert's opened date
            incGr.update();
            gs.log("Detection Time updated for incident " + incGr.number + " with " + current.u_opened_at,"syncBR");
        } else {
            gs.log("Incident not found for sys_id: " + incidentSysID,"syncBR");
        }
    }
})(current, previous);


try with new records.
1.Create a incident and save the record.
2.Then create a alert record and check.

ashok17
Tera Contributor

Hi @Rakesh_M ,

 

I have tried same script in After Business Rule but did not work for my side, please refer below screenshots and suggest if any wrong on script side.

ashok17_0-1777540337924.pngashok17_1-1777540391886.png

 

Hi @ashok17 ,
Can you share the logs with source=syncBR.
also share when to run condition

ashok17
Tera Contributor

Below is the condition have given in Business Rule and could you please confirm the Business Rule have selected incident is it correct right?

 

ashok17_0-1777543959508.png

 

hi @ashok17 ,

Please update the table name to the Alert table.

The current Business Rule works when the Incident is created first, and then the Alert is created — in this case, the Incident gets updated.

If your use case is different (for example, creating the Alert first and then the Incident), the script will need to be adjusted.

Could you please confirm your use case?