How does parent case work with business rule and m2m table?

AngelP83
Giga Guru

I need to understand the logic of the following:

 

I have 4 tables that work in the following way:

SIR (table 1) has a List Control with another table name Observable.

When I click NEW on the Observable List, it opens Table 2 as a form for me to enter information.

At this point, the record is created in Table 2 with Table 1 as the parent, which can be viewed in Table 3 (controlled by a M2M table).

So Table 3 (M2M table) will show something like:

Observable         Task

Honduras            SIR0001

 

Table 2 has a Business Rule that runs if the field “TYPE” changes to Soccer.  If it changes, the business rule will look at the Table 3 and grab the observable name and the Task # (SIR), and it will query some basic information such as: Type, and Short Description:  After collecting the data, it will create a new record on Task (table 4).

So, this is already built, and it is working as expected.

My question is:

As mentioned before, the user can click NEW on the List Observable, but this user wants to choose the type SOCCER directly which should create an SIT (on Table 4) without using the script above.

The only reason the script above works is because the record is already created, but if I am creating a new record from BR Table 2 to Table 4, the Table 3 information might not even exist yet.

I hope this makes sense.

Bottom line, I need some help to create a Child ticket using a BR from Table 2 in Table 4, and the Parent should be Table 1.  

I am adding the BR script that is currently working if the user changes the TYPE to Soccer.  I would like to reuse the script so users don't need to create the record first and then switch the type.  user wants to select the Type soccer right away.

Business Rule is on Table 2,

AFTER = 100

Type changes to Soccer

 

 

(function executeRule(current, previous /*null when async*/ ) {
	
		
    var res = {};
	
	//var obtask = new GlideRecord("ti_m2m_task_observable");
	//obtask.addEncodedQuery("observable=" + current.sys_id);
	
	
    obtask.orderBy("sys_created_on");
    obtask.setLimit(1);
    obtask.query();
    if (obtask.next()) {
        res.value = obtask.observable.value;
        res.typeName = obtask.observable.type.name;
        res.obSightingCount = obtask.observable.sighting_count;
        res.obNotes = obtask.observable.notes;
        res.createdBy = obtask.getValue("sys_created_by");
        res.taskId = obtask.sys_id;
        res.assignedToName = obtask.assigned_to.user_name;
        res.sdesc = obtask.short_description;
        res.source = obtask.task.u_source.getDisplayValue();
    }

    var sit = new GlideRecord("sn_task");
    var desc = "";
    desc += "Incident Source System: " + res.source + "\n";
    desc += "Value: " + res.value + "\n";
    desc += "Type: " + res.typeName + "\n";
    desc += "Incident Count: " + res.obSightingCount + "\n";
    desc += "Notes: " + res.obNotes;
    sit.initialize();
    sit.setValue("parent", res.taskId);
    sit.setValue("priority", 3);
    sit.setValue("assignment_group", "056ef4e01b9d34501d84a71b234bc231312be3"); 
	sit.setValue("assigned_to", "a8a597f41b893b00782bb8cc1d4bc21321321bb9"); 
    sit.setValue("short_description", "False Positive Request");
    sit.setValue("description", desc);
	
	if (res.createdBy != "admin" && res.createdBy != "system") {
        sit.setValue("affected_user", getUser(res.createdBy));
    } else {
        sit.setValue("affected_user", getUser(res.assignedToName));
    }
    sit.insert();
	
	

    function getUser(name) {
        var usr = new GlideRecord("sys_user");
        usr.addQuery("user_name", name);
        usr.setLimit(1);
        usr.query();
        if (usr.next()) {
            return usr.getValue("sys_id");
        }
        return "";
    }
})(current, previous);

 

 

1 ACCEPTED SOLUTION

Bert_c1
Kilo Patron

Hi Miguel33,

 

I saw a similar post where I proposed a similar fix.

 

