Another Example

  • Script Includes Script

    var IdentifyTwoUsers = Class.create();
    identifyTwoUsers.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    
        userNames: function() {
            var id1 = this.getParameter('syspart_user_id1');
            var id2 = this.getParameter('syspart_user_id2');
            var u1 = newGlideRecord('sys_user');
            var u2 = newGlideRecord('sys_user');
    
            if (!u1.get('employee_number', id1))
                return '';
            if (!u2.get('employee_number', id2))
                return '';
    return u1.name + ":" + u2.name;
        }
    })
    

In this example, the Script Include extends the AbstractAjaxProcessor Class.

The IdentifyTwoUsers object has a function called userName which is passed two values:|

  • sysparm_user_id1
  • sysparm_user_id2

The Script Include retrieves the records from the database for both sysparm_user_id1 and sysparm_user_id2.

If the records both have a value in the employee_number field, the Script Include concatenes the two employee numbers with a: (colon) and returns the string to the calling script.

  • Client-side Script

    var twoUsers = new GlideAjax('IdentifyTwoUsers');
    twoUsers.addParam('sysparm_name', 'userNames');
    twoUsers.addParam('sysparm_user_id1', g_form.source_id);
    twoUsers.addParam('sysparm_user_id2', g_form.target_id);
    twoUsers.getXML('userParse');
    
    function userParse(response) {
    var answerFromXML = response.responseXML.
    documentElement.getAttribute("answer");
    var user = answerFromXML.split(':');
    var congMerge = confirm('Merge ' + user[0] + ' --> ' + user[1] + '\nAre you sure?');
    
    if(confMerge) {
    <additional script logic would go here...>
    }
    }
    

The cliend-side code shown here would be wrapped in a function (onLoad(), onSubmit(), or onChange()) depending on the type of client-side script.

In this example, the answerFromXML variable from the response contains multiple pieces of information separated by a colon. The statement var user=answerFromXML.split(':') breaks the answer variable into an array called user with each element being whatever is between each set of colons. In this case, user[0] contains the value from the beginning of the answer string until the first colon, and user[1] contains the value from the colon to the end of string. Assume answer = Matt:Miranda. In this case user[0] = "Matt" and user[1] = "Miranda".

results matching ""

    No results matching ""