Wednesday, November 7, 2018

Sending multiple emails based on referenced records through a workflow using action scripts

Sending multiple emails based on referenced records through a workflow using action scripts

Some business processes would require the system to send email alerts to a number of records attached or referenced on a transaction. We can use the send email action on the workflow but a problem would arise if the number records being referenced varies. For example, if we are using Sales Team and we would like to send an email to each of the members assigned to an Opportunity. We won't be able to create a send email action for each since the number of members of a Sales Team can be a single member or more. In line with this, we can use Workflow Action script to determine the number of members and have it send an email to each.


The sample script provided below is for a workflow triggered for an Opportunity record. It would then send an email to all the members of the sales team of an opportunity record.

function actionScript(){

    var SalesTeamCount = nlapiGetLineItemCount('salesteam');
    var emailList = '';
    var separator = '';
    for (var i=1; i<=SalesTeamCount;i++){
        // If more than 1 email, create a comma separator.
        if (i>1){
            separator = ',';
        }

        var emp = nlapiGetLineItemValue('salesteam','employee',i);
        var employeeRec = nlapiLoadRecord('employee',emp);
        if (employeeRec.getFieldValue('email')){
            emailList = emailList+separator+employeeRec.getFieldValue('email');
        }        
    }
    var emailSubj = 'Sales Team Alert';
    var emailBody = 'Body of Message ';   
// The number 11 set as the 1st parameter is the internal id of any employee that you would like to appear as the sender / author of the email.
    nlapiSendEmail(11,emailList, emailSubj, emailBody);

}

 

Disclaimer: The above sample requires working knowledge in SuiteScript. Support to the sample code is not warranted.  The test cases conducted for the above sample is very limited and there is no guarantee that it will work in the future versions of NetSuite. 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