function executeRule(current, previous /*null when async*/ ) {
	
		
 //   var res = {};
	
	//var obtask = new GlideRecord("ti_m2m_task_observable");
	//obtask.addEncodedQuery("observable=" + current.sys_id);
	
	
    obtask.orderBy("sys_created_on");
    obtask.setLimit(1);
    obtask.query();
    if (obtask.next()) {
        var res.value = obtask.observable.value;
        var restypeName = obtask.observable.type.name;
        var resobSightingCount = obtask.observable.sighting_count;
        var resobNotes = obtask.observable.notes;
        var rescreatedBy = obtask.getValue("sys_created_by");
        var restaskId = obtask.sys_id;
        var resassignedToName = obtask.assigned_to.user_name;
        var ressdesc = obtask.short_description;
        var ressource = obtask.task.u_source.getDisplayValue();
  
		// this should be inside the 'if' statement for obtask.next()
		var sit = new GlideRecord("sn_task");
		var desc = "";
		desc += "Incident Source System: " + res.source + "\n";
		desc += "Value: " + res.value + "\n";
		desc += "Type: " + res.typeName + "\n";
		desc += "Incident Count: " + res.obSightingCount + "\n";
		desc += "Notes: " + res.obNotes;
		sit.initialize();
		sit.setValue("parent", res.taskId);
		sit.setValue("priority", 3);
		sit.setValue("assignment_group", "056ef4e01b9d34501d84a71b234bc231312be3"); 
		sit.setValue("assigned_to", "a8a597f41b893b00782bb8cc1d4bc21321321bb9"); 
		sit.setValue("short_description", "False Positive Request");
		sit.setValue("description", desc);
		
		if (res.createdBy != "admin" && res.createdBy != "system") {
			sit.setValue("affected_user", getUser(res.createdBy));
		} else {
			sit.setValue("affected_user", getUser(res.assignedToName));
		}
		sit.insert();
	}
}	

function getUser(name) {
    var usr = new GlideRecord("sys_user");
    usr.addQuery("user_name", name);
    usr.setLimit(1);
    usr.query();
    if (usr.next()) {
        return usr.getValue("sys_id");
    }
    return "";
}

 

See existing BRs for script syntax. I suspect that your use of "var res = {};" and trying to load values later is a problem.  Also, look at existing script syntax. I hope this helps.

 

Others may post their thoughts here.

View solution in original post

1 REPLY 1

Bert_c1
Kilo Patron

Hi Miguel33,

 

I saw a similar post where I proposed a similar fix.

 

function executeRule(current, previous /*null when async*/ ) {
	
		
 //   var res = {};
	
	//var obtask = new GlideRecord("ti_m2m_task_observable");
	//obtask.addEncodedQuery("observable=" + current.sys_id);
	
	
    obtask.orderBy("sys_created_on");
    obtask.setLimit(1);
    obtask.query();
    if (obtask.next()) {
        var res.value = obtask.observable.value;
        var restypeName = obtask.observable.type.name;
        var resobSightingCount = obtask.observable.sighting_count;
        var resobNotes = obtask.observable.notes;
        var rescreatedBy = obtask.getValue("sys_created_by");
        var restaskId = obtask.sys_id;
        var resassignedToName = obtask.assigned_to.user_name;
        var ressdesc = obtask.short_description;
        var ressource = obtask.task.u_source.getDisplayValue();
  
		// this should be inside the 'if' statement for obtask.next()
		var sit = new GlideRecord("sn_task");
		var desc = "";
		desc += "Incident Source System: " + res.source + "\n";
		desc += "Value: " + res.value + "\n";
		desc += "Type: " + res.typeName + "\n";
		desc += "Incident Count: " + res.obSightingCount + "\n";
		desc += "Notes: " + res.obNotes;
		sit.initialize();
		sit.setValue("parent", res.taskId);
		sit.setValue("priority", 3);
		sit.setValue("assignment_group", "056ef4e01b9d34501d84a71b234bc231312be3"); 
		sit.setValue("assigned_to", "a8a597f41b893b00782bb8cc1d4bc21321321bb9"); 
		sit.setValue("short_description", "False Positive Request");
		sit.setValue("description", desc);
		
		if (res.createdBy != "admin" && res.createdBy != "system") {
			sit.setValue("affected_user", getUser(res.createdBy));
		} else {
			sit.setValue("affected_user", getUser(res.assignedToName));
		}
		sit.insert();
	}
}	

function getUser(name) {
    var usr = new GlideRecord("sys_user");
    usr.addQuery("user_name", name);
    usr.setLimit(1);
    usr.query();
    if (usr.next()) {
        return usr.getValue("sys_id");
    }
    return "";
}

 

See existing BRs for script syntax. I suspect that your use of "var res = {};" and trying to load values later is a problem.  Also, look at existing script syntax. I hope this helps.

 

Others may post their thoughts here.