Inbound email action issues

Szilard
Tera Guru

Hello community!

I have an inbound email action, and the script within it looks like this:

 

var rsubject = email.subject;
  var keyword = "[SZR-Hibabejelentés - Hibabejelentés ";
  var len = keyword.length;
  var key = rsubject.indexOf(keyword);
  var hook = new Onlinewebhook;
  
  if (key >= 0) { 
    var start = key + len;
    var end = rsubject.indexOf("]", start); 
    if (end >= 0) {
      var number = rsubject.substring(start, end);
		gs.log('eternalID: ' + number);
      // Ellenőrizzük, hogy létezik-e már az azonosítóval rendelkező rekord
      var existingIncident = new GlideRecord('incident');
      existingIncident.addQuery('u_external_ticket_id', number);
      existingIncident.query();
		
      
      if (existingIncident.next()) {
        // Létező rekord esetén hozzáadjuk a levél tartalmát a comment mezőhöz
        var journal = "\n\n" + "Új levél tartalma:\n" + email.body_text;
		gs.log("incidens: " + existingIncident.getValue('number')),
        existingIncident["work_notes"].setJournalEntry(journal);
        existingIncident.update();
		if(hook.sendWebhook(existingIncident.getValue('sys_id'))){
			gs.info("Webhook sent to online after update on existing ticket");
		}
		if(sys_email.hasAttachments()){
			var attachmentGR = new GlideRecord('sys_attachment');
			var newAttachmentGR;
			attachmentGR.addQuery('table_name', 'sys_email');
			attachmentGR.addQuery('table_sys_id', sys_email.sys_id); // Az e-mail sys_id-je alapján keresünk
			attachmentGR.query();
			
			while (attachmentGR.next()) {
			newAttachmentGR = new GlideRecord('sys_attachment');
			newAttachmentGR.initialize();
			newAttachmentGR.table_name = 'incident';
			newAttachmentGR.table_sys_id = existingIncident.sys_id.toString();
			newAttachmentGR.file_name = attachmentGR.file_name;
			newAttachmentGR.content_type = attachmentGR.content_type;
			newAttachmentGR.file_size = attachmentGR.file_size;
			newAttachmentGR.file_stream = attachmentGR.file_stream;
			newAttachmentGR.insert();
          }
		}
      } else {
       		if(hook.sendWebhook(current.getValue('sys_id'))){
			gs.info("Webhook sent to online after new ticket : sys_id : " + current.getValue('sys_id') );
		}
		current.caller_id = '2fe0d724879f6d50d111646e8bbb35d8'/*'1b7b90f08705f550d111646e8bbb3536'*/;
		current.company = '9bf66da4db3098109521ae3bd39619b0';
		current.u_project_code= '09179c2bc33421d0d4424f45df013155';
		current.u_product = '5a020c43873fe150d111646e8bbb35f3';
		current.category = 'incident';
		current.u_report_date = gs.nowDateTime();
		current.contact_type = 'email';
		current.assignment_group = 'bc418a8adbdbc810d55a6a4cd396193c';
		current.description = email.body_text;
		current.state = 2;
        current.u_external_ticket_id = number;
		current.watch_list = email.from_sys_id;
        var bracket = rsubject.lastIndexOf(")");
        if (bracket >= 0) {
          var shortDesc = rsubject.substring(bracket + 1).trim();
          current.short_description = shortDesc;
        }
        
        var body1 = email.body_text;
        var versionIndex = body1.indexOf("Alkalmazás verziósz.:");
        if (versionIndex >= 0) {
          var versionStart = versionIndex + "Alkalmazás verziósz.:".length;
          var versionEnd = body1.indexOf("\n", versionStart);
          if (versionEnd >= 0) {
            var versionNumber = body1.substring(versionStart, versionEnd).trim();
            current.u_affected_version = versionNumber;
          }
        }
        
        var prio = email.body_text;
        var prio1 = prio.indexOf('Prioritás:');
        if (prio1 >= 0){
          var prioStart = prio1 + 'Prioritás:'.length;
          var prioEnd = body1.indexOf('\n', prioStart);
          if (prioEnd >= 0){
            var priority = body1.substring(prioStart, prioEnd).trim();
            
            switch (priority) {
              case "Blokkoló":
                current.impact = "1";
                current.urgency = "1";
                break;
              case "Sürgős":
                current.impact = "2";
                current.urgency = "1";
                break;
              case "Súlyos":
                current.impact = "2";
                current.urgency = "2";
                break;
              case "Közepes":
                current.impact = "3";
                current.urgency = "2";
                break;
              case "Kicsi":
                current.impact = "3";
                current.urgency = "3";
                break;
            }  
          }
        }
		current.update();
      }
    }
  }

 

When a user submits an email, the ticket is created successfully. However, if the same user submits the same email (which may have a different email body or attachments), it is expected to attach that email to the existing incident, as per the customer's requirements.

If I do this as an admin user (for testing, for example), it works fine, but when a regular user sends the same email (who has no permissions), a new incident is created.

I tried putting the code in a script include and calling it from the inbound action (because it might be the case that the inbound action runs in the user's context), but it still doesn't update the ticket; instead, it creates a new record.

I repeat myself: the script is definitely correct because if I set myself as the sender in the 'from' field of the inbound action, the update works fine.

Can you provide advice on how to make the system execute the script even for non-privileged email senders?

Thank you in advance for your assistance!

1 ACCEPTED SOLUTION

Szilard
Tera Guru

Hello everyone!

The error wasn't an error; there's a rule in the system (which I totally forgot about) that doesn't allow queries on the tables for certain designated companies unless the user has a specific (custom) role.

By assigning this role to the user, the script will run successfully.

 

Cheers!

View solution in original post

3 REPLIES 3

Saurav11
Kilo Patron
Kilo Patron

Hello,

 

Do the non-privileged email senders have write access. Please check that once if they have access to update existing records maybe they don't so the script creates a new record instead.

If there are write permissions, why is there no permission for the update? This is not clear to me 😞

I've also given the user an ITIL role, but it still doesn't work. What else could I do?

Szilard
Tera Guru

Hello everyone!

The error wasn't an error; there's a rule in the system (which I totally forgot about) that doesn't allow queries on the tables for certain designated companies unless the user has a specific (custom) role.

By assigning this role to the user, the script will run successfully.

 

Cheers!