当按值输入销售记录中的客户字段时,应自动填充自定义字段(主要联系人和主要电子邮件)的 Suitescript

Suitescript that should auto populate the custom field(Primary Contact & Primary Email) when a customer field in sales record is entered by a value

应从销售订单中提及的特定客户处检索主要联系人和电子邮件信息。

我尝试了以下代码,但无法从客户记录中检索角色(主要联系人)。我在销售订单中输入客户字段时得到空值(空),也没有自动填充自定义字段。

function fieldChanged(context) 
    {

        var sales=context.currentRecord;

        if(context.fieldId=='entity')
        {
            var cusid=sales.getValue('entity');

            var cust=record.load({
                type: record.Type.CUSTOMER,
                id:cusid
            });

            var custid=cust.getText('entityid');

            log.debug(custid);


            var roleCount= cust.getLineCount({
                sublistId :'contactroles',

            });

            log.debug('count',roleCount);

            for(var i=0;i<roleCount;i++)
            {
                var roleName=cust.getSublistText({ sublistId : 'contactroles',fieldId : 'contactrole', line:i});

                log.debug('role',roleName);

                if(roleName=='Primary Contact')
                {
                    var emailinfo=cust.getSublistText({ sublistId : 'contactroles',fieldId : 'email', line:i});
                    sales.setValue('custbody_primary_email',emailinfo);
                }

            }

        }
    }

contactroles 子列表中没有 contactrole 字段。请参阅 Records Browser 以获得正确的字段 ID。

function fieldChanged(context) {
  try {
    var sales = context.currentRecord;
    if (context.fieldId == 'entity') //checking whether the cursor is in customer field or not
     {
      var cusid = sales.getValue('entity'); //retrieving the id of customer in salesOrder

      var cust = record.load({     //loading the customer record using above id
        type: record.Type.CUSTOMER,
        id: cusid
      });

      log.debug('customer id', cusid);

      var custid = cust.getText('entityid');

      log.debug(custid);


      var roleCount = cust.getLineCount({  //counting the lines in sublist (customer record)
        sublistId: 'contactroles'

      });

      log.debug('count', roleCount);

      for (var i = 0; i < roleCount; i++)   //traversing the list in sublist(contact list)
      {
        var roleId = cust.getSublistValue({
          sublistId: 'contactroles',
          fieldId: 'contact',
          line: i
        });

        log.debug('role id', roleId);

        var contactRecord = record.load({   //loading the contact record
          type: record.Type.CONTACT,
          id: roleId
        });

        var roleName = contactRecord.getValue({     //fetching the role in contact record
          fieldId: 'contactrole'
        });

        var phone1 = contactRecord.getText('phone');

        var emailinfo = contactRecord.getText('email');

        if (roleName == '-10')  //checking whether it is primary contact or not(primary contact id is -10)
         {

          sales.setValue('custbody_primary_email', emailinfo);//setting the custom fields )

          sales.setValue('custbody_primary_contact', phone1);
        }

      }

    }
  } catch (e) {
    log.error('Error Occurred in Updating Values' + e.message);
  }
}

您需要引用一个客户字段'contact'。此字段将为您提供主要联系人的内部 ID。从那里您可以构建逻辑以从主要联系人那里获取信息。