netsuitefreemarkerinvoiceemail-templates

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?


Solution

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