ServiceDesk Plus 附加字段设置为用户邮箱

[工单] 配置附加字段的值参考用户邮箱 | 附加字段可搜索所有用户邮箱

简介

在工单模板的配置过程中,会遇到部分场景需要附加字段引用用户的信息。比如申请权限的场景,用户A申请权限时,需要权限与用户B保持一致;又比如在移交资产时,用户A移交资产给用户B,需要用户B做确认接收资产。
目前局限:
如使用目前系统中的附加字段参考实体为User,则会仅展示用户的显示名。
不方便搜索,以及用户显示名会有重复的情况。
因此,暂时使用字段表单规则的方式来实现。

效果展示

输入两个字符以上后,会自动展示符合条件的用户邮箱,点击即可选择。

配置

全局函数


函数详情:
hosturl修改为可以直接调用的服务器本机URL,authtoken为技术员的API key
Quote
return {
"authtoken":"72342127-CD10-4277-A71C-D9BF74875D59"
};

回调自定义函数


函数详情:
Quote
global = global_config();

input_data= {
    "list_info": {
        "sort_field": "name",
        "start_index": 1,
        "sort_order": "asc",
        "row_count": "25",
        "get_total_count": true,
        "search_fields": {
            "email_id": param
        }
    },
    "fields_required": [
        "name",
        "is_technician",
        "citype",
        "login_name",
        "email_id",
        "department",
        "phone",
        "mobile",
        "jobtitle",
        "project_roles",
        "employee_id",
        "first_name",
        "middle_name",
        "last_name",
        "is_vipuser",
        "ciid"
    ]
};
response = invokeurl
[
url: global.get("hosturl")+"/api/v3/users"
type: GET
parameters: {"input_data":input_data}
headers: {"authtoken":global.get("authtoken")}
];
info response;
return response;


字段表单规则


脚本详情:
Quote
// 获取目标input元素,其中udf_sline_6902修改为实际附加字段的API字段名称
    var $input = jQuery('#for_udf_fields\\.udf_sline_6902');
    // 创建下拉容器
    var $dropdown = jQuery('<div class="autocomplete-dropdown"></div>')
        .css({
            'position': 'absolute',
            'z-index': '1000',
            'background': 'white',
            'border': '1px solid #ddd',
            'max-height': '200px',
            'overflow-y': 'auto',
            'display': 'none'
        })
        .insertAfter($input);

    // 输入事件处理(带防抖)
    var timer;
    $input.on('input', function() {
        clearTimeout(timer);
        timer = setTimeout(() => {
            var searchText = jQuery(this).val().trim();
            if (searchText.length >= 2) { // 至少2个字符才请求
                fetchSuggestions(searchText);
            } else {
                $dropdown.hide().empty();
            }
        }, 300);
    });

    // 获取建议数据
    function fetchSuggestions(query) {
    //调用回调函数,其中URL修改为实际回调自定义函数的自定义函数URL
jQuery.ajax({
        url:"/AppIntegrations?serviceName=callbackFunctions&api_name=get_user_list&auth_token=b44073e0c0313066e05cb8f5b4bbe29c237d4e77b6a2a9e7bed97c4ae3ed4842a71f607baad0a9762685d4469065bb385a0b1f7cef910e619d94e3ed9a94b30d85031457&PORTALID=1",
        type:"POST",
        async:false,
        cache:false,
        data:{"arguments":'{"param":"'+query+'"}'},
        success(data){
           dataJson = JSON.parse(data);
            result= dataJson.output;
             console.log(result);
             renderDropdown(result.users);
        }
   }); 
}

    // 渲染下拉选项
    function renderDropdown(items) {
        $dropdown.empty();
        items.forEach(item => {
            jQuery('<div class="select2-results-dept-0 select2-result select2-result-selectable"></div>')
                .text(item['email_id'])
                .css({ 'padding': '5px', 'cursor': 'pointer', 'width': '500px' })
                .hover(
                    () => jQuery(this).css('background', '#f5f5f5'),
                    () => jQuery(this).css('background', 'white')
                )
                .click(() => {
                    $input.val(item['email_id']);
                    $dropdown.hide();
                })
                .appendTo($dropdown);
        });
        $dropdown.show();
    }

    // 点击外部隐藏下拉
    jQuery(document).click(function(e) {
        if (!jQuery(e.target).closest('.autocomplete-dropdown, #for_udf_fields\\.udf_sline_301').length) {
            $dropdown.hide();
        }
    });