delegate record in script include

siva14
Tera Contributor

Hi Everyone,

in the below script include , we are using to create delegate record, but in that we want to check any already existing record is created or not in past six months, can anyone help me how to do that,

 

addDelegate: function(userID, delegateID) {
        var gdt = new GlideDateTime();
        var delegate = new GlideRecord("sys_user_delegate");
        delegate.addQuery('user.active', true);
        delegate.initialize();
        delegate.setValue("user", userID);
        delegate.setValue("delegate", delegateID);
        delegate.setValue("approvals", true);
        delegate.setValue("assignments", true);
        delegate.setValue("notifications", true);
        delegate.setValue("invitations", true);
        delegate.setValue("starts", gdt.getValue());
        gdt.addYears(50);
        delegate.setValue("ends", gdt.getValue());
        delegate.insert();
        return delegate;
    },
 
#delegate #script include
1 REPLY 1

Tony Chatfield1
Kilo Patron

Hi, I think something like this should work.
I don't think a check for user.active would add any value here, and if the check is required it should be made in the script that calls this function IE don't call the function unless both your user(s) are valid.

Also while this matches you post requirement I think you would need to extend the query to ensure that any existing delegation created in the last 6 months has not ended already.

addDelegate: function(userID, delegateID) {

    var myQuery = "user=" + userID + "^delegate=" + delegateID + "^sys_created_onONLast 6 months@javascript:gs.beginningOfLast6Months()@javascript:gs.endOfLast6Months()";

        var delegate = new GlideRecord("sys_user_delegate");
        delegate.addEncodedQuery(myQuery);
		delegate.query();
		
		if(delegate.next()) {
		 gs.info('record match found');
		} else {
        delegate.initialize();
        delegate.setValue("user", userID);
        delegate.setValue("delegate", delegateID);
        delegate.setValue("approvals", true);
        delegate.setValue("assignments", true);
        delegate.setValue("notifications", true);
        delegate.setValue("invitations", true);
        delegate.setValue("starts", gdt.getValue());
        gdt.addYears(50);
        delegate.setValue("ends", gdt.getValue());
        delegate.insert();
        return delegate;
		}
    },