Check for Duplicates on sales order(Customer PO) Save
<p>This is the code i created to warn the user but 70% of sales orders are being created by Web services( SPS commerce, Celigo). How do terminate the web service process( i don't wanna let web service to save the sales order) if the PO is already exist in system.</p>

<p>--> This code shows the message for all the sales order. its not checking for duplicate po number.
code:</p>

<pre><code>function checkForDuplicates() {
    //Get the current form customer PO number to validate
    var customerpo = nlapiGetFieldValue('otherrefnum');
    var filters = new Array();
    //Create the search filter for that PO number
    filters[0] = new nlobjSearchFilter('otherrefnum', null, 'is', customerpo);
    var results = nlapiSearchRecord('salesorder', null, filters, null);
    if (results !== null) {
    //Is the result this record?
    if (results[0].getId() !== nlapiGetRecordId()) {
        //No, there is another record, ask user to confirm/cancel saving action
        var doWeSave = confirm('Sales Order with the PO number ' + customerpo + ' already exists.\n' +
            'Click OK to save a duplicate Sales Order.\n' +
            'Click Cancel to return to editing Sales Order.');
        if (doWeSave) {
            //User selected to save the record
            return true;
        }
        //User selected to cancel the save
        return false;
    }
}
//No duplicates
return true;
}
</code></pre>

<p>Hello i used below code but it still shows the message for all the sales orders.</p>

<pre><code>function checkForDuplicates() {
    //Get the current form customer PO number to validate
    var customerpo = nlapiGetFieldValue('otherrefnum');

    //Create the search filter for that PO number
    var filters = [
   new nlobjSearchFilter('otherrefnum', null, 'is', customerpo),
   new nlobjSearchFilter('mainline', null, 'is', 'T'), // filter to mainline makes results faster in many cases
   new nlobjSearchFilter('entity', null, 'is', nlapiGetFieldValue('entity'))];
        var results = nlapiSearchRecord('salesorder', null, filters, null);
    if (results !== null) {
        //Is the result this record?
        if (results[0].getId() !== nlapiGetRecordId()) {
        //No, there is another record, ask user to confirm/cancel saving action
        var doWeSave = confirm('Sales Order with the PO number ' + customerpo + ' already exists.\n' +
            'Click OK to save a duplicate Sales Order.\n' +
            'Click Cancel to return to editing Sales Order.');
        if (doWeSave) {
            //User selected to save the record
            return true;
        }
        //User selected to cancel the save
        return false;
    }
}
//No duplicates
return true;
</code></pre>

<p>}</p>