There is no record found but sys id and number is created while using inbound email action, how to resolve this?

Pragyan das
Tera Contributor

The below script is used in Inbound action to create incident if no record is found:-

 

 else { //Create a new Incident if no original reference is found
    gs.log('Email sys_id: ' + sys_email.sys_id + ' ELSE statement: Creating new Incident because no original reference was found');
    var newInc = new GlideRecord('incident');
    newInc.comments = "received from: " + email.origemail + "\n\n" + email.body_text;
    newInc.short_description = email.subject;
 
    var userObj = new GlideRecord('sys_user'); //get user Object
    if (userObj.get('email', email.from)) {
        newInc.caller_id = userObj.sys_id;
        newInc.opened_by = userObj.sys_id;
        gs.log("check4");
 
        //Update location and country of caller to United Kingdom
        var usr_uk = new GlideRecord("sys_user");
        usr_uk.addQuery("sys_id", userObj.sys_id);
        usr_uk.query();
        if (usr_uk.next()) {
 
            var role = new GlideRecord('sys_user_has_role');
            role.addQuery('user', usr_uk.sys_id);
            role.addQuery('role', 'a830c07a87b8cd5088d00e150cbb3531');
            role.query();
            if (!role.next()) {
                role.initialize();
                role.user = usr_uk.sys_id;
                role.role = "a830c07a87b8cd5088d00e150cbb3531";
                role.insert();
            }
 
            usr_uk.location = gs.getProperty('location.uk');
            usr_uk.location.country = 'United Kingdom';
            usr_uk.update();
        }
 
 
 
    } else {
        //If the email address cannot be matched to a user, we want ServiceNow to create the record
        //on behalf of the default "Guest" account
        newInc.caller_id = gs.getUserID();
        newInc.opened_by = gs.getUserID();
        gs.log("test exe 2");
        //newInc.caller_id.location = gs.getProperty('location.uk');
 
 
    }
    newInc.assignment_group = gs.getProperty('PEXAUKSupportCenter');
    newInc.incident_state = 1;
    newInc.notify = 2;
    newInc.contact_type = "Email";
    newInc.u_category = "incident_uk";
 
    newInc.location = gs.getProperty('location.uk');
    //newInc.caller_id.location =gs.getProperty('location.uk');
    //newInc.caller_id.location.country = 'United Kingdom';
 
  if (email.importance != undefined)
        if (email.importance == "High")
            newInc.priority = 1;
    if (email.body.priority != undefined)
        newInc.priority = email.body.priority;
    newInc.insert();
    gs.log("Check5 PTR:" + newInc.sys_id + newInc.caller_id + newInc.u_category + "" + newInc.number);
    updateEmailtarget(newInc.sys_id);
    attachFiles(newInc.sys_id);
}

 

 

Logs:-

Check5 PTR :ef41338d87791510c54c0e170cbb356c5136 503cc611227c0183e96598c4f706 incident_uk PTR1244382

I logs sys id and number is there,but no record in table.

 

4 REPLIES 4

Pavankumar_1
Mega Patron

Hi,

You have to use newInc.initialize(); this before inserting the incidents. This is the reason for that you are not able to see in backend but it will show those details but it will not save.

replace below code on last of your code or add below line in your code.

if (email.body.priority != undefined){
       newInc.initialize();  //add this line it will works
        newInc.priority = email.body.priority;
    newInc.insert();
    gs.log("Check5 PTR:" + newInc.sys_id + newInc.caller_id + newInc.u_category + "" + newInc.number);
    updateEmailtarget(newInc.sys_id);
    attachFiles(newInc.sys_id);
}
}
 

Hope you it helps you.

Please Mark Correct/helpful if applicable, Thanks!! 

 

Regards

Pavankumar

If it helps please click Accept as Solution/hit the Thumb Icon.
ServiceNow Community MVP 2024.
Thanks,
Pavankumar

Hi Pavan,

Thanks for the reply.

Actually, this code is working fine in my development instance and creating record but not working in pre-prod

Hi,

is it triggering the email action?

have you kept any logs ?

 

Thanks,

Pavankumar

If it helps please click Accept as Solution/hit the Thumb Icon.
ServiceNow Community MVP 2024.
Thanks,
Pavankumar

Pavankumar_1
Mega Patron

Hi @Pragyan das ,

If you issue resolved please close the thread so that others will get benefit.

Please Mark Correct/helpful if applicable, Thanks!! 

 

Regards

Pavankumar

If it helps please click Accept as Solution/hit the Thumb Icon.
ServiceNow Community MVP 2024.
Thanks,
Pavankumar