我有一个userevent脚本来更改PO记录中的Field in Contract记录。脚本运行正常。但每当我编辑合同记录并尝试提交它时:它会抛出错误"自您开始编辑以来,另一位用户已更新此记录。请关闭记录并再次打开以进行更改"。
我可以知道这背后的原因吗?
/*---------------------------------------------------------------------------------------------------------------
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
}
}
}
答案 0 :(得分:1)
用户事件脚本在合同记录保存到数据库时执行。同时,您从数据库加载记录的第二个副本,并尝试提交副本。这会导致您看到错误。
您只需使用nlapiSetFieldValue
在合同上设置相应字段即可解决此问题。
我可能还建议您通过JavaScript Guide over at MDN更熟悉JavaScript。特别是,请查看Boolean description,以便了解JavaScript如何评估布尔表达式。这将有助于您大大减少您在此处编写的代码量,因为您的许多条件都是不必要的。
答案 1 :(得分:0)
你有什么用途?它正在发生,具体取决于您使用的用户事件和API的类型。查看代码,您正在尝试加载已在数据库中更新的合同记录。因此,您可以在下面考虑解决您的问题。希望能帮助到你。
如果是提交之前,则无需加载部署脚本的记录。
只需使用nlapiGet *和nlapiSet *来获取和设置值。您也不需要使用nlapiSubmitRecord来反映更改。在提交之前,它会在将记录保存到数据库之前执行。因此,您的更改仍将得到反映。
如果是在提交之后,它将在记录保存到数据库后执行,因此您可以根据需要使用以下API。实际上,这是确保解决方案的最佳实践。
答案 2 :(得分:0)
作为用户事件脚本代码,考虑到性能,您展示的代码非常不好。
以下是您可以合并的示例
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');