mrSmith
mrSmith

Reputation: 3

Netsuite email template - how to get child values from custentity field?

guys! I'm writing an email template for the invoices. On the invoice form I have a "Project" (internalId: Job) field -> on the "Project" form I have a "custom entity" field with employee-type. I can get an employee's name with ${transaction.job.custentity5}. But I can't get access to related fields such as email, phone number and etc. The code ${transaction.job.custentity5.email} gives me nothing. The code ${transaction.job.custentity5.mobilephone} gives me a strange error like "field job.mobilephone not found" (netsuite hides custentity5 in this objects chain), but I see this field in employee's profile.

How do i get child values from custentity field?

Upvotes: 0

Views: 1092

Answers (2)

W.S.
W.S.

Reputation: 1505

Unfortunately, you can't go into such a deep level with the standard data provided.

You can however fetch the data with a search.lookupFields during a beforeLoad and set it as default value on a custom field of the form.

/**
 * @NApiVersion 2.x
 * @NScriptType UserEventScript
*/
define(['N/ui/serverWidget', 'N/search'], function(serverWidget, search) {

  function beforeLoad(context) {
    var invoice = context.newRecord;
    var form = context.form;
    var type = context.type;
    var UserEventType = context.UserEventType;

    // only execute during printing...
    if (type != UserEventType.PRINT) return

    var jobID = invoice.getValue({fieldId: 'job'});
    // return when no job/project is set on the invoice...
    if (!jobID) return

    var job = search.lookupFields({
      type: search.Type.JOB,
      id: jobID,
      columns: ["custentity5"]
    })

    // return when no employee is set on the project...
    if (!(job.custentity5 && job.custentity5[0] && job.custentity5[0].value)) return

    var employee = search.lookupFields({
      type: search.Type.JOB,
      id: job.custentity5[0].value,
      columns: ["email", "phone"]
    })

    var field = form.addField({
      id : 'custpage_custom_data_employee',
      label: 'Employee',
      type : serverWidget.FieldType.LONGTEXT
    });

    field.defaultValue = JSON.stringify(employee); 

  }

  return {
    beforeLoad: beforeLoad
  };

})

You can access the data within the template through:

<#if record.custpage_custom_data_employee?has_content>
<#assign employee = record.custpage_custom_data_employee?eval />
</#if>

Upvotes: 1

bknights
bknights

Reputation: 15402

Netsuite doesn't drill down that far for you. Generally you get one step away from the primary record so you see a name using ${transaction.job.custentity5} because you'd see a name if you opened the associated job record.

How you can show the details from custentity5 depends on a bunch of factors like who is sending this email? Is is special enough to have its own button? Is the email being sent batch etc.

Options:

  • A before load user event script can check whether the record is being printed or emailed the standard way. That script can load and populate custom fields on the main record so you may be able to reference ${transaction.custpage_ent5.mobilephone} where you'd added the job's custom entity to the transaction
  • fully script your email using n/Render. You'll likely need to script everything but you can look up and add arbitrary records and datastructures to your renderer. This would be triggered by a custom button or batched script.

Upvotes: 0

Related Questions