R Malak
R Malak

Reputation: 11

NetSUite primary role email

I would appreciate your help please. I am new to Netsuite Scripting. I need a workflow that workflow action script to get email address of the customer contact with role 'Primary Contact'and set this email address on custom field on the Customer record.

function getContactEmail() {
    var numItem = nlapiGetLineItemCount('contactroles');

    for (var i = 1; i <= numItem; i++)
    {

        var contact_rec = nlapiGetLineItemValue('contactroles', 'contact', i);
        // get all the contact name of the contacts  of the customer

        var contactEmailAddress = nlapiGetLineItemValue('contactroles', 'email', i);
        // get the e-mail address of the contact

        var contactRole = nlapiLookupField('contact', contact_rec, 'contactrole');
        // get the internal ID of the role of that particular contact from the customer

        if (contactRole == '-10') //check is the role is 'Primary Contact' or not.
        {
            nlapiSetFieldValue('custentity_email', contactEmailAddress); 
            //set the value of custom field with email address of the
            //Contact which has 'Primary Contact' role 
        }
    }

}

Upvotes: 0

Views: 865

Answers (2)

bknights
bknights

Reputation: 15367

Sorry. I'd forgetting how bizarre contacts are. The safest way to do this is with a search:

function setContactEmail(){
  var contacts = nlapiSearchRecord('customer', null,
        [
            new nlobjSearchFilter('internalid',null, 'is', nlapiGetRecordId()),
            new nlobjSearchFilter('contactrole', 'contact', 'is', '-10'),
            new nlobjSearchFilter('isinactive', 'contact', 'is', 'F'),
            new nlobjSearchFilter('company', 'contact', 'is', nlapiGetRecordId())
        ],[
          new nlobjSearchColumn('email', 'contact')
        ]);
  if(contacts) nlapiSetFieldValue('custentity_email', contacts[0].getValue('email', 'contact'));
}

Upvotes: 0

bknights
bknights

Reputation: 15367

You're close. This should do:

function getContactEmail() {
    var numItem = nlapiGetLineItemCount('contactroles');
    for (var i = 1; i <= numItem; i++) {
       if(nlapiGetLineItemValue('contactroles', 'role', i) != 14) continue; // the id for primary contact is 14 in my test account
       try{ //contact may be inactive
         var contactInfo = nlapiLookupField('contact', nlapiGetLineItemValue('contactroles', 'contact', i), ['email', 'company']);
         var roleEmail = nlapiGetLineItemValue('contactroles', 'email', i) || contactInfo.email;
         if(!roleEmail) continue;
         if(!contactInfo.company || contactInfo.company != nlapiGetRecordId()) continue; //maybe ok if not assigned to any company?

         nlapiSetFieldValue('custentity_email', roleEmail); //set the value of custom field with email address of the Contact which has 'Primary Contact' role 
         break;
       }catch(e){
         nlapiLogExecution('ERROR', "Looking up contact: "+ nlapiGetLineItemValue('contactroles', 'contact', i), e);
       }
    }
}    

Note you can reference the field ids at https://system.netsuite.com/help/helpcenter/en_US/srbrowser/Browser2015_1/script/record/customer.html

Upvotes: 2

Related Questions