Replicating the 'Email Received' functionality from the Incident table onto the Requested Item table

LRhodes
Tera Guru

Hi all,

So currently on the Incident table, if a new Incident is created from an email the initial stamp on the records journal is an 'Email received' function that contains the body of the email in an HTML format (see below).

LRhodes_0-1731420263943.png

 

This scenario we're dealing with, we're always going to receive a table such as in the email above. So I've tried to replicate this with an inbound action however it isn't having the impact we want as it's just placing a rich text formatting of the email in as an additional comment - so we're missing the structure of the table and it's just a single line with all the characters mashed together in one continuous string.

 

Here's the script of the inbound action I've put together.

 

(function runAction(/*GlideRecord*/ current, /*GlideRecord*/ event, /*EmailWrapper*/ email, /*ScopedEmailLogger*/ logger, /*EmailClassifier*/ classifier) {

    createRequest();

    function createRequest() {
        var cart = new Cart();
        
        // Add the catalog item to the cart using its sys_id
        var item = cart.addItem('e7c833ecfb34da50e53cff57aeefdc6e'); // Provisioning Request
        
        // Set variables: description and summary (short_description)
        cart.setVariable(item, 'summary', email.subject);
        
        // Place the order and get the request object (sc_request)
        var rc = cart.placeOrder();
        
        // Retrieve the corresponding sc_req_item
        var ritm = new GlideRecord('sc_req_item');
        ritm.addQuery('request', rc.sys_id); // Link sc_req_item to the request
        ritm.query();
        
        if (ritm.next()) {
            // Set opened_by and requested_for to the email sender
            var userSysID = getUserFromEmail(email.from);
            ritm.setValue('opened_by', userSysID);
            ritm.setValue('requested_for', userSysID);

			ritm.setValue('summary', current.variables.summary);
            
            // Set the description field
            ritm.setValue('description', email.body_text);
            
            // Add the email body as an additional comment
            ritm.comments = "Received from: " + email.origemail + "\n\n" + email.body_text;

            // Update the sc_req_item record
            ritm.update();
            
            // Attach any email attachments to the sc_req_item
            attachEmailAttachments(ritm.sys_id);
        }
    }

    // Function to get the sys_id of the user sending the email
    function getUserFromEmail(emailAddress) {
        var user = new GlideRecord('sys_user');
        user.addQuery('email', emailAddress);
        user.query();
        if (user.next()) {
            return user.sys_id; // Return the sys_id of the user
        }
        return gs.getUserID(); // Fallback to the system user if no match
    }

    // Function to attach email attachments to the requested item
    function attachEmailAttachments(ritmID) {
        var emailRec = new GlideRecord("sys_email");
        emailRec.addQuery("uid", email.uid);
        emailRec.orderByDesc("sys_created_on");
        emailRec.query();
        
        if (emailRec.next()) {
            // Copy attachments from the email to the newly created request item
            GlideSysAttachment.copy("sys_email", emailRec.sys_id, "sc_req_item", ritmID);
        }
    }

})(current, event, email, logger, classifier);

It creates the requested item fine, not issues there but it's taking the body of the email and placing it into the journal. I've noticed on Incident's it's stamped with Email received rather than Additional comments. Is there a way we can tap into this?

LRhodes_1-1731420504688.png

 

LRhodes_2-1731420521085.png

 

 

1 ACCEPTED SOLUTION

Kieran Anson
Kilo Patron

You can remove 'ritm.comments = "Received from: " + email.origemail + "\n\n" + email.body_text;' if you don't want to add a comment. To attach the email to the record, and show it in the activity feed,  you can add the following into your inbound action:

sys_email.target_table = 'sc_req_item';
sys_email.instance = 'sys_id_of_created_record'
event.state = 'stop_processing';

 

	//Create Request using the non-deprecated CartJS API
	var cart = new sn_sc.CartJS();
	var cartOrderDetails = cart.orderNow({
		'sysparm_id' : '',
		'sysparm_quantity' : '1',
		'variables' : {
			'summary' : email.getValue('subject')
		}
	});

	//Either we failed to create a request, or 2-step checkout is on.
	if(!cartOrderDetails.hasOwnProperty('request_id')){
		return;
	}

	var ritm = new GlideRecord('sc_req_item');
	ritm.setLimit(1);
	ritm.addQuery('request' , cartOrderDetails.request_id);
	ritm.query();
	if(ritm.next()){
		//do work here

		sys_email.target_table = ritm.getTableName();
		sys_email.instance     = ritm.getUniqueValue();
	}

 

You should also move away from using the Cart API and use CartJS. The former is deprecated  

View solution in original post

2 REPLIES 2

Kieran Anson
Kilo Patron

You can remove 'ritm.comments = "Received from: " + email.origemail + "\n\n" + email.body_text;' if you don't want to add a comment. To attach the email to the record, and show it in the activity feed,  you can add the following into your inbound action:

sys_email.target_table = 'sc_req_item';
sys_email.instance = 'sys_id_of_created_record'
event.state = 'stop_processing';

 

	//Create Request using the non-deprecated CartJS API
	var cart = new sn_sc.CartJS();
	var cartOrderDetails = cart.orderNow({
		'sysparm_id' : '',
		'sysparm_quantity' : '1',
		'variables' : {
			'summary' : email.getValue('subject')
		}
	});

	//Either we failed to create a request, or 2-step checkout is on.
	if(!cartOrderDetails.hasOwnProperty('request_id')){
		return;
	}

	var ritm = new GlideRecord('sc_req_item');
	ritm.setLimit(1);
	ritm.addQuery('request' , cartOrderDetails.request_id);
	ritm.query();
	if(ritm.next()){
		//do work here

		sys_email.target_table = ritm.getTableName();
		sys_email.instance     = ritm.getUniqueValue();
	}

 

You should also move away from using the Cart API and use CartJS. The former is deprecated  

Perfect Kieran, this has worked a treat. Also appreciate the information regarding the move away from CartJS - I've fed this back and will update the script accordingly.