How to update a record in inbound email actions?

peterraeves
Mega Guru

We are creating an event system through email.

The email looks like this:

- Subject: <text>_SerialNumber_<text>

- Body: <Text that will trigger condition>

The inbound action will trigger on the body, and get the serial number from the subject. So far so good. Though we only want to have 1 incident open per CI with the given serial number. So if a new email comes in with a serial number, that has already an open incident, it should be updated. How should the inbound action look like?

First we tried:

var incident = new GlideRecord('incident');

incident.addQuery('cmdb_ci.serial_number', serial);

incident.addQuery('active', true);

incident.addQuery('contact_type', 'Automated Event');

incident.query();

if (incident.next()) {

  incident.work_notes = email.body_text;

  incident.update();

} else {

  current.work_notes = email.body_text;

  current.insert();

}

With this, it worked, though the attachments were not added, because we did not update the current object. So we tried:

var incident = new GlideRecord('incident');

incident.addQuery('cmdb_ci.serial_number', serial);

incident.addQuery('active', true);

incident.addQuery('contact_type', 'Automated Event');

incident.query();

if (incident.next()) {

  current.get(incident.getUniqueValue());

  current.work_notes = email.body_text;

  current.update();

} else {

  current.work_notes = email.body_text;

  current.insert();

}

Now, the Target is set correctly and the attachments are transferred from the email to the incident, but the work notes are not added and we see the error:

java.sql.SQLIntegrityConstraintViolationException: Duplicate entry '5a4376cc376307401517ab2943990e29' for key 'PRIMARY':

So, how should I create an inbound action with type 'New', that updates a record and is able to transfer attachments?

6 REPLIES 6

Deepak Ingale1
Mega Sage

Hi Peter,



current.update() in your case is trying to insert the new record with existing sys_id via expression current.get(incident.getUniqueValue())



Note : You can create record using current.update() as well. For database, insert is kind of update operation.



I think what you requires is to get the attachments from email record and copy those to incident record using GlideSysAttachment() api.



You can access the sys_id of incoming email via sys_email.sys_id. Then get the email record. find associated attachments if any, and copy those to incident record.


The sys_email variable


But then the Target field is not set on the email if I do not update the current object


If Target field is not set, what impact will it create? I think its nothing. Then we should be fine.


To me, it means we are not using the system how we should. So we might be missing out on some behind-the-scenes-functionality. But yeah, I suppose it would only impact the admins. We aren't that important anyway