Friday, July 5, 2019

Assigning cases automatically sent to the Inbound Email Address via SuiteScript

When sending emails to the NetSuite Address under Setup > Support > Support Preferences > Inbound Email, a case is automatically created in NetSuite.

Some companies has a different approach in creating cases. In some instances, companies has their support representatives send an email to their customer and CC's the inbound email address. Using SuiteScript, the created case can be attached to the customer using the recipient's email address of the inbound message and have it assigned to the support representative who sent the email.

One can utilize a User Event Script triggered on an After Submit function to achieve this goal.

Below is a sample code:

function afterSubmit(type)
{
    //only for creation of a case
    if (type == 'create')
    {
        //get the internal id of the created case
        var caseId = nlapiGetRecordId();
  
        //results column in performed searches below
        var column = new Array();
        column[0] = new nlobjSearchColumn('internalid');
  
        //search for the inbound email message which is connected to the created case
        var message = nlapiSearchRecord('message', null, [new nlobjSearchFilter('internalid', 'case', 'anyof', caseId)], column);
        //get the internal id of the inbound email message
        var messageId = message[0].getValue(column[0]);
  
        //get the sender's email address of the inbound email message
        var authorEmail = nlapiLookupField('message', messageId, 'authoremail');
  
        //get the recipient's email address of the inbound email message
        var recipientEmail = nlapiLookupField('message', messageId, 'recipientemail');
  
        //search for the employee which has the same sender's email address used in the inbound email message
        var employee = nlapiSearchRecord('employee', null, [new nlobjSearchFilter('email', null, 'is', authorEmail)], column);
        if (employee)
        { 
            //assuming that there is a unique email address for each employee
            var employeeId = employee[0].getValue(column[0]);
            //set the value of the 'Assigned To' field on the case record
            nlapiSubmitField('supportcase', caseId, 'assigned', employeeId);
        }
  
        //search for the customer which has the same recipient's email address used in the inbound email message
        var customer = nlapiSearchRecord('customer', null, [new nlobjSearchFilter('email', null, 'is', recipientEmail)], column);
        if (customer)
        { 
            //assuming that there is a unique email address for each customer
            var customerId = customer[0].getValue(column[0]);
            //set the value of the 'Company' and 'Email' field on the case record
            nlapiSubmitField('supportcase', caseId, ['company', 'email'], [customerId, recipientEmail]);
        }
    }
}

DISCLAIMER: The sample code described herein is provided on an "as is" basis, without warranty of any kind, to the fullest extent permitted by law. Netsuite Inc. does not warrant or guarantee the individual success developers may have in implementing the sample code on their development platforms or in using their own Web server configurations.

Netsuite Inc. does not warrant, guarantee or make any representations regarding the use, results of use, accuracy, timeliness or completeness of any data or information relating to the sample code. Netsuite Inc. disclaims all warranties, express or implied, and in particular, disclaims all warranties of merchantability, fitness for a particular purpose, and warranties related to the code, or any service or software related thereto.

Netsuite Inc. shall not be liable for any direct, indirect or consequential damages or costs of any type arising out of any action taken by you or others related to the sample code.

No comments:

Post a Comment