J. G.
J. G.

Reputation: 1832

Duplicate entry unique key.... my error catching failed

So I am updating something but I only want it to make a record for new records.

 for (var i=0; i<allquery.length;i++)
    {
    var oldrecord = allquery[i];
    var newrecord = app.models.xx.newRecord();
    newrecord.gPor = xxString;
    newrecord.UniqueNumber = oldrecord.Unique;
      newrecord.Name = oldrecord.dname;      
    try{app.saveRecords([newrecord]);//this is line 30            
    results.push(newrecord);}catch(e){Logger.log (e+ " "+oldrecord.dname);}     

    }//for loop

I'm frustrated because the sql error should be caught in the try catch (it is appearing for the saveRecords line... but instead it is killing the script. Thoughts? do I need to implement manual save mode? (which will require rewriting elsewhere.

The error is:

Exception: Malformed SQL. More information: Error with SQL statement: Duplicate entry '0270' for key 'UniqueNumber_unique'. at createApprovals (ApprovalBugScripts:30)

Upvotes: 0

Views: 48

Answers (1)

J. G.
J. G.

Reputation: 1832

Enclosing the entire creation part in a try catch worked:

 for (var i=0; i<allquery.length;i++)
    {
try{
    var oldrecord = allquery[i];
    var newrecord = app.models.xx.newRecord();
    newrecord.gPor = xxString;
    newrecord.UniqueNumber = oldrecord.Unique;
      newrecord.Name = oldrecord.dname;      
    app.saveRecords([newrecord]);//this is line 30            
    results.push(newrecord);}catch(e){Logger.log (e+ " "+oldrecord.dname);}     

    }//for loop

Upvotes: 1

Related Questions