Delegate Request Script Needed

Community Alums
Not applicable

I Created a Delegate catalog Item to request Delegations and I attached one Workflow. In this work flow I used run script to create delegate record automatically.

Run Script:

var del = new GlideRecord('sys_user_delegate');
del.initialize();
del.user = current.variables.requested_for;
del.delegate = current.variables.delegate_user_name;
del.starts = current.variables.start_date;
del.ends = current.variables.end_date;
del.approvals = current.variables.approvals;
del.assignments = current.variables.assignments;
del.notifications = current.variables.all_notifications;
del.invitations = current.variables.meeting_invitations;
del.insert();

With above script new delegate record is created for requested user.

 

But I want script for If delegate record is already exits for requested user that can be replace or update existing record Instead of creating new record for same user with new details.

 

Can anyone help me on this?

1 ACCEPTED SOLUTION

Hello Akhila,

Change the code like below.

var del = new GlideRecord('sys_user_delegate');

del.addQuery('user',current.variables.requested_for);

del.query();

if(!del.next())
{
del.initialize();
del.user = current.variables.requested_for;
del.delegate = current.variables.delegate_user_name;
del.starts = current.variables.start_date;
del.ends = current.variables.end_date;
del.approvals = current.variables.approvals;
del.assignments = current.variables.assignments;
del.notifications = current.variables.all_notifications;
del.invitations = current.variables.meeting_invitations;
del.insert();
}
else
{
del.delegate = current.variables.delegate_user_name;
del.starts = current.variables.start_date;
del.ends = current.variables.end_date;
del.approvals = current.variables.approvals;
del.assignments = current.variables.assignments;
del.notifications = current.variables.all_notifications;
del.invitations = current.variables.meeting_invitations;
del.update();
}

 

Please mark my answer as helpful/correct if it helps you.

Regards,

Namrata

View solution in original post

3 REPLIES 3

Namrata Ghorpad
Mega Sage
Mega Sage

Hello Akhila,

You can update the script like below.

 

var del = new GlideRecord('sys_user_delegate');

del.addQuery('delegate',current.variables.requested_for);

del.query();

if(!del.next())

{
del.initialize();
del.user = current.variables.requested_for;
del.delegate = current.variables.delegate_user_name;
del.starts = current.variables.start_date;
del.ends = current.variables.end_date;
del.approvals = current.variables.approvals;
del.assignments = current.variables.assignments;
del.notifications = current.variables.all_notifications;
del.invitations = current.variables.meeting_invitations;
del.insert();

}

 

Please mark my answer as helpful/correct if it helps you.

Regards,

Namrata

Community Alums
Not applicable

Hi Namrata Ghorpadm,
I tried with above script but that is not working 

AkhilaNaradi_0-1673853640186.png

It creates new delegate record instead of updating new record.

Hello Akhila,

Change the code like below.

var del = new GlideRecord('sys_user_delegate');

del.addQuery('user',current.variables.requested_for);

del.query();

if(!del.next())
{
del.initialize();
del.user = current.variables.requested_for;
del.delegate = current.variables.delegate_user_name;
del.starts = current.variables.start_date;
del.ends = current.variables.end_date;
del.approvals = current.variables.approvals;
del.assignments = current.variables.assignments;
del.notifications = current.variables.all_notifications;
del.invitations = current.variables.meeting_invitations;
del.insert();
}
else
{
del.delegate = current.variables.delegate_user_name;
del.starts = current.variables.start_date;
del.ends = current.variables.end_date;
del.approvals = current.variables.approvals;
del.assignments = current.variables.assignments;
del.notifications = current.variables.all_notifications;
del.invitations = current.variables.meeting_invitations;
del.update();
}

 

Please mark my answer as helpful/correct if it helps you.

Regards,

Namrata