How to prevent duplicate value errors to be displayed?

  • 1
  • Question
  • Updated 3 years ago
  • Answered
We have a unique field to prevent duplicate entries from being inserted. We delegated this to Salesforce to minimize the number of server calls.
The problem is that even though we anticipate that exception can occur, we are unable to find a way to prevent the duplicate values errors from being shown.

Please advice.
Photo of Rodrigo

Rodrigo

  • 116 Points 100 badge 2x thumb

Posted 3 years ago

  • 1
Photo of Irvin Waldman

Irvin Waldman, Champion

  • 9,006 Points 5k badge 2x thumb
Hi Rodrigo,

I had a chat with Pat Vachon about this issue and he suggested CSS could help here.  

Following Pat's train of thought, messages are displayed in a .nx-message DOM node and added to a .nx-messages DOM container.

  So you could hide the container until the save returns, inspect the messages & remove the dup value messages, then unhide the container.  Seems sort of hackish but could work if there it is no better way.

Check out this tutorial for more details
http://help.skuidify.com/m/11720/l/205332-skuid-ui-editor

Regards,
Irvin
(Edited)
Photo of Moshe Karmel

Moshe Karmel, Champion

  • 8,646 Points 5k badge 2x thumb
If you want to get a little crazy, you can try something like this. I ran into a very similar issue and here's my hackish approach. I'm assuming you have a unique id based on 2 or more fields concatenated together. Let's say "opportunityId + accountId" is the key. In that case you can query salesforce before the save, and check if a record with that unique key already exists, based on the values the user has entered. This may seem cumbersome but actually runs pretty fast. You can use the snippet below, just replace all of the API names with your field names.

var params = arguments[0],	$ = skuid.$;
$.blockUI({
    message:'Assigning...',
    timeout: 1500
});
//variable declarations to get at model, row and fields
var MODEL_NAME = 'Your Model Name';
var model = skuid.$M(MODEL_NAME);
var row = model.getFirstRow();
var field1 = row.Your_Field__c;
var field2 = row.Your_Other_Field__c
//setup the unique string manually
var str = field1.toString() + field2.toString();
//check if these combinations exist
var qryStr = "Select Id, Unique_Id__c From You_Object__c Where Unique_Id__c = '" + str + "'";
var result;
//use the AJAX API to query salesforce and see if this combination already exists before the save
$.when(result = sforce.connection.query(qryStr)).done(function(){
    var recs = result.records;
    if(recs){//we got something back from the query! so let's not save...
        alert('The record you are trying to add already exists!');
        return;
    }
    else{//just save and everything is groovy :)
        model.save();
    }
});
Photo of Irvin Waldman

Irvin Waldman, Champion

  • 9,006 Points 5k badge 2x thumb
Much better that JavaScript/CSS monkey tricks.  Nice one Moshe.