Need to auto populate the incident location and phone based on the affected user in the mobile agent

Rhonda9
Tera Expert

Hello,

I have a requirement to auto populate the incident location and phone field based on the Affected user details on the mobile when creating an incident. This works on the desktop ui but I am unable to get the same results in mobile.  I updated the mobile action item  with the script below but it is not working at all.   I'm not sure what I am doing wrong. I also would like the affected to be the same as caller by default. I tried a client script with a script include and that did not work either.  I need guidance on what to do. 

 

 

(function executeAction(input, output) {
    // Log the start of the action
    gs.info('Starting WriteBackAction for Incident creation.');

    // Initialize a new GlideRecord for the 'incident' table
    var gr = new GlideRecord('incident');
    gr.initialize();

    // Check if caller_id is provided
    if (!input.caller_id) {
        gs.error('Caller ID is missing from input.');
        output.status = 'failure';
        output.message = 'Caller ID is required.';
        return;
    }

    // Set the caller ID
    gr.setValue("caller_id", input.caller_id);

    // Set the affected user to be the same as the caller
    gr.setValue("u_affected_user", input.caller_id);

    // Retrieve user details for the affected user (same as caller)
    var userGr = new GlideRecord('sys_user');
    if (!userGr.get(input.caller_id)) {
        gs.error('User record not found for caller_id: ' + input.caller_id);
        output.status = 'failure';
        output.message = 'Caller user not found.';
        return;
    }

    // Populate incident location and phone with affected user's details
    if (userGr.location) {
        gr.setValue('location', userGr.location);
    } else {
        gs.warn('Location not found for user: ' + input.caller_id);
    }

    if (userGr.phone) {
        gr.setValue("u_phone", userGr.phone);
    } else if (userGr.mobile_phone) {
        gr.setValue("u_phone", userGr.mobile_phone);
    } else {
        gs.warn('No phone number found for user: ' + input.caller_id);
        gr.setValue("u_phone", ''); // Clear phone if not available
    }

    // Set other incident fields from input
    if (input.urgency) gr.setValue('urgency', input.urgency);
    if (input.short_description) gr.setValue('short_description', input.short_description);
    if (input.description) gr.setValue("description", input.description);
    if (input.assignment_group) gr.setValue("assignment_group", input.assignment_group);
    if (input.assigned_to) gr.setValue("assigned_to", input.assigned_to);
    if (input.business_service) gr.setValue("business_service", input.business_service);
    if (input.service_offering) gr.setValue("service_offering", input.service_offering);
    if (input.cmdb_ci) gr.setValue("cmdb_ci", input.cmdb_ci);
    if (!gs.nil(input.company)) gr.setValue("company", input.company);

    // Attempt to insert the record
    var incidentSysId = gr.insert();
    if (incidentSysId) {
        var incidentNumber = gr.getValue("number");
        gs.info('Success! Incident ' + incidentNumber + ' created.');

        // Return a success response with the incident number
        output.status = 'success';
        output.message = 'Incident ' + incidentNumber + ' created successfully.';
        output.incident_number = incidentNumber;
    } else {
        gs.error('Failure! Incident creation failed.');
        output.status = 'failure';
        output.message = 'Incident creation failed.';
    }
})(input, output);

 

2 REPLIES 2

Ankur Bawiskar
Tera Patron
Tera Patron

@Rhonda9 

what have you configured which works in desktop UI?

Is that a BR? If yes then why not same BR to be used for mobile?

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hello Ankur,  There are 2 client scripts already that was created when ServiceNow was implemented  but they dont work in mobile agent. 

1. Copy Caller to Affected user ( on change ) where the field name is caller_id

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
   
    var caller = g_form.getValue('caller_id');
    g_form.setValue('u_affected_user', caller);
   
}
 
2. Set fields from Affected user (on change) field name: u_affected user 
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '' || !newValue || newValue == oldValue) {
        return;
    }
   
    var affected = g_form.getReference('u_affected_user', setLocAndComp);
}
    function setLocAndComp(affected) {
       
        var locPresent = g_form.getControl('location');
        var compPresent = g_form.getControl('company');
        var phonePresent = g_form.getControl('u_phone');
       
        if (locPresent)
            g_form.setValue('location', affected.location);
        if (compPresent)
            g_form.setValue('company', affected.company);
        if (phonePresent){
            if(affected.phone != ''){
                g_form.setValue('u_phone', affected.phone);
            }
            else if(affected.mobile_phone != ''){
                g_form.setValue('u_phone', affected.mobile_phone);
            }
            else{
                g_form.setValue('u_phone', '');
            }
           
        }
        else{
            g_form.setValue('u_phone', '');
        }
    }