Tuesday, January 29, 2019

SuiteScript > Notifying Primary Contact and Decision Maker when an order has been placed.

If you are trying to notify the Vendor Primary Contact and Decision Maker when a purchase order has been placed the following script should be a User Event script and should be executed on After Record Submit. This script gets the information from the purchase order. It then loads up the vendor record and gets the email addresses of the Primary Contact and the Decision Maker. Once the user hits submit it sends the Purchase Order based on DEFAULT which uses the user/company preference for print output. This email gets sent to the Primary Contact and Decision Maker.


    
function sendEmailToVendor(){ 
        var record = nlapiLoadRecord('purchaseorder', nlapiGetRecordId());
        var rec = record.getFieldValue('entity');
        nlapiLogExecution('DEBUG', 'Vendor ID: ' + rec, rec);

        /* -10, -30 internalid's of the roles */
        var searchCriteria = new Array();
        searchCriteria[0] = -10;
        searchCriteria[1] = -30;

        var filters = new Array();
        filters[0] = new nlobjSearchFilter('contactrole', null, 'anyof', searchCriteria);
filters[1] = new nlobjSearchFilter('company', null, 'is', record.getFieldValue('entity'));

        var columns = new Array();
        columns[0] = new nlobjSearchColumn('contactrole');
        columns[1] = new nlobjSearchColumn('entityid');
        columns[2] = new nlobjSearchColumn('email');


        var file = nlapiPrintRecord('TRANSACTION', record.getId(), 'DEFAULT', null);
        var search = nlapiSearchRecord('contact', null, filters, columns);
        for (var i = 0; i < search.length; i++){
            var searchresult = search[i];
            var contactId = searchresult.getId();
            nlapiSendEmail(-5, contactId, 'test', 'Please see attached transaction', null, null, null, file);
        }
}

No comments:

Post a Comment