Galdiator
Galdiator

Reputation: 117

Netsuite Userevent Script

I have a userevent script to change the Field in Contract record from PO record. The Script is running fine. But whenever I edit a contract record and try to submit it : It throws the error "Another user has updated this record since you began editing it. Please close the record and open it again to make your changes".

May I know the reason behind this ?

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

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

                                }   
                }
    }

Upvotes: 0

Views: 3913

Answers (3)

Chinmay Shepal
Chinmay Shepal

Reputation: 51

Being a user event script code, The code you showed is very not good considering performance.

Here is the sample you can merge

var stRecordid = nlapiGetRecordId(); //returns the contract 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');

Upvotes: 0

Leibnitz Jacquard
Leibnitz Jacquard

Reputation: 469

What userevent do you have? It is happening depending on what type of user event and API you are using. Looking at your code, you are trying to load contract record that is already updated at the database. So you might consider below to address your issue. Hope, it helps.

If it is a before submit, you don't need to load the record where the script is deployed.
Just use nlapiGet* and nlapiSet* to get and set values. You also don't need to use nlapiSubmitRecord to reflect the change. With before submit, it executes before the record is being saved to the database. So your changes will still be reflected.
Then if it is after submit, it will be executed after the record has been saved to the database, Thus you might use the following API depending on your needs. Actually, this is the best practice to make sure the solution .

  • nlapiGetNewRecord - only use this if the script only needs to retrieve info from header and sublists. And nothing to set.
  • nlapiLookupField - use this if the script only needs to get value/s at the header and nothing from the line.
  • nlapiSubmitField - the script don't need to load and submit record if the changes only on header. Just use this API.
  • nlapiLoadRecord and nlapiSubmitRecord- use the former if the script will have changes at the line and then use the latter api to commit it on the database.

Upvotes: 0

erictgrubaugh
erictgrubaugh

Reputation: 8847

The User Event script executes as the Contract record is being saved to the database. At the same time, you are loading a second copy of the record from the database and trying to submit the copy as well. This is causing the error you're seeing.

You fix this by just using nlapiSetFieldValue to set the appropriate field on the Contract.

I might also recommend getting more familiar with JavaScript by going through the JavaScript Guide over at MDN. In particular, take a look at the Boolean description so that you know how JavaScript evaluates Boolean expressions. This will help you greatly reduce the amount of code you've written here, as many of your conditionals are unnecessary.

Upvotes: 1

Related Questions