NetSUite 主要角色电子邮件

NetSUite primary role email

非常感谢您的帮助。我是 Netsuite 脚本的新手。我需要一个工作流操作脚本来获取角色为“主要联系人”的客户联系人的电子邮件地址,并将此电子邮件地址设置在客户记录的自定义字段中。

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 
        }
    }

}

你很接近。应该这样做:

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);
       }
    }
}    

请注意,您可以在 https://system.netsuite.com/help/helpcenter/en_US/srbrowser/Browser2015_1/script/record/customer.html

处引用字段 ID

对不起。我会忘记接触是多么奇怪。最安全的方法是搜索:

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'));
}