Fetch requested for from user entered in CC when RITM is created from Inbound action

Sarabjeet1
Tera Contributor

Hi,

I want to pop up requested for on RITM field with the user entered in CC (there will always be one). The tickets will be created through inbound action. Here is the script i am using not sure what exactly i am doing wrong. I checked with logs, email id is coming fine and user is also present in system but requested for is going blank. please have a look and help me any possible way.

    function createRequest() {
        var body = email.body_text.toString();
        var strEmailContent = email.content_type + "\n" + email.headers + "\n\n\n\n" + body;
        var sa = new GlideSysAttachment();
        var short_description = email.subject.toString();
        var message = "received from: testing wp " + email.origemail + "\n\n" + body;
        var cat_item = '6325bdd7db0677003286d18c6896196d'; // my catalog item sys_id
        var requested_for = email.copied;
        var cart = new Cart();
        var item = cart.addItem('6325bdd7db0677003286d18c6896196d');
        cart.setVariable(item, 'description', message);
        cart.setVariable(item, 'short_description', short_description);
        cart.setVariable(item, 'sc_req_item.request.requested_for', requested_for);
        cart.setVariable(item, 'requested_for', requested_for);
        var req = cart.placeOrder();
        var rc = req.sys_id;

        // update the RITM to have a short description from the email
        var rec = new GlideRecord("sc_req_item");
        rec.addQuery("request", rc);
        rec.query();
        // check for a record
        if (rec.next()) {
            var email_log = new GlideRecord('sys_email');
            email_log.query('uid', email.uid);
            email_log.orderByDesc('sys_created_on');
            email_log.query();
            while (email_log.next()) {
                var email_sys = email_log.sys_id;
                //gs.log(email_sys +  'sarab testing');
                GlideSysAttachment.copy('sys_email', email_log.sys_id, 'sc_req_item', rec.sys_id); }

            // get the RITM, should be the only item with this parent
            rec.short_description = short_description;
            rec.description = message;
            rec.contact_type = "email";
            sa.write(rec, email.subject + ".eml", " application/octet-stream ", strEmailContent);
            rec.update();
        }

        var test = new GlideRecord('sc_request');
        test.addQuery("sys_id", rc);
        test.query();
        while (test.next()) {
            test.short_description = short_description;
            test.update();
        }
4 REPLIES 4

Mark Manders
Mega Patron

Assuming it will always be only 1 email address in cc, this won't be that hard.

The issue is that you are setting a reference field ('requested_for') to a string value (email address in the copied field). That won't work. You need to query the user table to find the user with that email address and fill the user's sys_id.
var requested_for = '';

var copied = email.copied;

var reqFor = new GlideRecord('sys_user')

reqFor.addQuery('email',copied);

reqFor.query();

if(reqFor.next()){

requested for = reqFor.getUniqueValue;

}


Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark

Hi Mark,

Thanks for the response. I tried the way you mentioned but requested for is still coming blank on RITM.

I just noticed a typo in my script, so if you copied it literally, make it 'requested_for = reqFor.getUniqueValue();'

 

Otherwise: add logs to your script to see what it is your are getting. If it is the sys_id of the user, validate if the field is correct and also check where you are really setting the value on the form. I see you are setting rec.short_description and other fields, but not requested for.


Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark

Sarabjeet1
Tera Contributor

Hi @Mark Manders 
I already corrected the typo before using code. The log i got is bit weird
native code, arity=0] 
Please help me if you have any idea.