2017-08-22 26 views
0

我的公司運行幾種不同類型的項目,並希望根據所選項目記錄中運行的項目類型以不同方式查看項目記錄。Suitescript 2.0爲不同類型的項目使用不同的表格

我有選擇標題爲「自定義窗體」(這是一個選擇字段)和我們的工作人員輸入項目類型「custentityjt_fie_pro_projecttype」(也是選擇字段)的字段。

我創建負荷用戶事件腳本之前下面來嘗試實現這一目標:

/** 
* @NApiVersion 2.x 
* @NScriptType UserEventScript 
* @NModuleScope SameAccount 
*/ 
define(["N/record"], function(r) { 
function beforeLoad(context) { 
    var currentRecord = context.newRecord; 
    var projectType = currentRecord.getValue({ 
     fieldId: "custentityjt_fie_pro_projecttype", 
    }); 

    currentRecord.setValue({ 
     fieldID: 'customform', 
     value: projectType 
     }) 

} 

return { 
    beforeLoad: beforeLoad, 
} 

}) 

當加載在編輯模式下的項目備案,自定義窗體選擇不會改變,並加載項目記錄時在查看模式下,我得到如下:

{"type":"error.SuiteScriptError","name":"UNEXPECTED_ERROR","message":null,"stack":["anonymous(N/recordService)","beforeLoad(/SuiteScripts/setForm.js:13)"],"cause":{"type":"internal error","code":"UNEXPECTED_ERROR","details":null,"userEvent":"beforeload","stackTrace":["anonymous(N/recordService)","beforeLoad(/SuiteScripts/setForm.js:13)"],"notifyOff":false},"id":"","notifyOff":false} 

我很新的NetSuite和編程一般,所以請溫柔:)

回答

0

您需要使用客戶端腳本以更改自定義窗體。最好的辦法是在兩個地方做一個pageInit()和一個fieldChanged()。另一個潛在的問題是嘗試將自定義窗體值設置爲在字段custentityjt_fie_pro_projecttype的getValue中檢索的值。從您的示例中的currentRecord.getValue()返回的值將是在那裏設置的項目類型的自定義列表值的內部標識(返回到您的自定義列表,您將看到列出的內部標識值)。這是一個問題,因爲在設置自定義表單字段的值時,您需要引用您想要使用的自定義表單的內部標識。如果所引用的項目類型的內部ID與自定義表單內部標識相匹配,那將是非常不尋常的。我的建議是在你的代碼中創建一個散列表來存儲引用(假設你的項目類型列表不經常改變)。這給一個嘗試(只是確保你在查找變量更新值那些被彌補。

/** 
*@NApiVersion 2.x 
*@NScriptType ClientScript 
*/ 
define([ 
    'N/record' 
], 
    function (
    nsRecord 
) { 
    // 
    // lookup table where the object property represents the internal IDs of the 
    // custom list values, and the value of each property represents the 
    // internal id's of the Custom Forms you wish to associate with each list value. 
    // 
    var lookup = { 
     1: 122, 
     2: 123, 
     3: 125, 
     4: 136 
    }; 

    function fieldChanged(context) { 
     var field = context.fieldId; 
     var rec = context.currentRecord; 
     var projId; 

     if (field === 'custentityjt_fie_pro_projecttype' && rec.getValue('custentityjt_fie_pro_projecttype')) { 
      projId = rec.getValue('custentityjt_fie_pro_projecttype'); 

      if (lookup[projId]) { 
       rec.setValue({ 
        fieldId: 'customform', 
        value: lookup[projId], 
        ignoreFieldChange: true, 
        fireSlavingSync: true 
       }); 
      } 
     } 
    } 

    function pageInit(context) { 
     var rec = context.currentRecord; 
     var mode = context.mode; 
     var projId; 
     var formId; 

     if (mode !== 'create') { 
      formId = rec.getValue('customform'); 
      projId = rec.getValue('custentityjt_fie_pro_projecttype'); 

      if (lookup[projId] && lookup[projId] !== formId) { 
       rec.setValue({ 
        fieldId: 'customform', 
        value: lookup[projId], 
        ignoreFieldChange: true, 
        fireSlavingSync: true 
       }); 
      } 
     } 
    } 

    return { 
     fieldChanged: fieldChanged, 
     pageInit: pageInit 
    }; 
}); 
+0

非常感謝您的幫助。我是說得很對,這隻會在編輯模式下工作嗎?是在視圖模式下查看項目時有什麼辦法讓它工作? – jtux

+0

我還應該提到,在編輯模式下,自定義窗體選擇的確會改變,但實際的窗體本身不會改變。將不得不點擊選擇,而不是隻是改變字段的值,以便它實際執行更改。這可以通過在更改後添加頁面重新加載來解決嗎? – jtux

+0

@jtux您是正確的,這隻會在編輯模式,我也相信它只能作爲客戶端腳本,而不能在加載之前用戶事件腳本。 –

相關問題