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

Simple update set of records

In Apex/Eclipse, how can I perform this simple update:

UPDATE bookings SET field1 = 'a', field2 = 'b', field3 = 'c' WHERE set_id = 999

So all I want to do is update all the records with the set_id that I specify. bookings would be my custom object.

//simple example - please review the docs for further syntax details

List<bookings__c> bookings = [select set_id__c, field1__c, field2__c, field__c from bookings__c where set_id__c = '999'];
for(bookings__c booking :bookings){
booking.field1__c = 'a';
booking.field2__c = 'b';
booking.field3__c = 'c';
update bookings;
That's how I tried it but it causes an error saying 'can't update self', this is because the record that is getting updated by the standard form gets selected, so I change the SOQL to this

select fields from bookings__c where set_id__c = '999' AND Id != :currentId

So it should not select the record that is currenrtly being updated, but it's producing unexpected errors. Will post more on that when I try again this morning.
I think I've found the problem. All this code is in the before update trigger. So when I loop through the selected data and call 'update bookings', it then fires this trigger again for each booking that it tries to update. So it should get stuck in a continuos loop. But it's erroring saying unable to update self, even when I tell it not to select self in the where clause.

Any ideas?