function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
WeylandYutanieWeylandYutanie 

Duplicate Value Error

Hi again, another error I am running into.  I have a collection of objects that I want to update a numeric paramer.  Error I get is:

 

UpdateOtherSpotEntries: execution of BeforeDelete
caused by: System.DmlException: Update failed. First exception on row 0 with id a03E0000001AS6HIAW; first error: DUPLICATE_VALUE, duplicate value found: LineNum__c duplicates value on record with id: a03E0000001AS8h: []
Trigger.UpdateOtherSpotEntries: line 17, column 5

 

Here is my code, I think I don't have a good understanding of what exactly the SELECT function does for me.  Doesn't it get me a List all objects with status = 'Pending' that I can loop through and modify?  Since I queried the LineNum__c parameter, I should be able to set to new values. 

 

Many thanks,

Weyland

 

trigger UpdateOtherSpotEntries on Place_in_Line__c (before delete) {

    LIST<Place_in_Line__c> AllPending = [SELECT LineNum__c FROM Place_in_Line__c WHERE Place_in_Line__c.Status__c = 'Pending'];

    if (AllPending.size() > 0)
    {
        for (Place_in_Line__c ToUpdate : AllPending )
        {
            if (ToUpdate.LineNum__c > 2)
                 ToUpdate.LineNum__c = ToUpdate.LineNum__c - 1;
        }
       
    }
    update AllPending;
}

 

Best Answer chosen by Admin (Salesforce Developers) 
Shashikant SharmaShashikant Sharma

I hope you have not made this LineNum__c field as Unique at field level. One more thing when you are udating I want to know

Once any Place_in_Line__c  is getting deleted , why are you updating all the Place_in_Line__c  records with Status pending. Shouldn't there be some common reference between the deleting item and pending items that you select.

 

Another thing It seems that you select quey will also give the item that you are deleting , and it will also go for update, If Yes the Is there any reason for updating a record which is deleting.

 

One last question : When do you face this error, when you runtest or from UI when you delete or from code when you delete/

All Answers

WeylandYutanieWeylandYutanie

Ah, it seems to have to do with the fact that I'm updating the same parameter.  If I change the IF statement to the following:

 

if (ToUpdate.LineNum__c > 2)
                 ToUpdate.Test_var__c = ToUpdate.LineNum__c - 1;

 Then it works just fine.  Very interesting...is it not allowed to update the same entry based on its current value?  Do I need to have hidden variables that can operate as a temporary variable?  That seems weird. 

~Weyland

Shashikant SharmaShashikant Sharma

I hope you have not made this LineNum__c field as Unique at field level. One more thing when you are udating I want to know

Once any Place_in_Line__c  is getting deleted , why are you updating all the Place_in_Line__c  records with Status pending. Shouldn't there be some common reference between the deleting item and pending items that you select.

 

Another thing It seems that you select quey will also give the item that you are deleting , and it will also go for update, If Yes the Is there any reason for updating a record which is deleting.

 

One last question : When do you face this error, when you runtest or from UI when you delete or from code when you delete/

This was selected as the best answer
WeylandYutanieWeylandYutanie

Haha, I did set the field to unique.  Oops!  So what I am building is a queuing system for objects like a linked-list.  When one is deleted, I would like to update values in the subsequent objects.  In this case, I am removing one object and updating everything afterwards so their index is updated appropriately.  Similarly, when one is inserted into a certain position, every other object is updated.

 

To answer your second question regarding passing the object that is meant to be deleted, yes, it's not the most efficient.  This is mainly because I'm inexperienced at coding in APEX haha =) Just want to get it working first and then go back over and optimize.

 

As for when I am seeing the errors, I test by saving the code in the Web interface, going over to the tabs and manually creating/updating/deleting objects to trigger and go through my code.  Yes, I should make unit_tests but haven't quite learned to do that yet ^_^  Soon!

 

Many thanks.

Shashikant SharmaShashikant Sharma

Hi ,

 

Could you please mark it as accepted solution as You ahve figured out the original issue in the post, it would be helpful for others. And please let me know if you face any issue in optimizing or writing test.