【问题标题】:Netsuite Userevent ScriptNetsuite 用户事件脚本
【发布时间】:2016-09-29 08:05:50
【问题描述】:

我有一个 userevent 脚本来从 PO 记录更改合同记录中的字段。脚本运行良好。但是每当我编辑合同记录并尝试提交时:它会抛出错误“自您开始编辑后,另一个用户已更新此记录。请关闭记录并再次打开以进行更改”。

我可以知道这背后的原因吗?

/*---------------------------------------------------------------------------------------------------------------

Description     :   Whenever the PO vendor is changed(due to Split vendor) that should replace the same in Contract page record automatically.
Script type         :   User Event Script
Script id           :   customscript452 
Version             :   1.0
Applied to          :   Contract
----------------------------------------------------------------------------------------------------------------*/

function srchfield()
    {
        var stRecordid = nlapiGetRecordId(); //returns the contract id
        if(stRecordid== undefined || stRecordid== null || stRecordid==' ')
            { 

            }
                else
                {   
                    var stRecordtype = nlapiGetRecordType(); //returns the contract record type = jobs

                    var stRecord = nlapiLoadRecord(nlapiGetRecordType(), stRecordid);
                    nlapiLogExecution('debug','Load Object',stRecord);

                    var stContractID = stRecord.getFieldValue('entityid'); //returns the value of the field contractid whose fieldid is = entityid
                    nlapiLogExecution('debug','stContractID',stContractID);

                    var stCompanyName = stRecord.getFieldValue('companyname'); //returns the value of the field company name whose fieldid is = companyname
                    nlapiLogExecution('debug','stCompanyName',stCompanyName);

                    var stConcatenate = stContractID+" : "+stCompanyName; //Concatenate the two Fields to get the result which needs to be found in PO

                    var arrFilters = new Array(); // This is Array Filters all the Purchase Order Record Search
                    arrFilters.push(new nlobjSearchFilter('type', null, 'anyof',
                        [
                            'PurchOrd'
                        ]));
                    arrFilters.push(new nlobjSearchFilter('mainline', null, 'is', 'T')); //This is to exclude line level results
                    arrFilters.push(new nlobjSearchFilter('custbodycontract', null, 'is', stRecordid)); //This is Filters in Contracts Search

                    var arrColumns = new Array();
                    arrColumns.push(new nlobjSearchColumn('entity')); //This is Search Column Field in Records
                    var arrSearchresults = nlapiSearchRecord('purchaseorder', null, arrFilters, arrColumns); //This is Filters in Search Result Purchase Order

                    if(arrSearchresults== undefined || arrSearchresults== null || arrSearchresults==' ')
                        { 

                        }
                            else
                            {   
                                var length = arrSearchresults.length;
                            }

                    if(length== undefined || length== null || length==' ')
                        { 

                        }
                            else
                                {
                                    for (var i = 0; arrSearchresults != null && i < arrSearchresults.length; i++)
                                        {
                                            var objResult = arrSearchresults[i];
                                            var stRecId = objResult.getId();
                                            var stRecType = objResult.getRecordType();
                                            var stCntrctName = objResult.getValue('entity'); //This is Value are Get Purchase Order Records and Field for Vendor = entity
                                        }
                                }
                     //var record = nlapiLoadRecord(nlapiGetRecordType(), stRecordid, stCntrctName);

                    if (stCntrctName =='custentityranking_vendor_name')
                        {

                        }
                            else
                                {
                                    var stChangeName = stRecord.setFieldValue('custentityranking_vendor_name', stCntrctName); //This is Value are the Set in Main Vendor Field = custentityranking_vendor_name

                                    nlapiSubmitRecord(stRecord, null, null); // Submit the Field Value in Record Type

                                }   
                }
    }

【问题讨论】:

    标签: netsuite suitescript


    【解决方案1】:

    用户事件脚本在合同记录被保存到数据库时执行。同时,您正在从数据库中加载记录的第二个副本并尝试提交该副本。这导致了您看到的错误。

    您只需使用nlapiSetFieldValue 在合同上设置适当的字段即可解决此问题。

    我还建议通过JavaScript Guide over at MDN 更熟悉 JavaScript。特别是,请查看Boolean description,以便了解 JavaScript 如何评估布尔表达式。这将帮助您大大减少您在此处编写的代码量,因为您的许多条件是不必要的。

    【讨论】:

      【解决方案2】:

      你有什么用户事件?它的发生取决于您使用的用户事件和 API 的类型。查看您的代码,您正在尝试加载已在数据库中更新的合同记录。因此,您可以考虑在下面解决您的问题。希望能帮助到你。

      如果是前提交,则不需要加载脚本部署的记录。
      只需使用 nlapiGet* 和 nlapiSet* 即可获取和设置值。您也不需要使用 nlapiSubmitRecord 来反映更改。在提交之前,它在记录被保存到数据库之前执行。因此,您的更改仍然会得到反映。
      那么如果是在提交之后,它会在记录保存到数据库后执行,因此您可以根据需要使用以下API。实际上,这是确保解决方案的最佳实践。

      • nlapiGetNewRecord - 仅当脚本只需要从标题和子列表中检索信息时才使用它。没有什么可设置的。
      • nlapiLookupField - 如果脚本只需要在标题处获取值而不需要从行中获取任何内容,则使用此字段。
      • nlapiSubmitField - 如果仅更改标题,则脚本不需要加载和提交记录。只需使用此 API。
      • nlapiLoadRecord 和 nlapiSubmitRecord - 如果脚本将在该行发生更改,则使用前者,然后使用后者 api 将其提交到数据库。

      【讨论】:

        【解决方案3】:

        作为一个用户事件脚本代码,你展示的代码从性能上来说是非常糟糕的。

        这是您可以合并的示例

        var stRecordid = nlapiGetRecordId(); //返回合约id

        // Every record has an internal id associated with it. No need to add condition explicitly to check if its null  
        var stRecordtype = nlapiGetRecordType();
        
        var fields = ['entityid','companyname'];
        var columns = nlapiLookupField(stRecordtype, stRecordid, fields);
        
        var stContractID = columns.entityid;
        var stCompanyName = columns.companyname;
        
        nlapiLogExecution('debug','stContractID/stCompanyName',stContractID+'/'+stCompanyName);
        
        var stConcatenate = stContractID+" : "+stCompanyName; //Concatenate the two Fields to get the result which needs to be found in PO
        //
        //your code of search
        //you can improve that code also by using nlapilook up
        
        nlapiSubmitField(stRecordtype, stRecordid, 'custentityranking_vendor_name', 'name to be updated');
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多