Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

How to properly call a script include from the record producer script field?

Linda6
Mega Guru

Dear Community, can someone please help me with my code?

I am creating a record producer, and in it's script field I am supposed to call a newly created script include, that will search for the user's assignment group and if that one is not available, then it shall use the fallback group saved in a sys property.

Can someone please tell me how I shall continue here...? As the code does not work...

From the record producer, I need to get the current.u_affected_user.company.u_service_desk_group. If that affected user does not have a service desk group, then a fallback group shall be used (saved in the given sys property)

I have read through various articles here, but am not able to put the code together properly...

Record producer's script field:

var callScriptInclude = new RecordProducerUtils().getAssignmentGroup(producer);

My script include:

var RecordProducerUtils = Class.create();
RecordProducerUtils .prototype = {

    getAssignmentGroup: function(producer) {
        if (producer.u_affected_user.company.u_service_desk_group != '') {
            producer.assignment_group = producer.u_affected_user.company.u_service_desk_group;
        } else {
            producer.assignment_group = gs.getProperty("record.producer.fallback.group");
        }
    },

    type: 'RecordProducerUtils '
};

Thank you in advance for any help...

3 REPLIES 3

chrisperry
Giga Sage

Hi there,

If you have a reference variable on sys_user to capture the 'u_affected_user' value then you may not need a script include at all to get the data you're looking for.

In the record producer script itself, you should be able to dot-walk on the reference variable's value like below:

var group = producer.u_affected_user.company.u_service_desk_group.toString();
if (group) {
	current.assignment_group = group;
} else {
	current.assignment_group = gs.getProperty('record.producer.fallback.group');
}

If this answer is helpful please mark correct and helpful!

Regards,

Christopher Perry

If this answer is helpful please mark correct and helpful!

Regards,
Chris Perry

Hello @christopherperry 

it works like a charm! However, this time the client really wants it to be set through a script include, could you maybe direct me on that, please?

 

 

Sure, it seems like an over-complication but regardless here is how you could accomplish that:

1. Update your script include to below code:

var RecordProducerUtils = Class.create();
RecordProducerUtils.prototype = {

    getAssignmentGroup: function(user) {
        var uGr = new GlideRecord('sys_user');
        if (uGr.get(user)) {
            var group = uGr.company.u_service_desk_group.toString();
            return group ? group : gs.getProperty('record.producer.fallback.group');
        }
    },

    type: 'RecordProducerUtils '
};

2. Update your record producer script to set Assignment group as below:

current.assignment_group = new RecordProducerUtils().getAssignmentGroup(producer.u_affected_user);

If this answer is helpful please mark correct and helpful!

Regards,

Christopher Perry

If this answer is helpful please mark correct and helpful!

Regards,
Chris Perry