+ Start a Discussion
sp13sp13 

how to update field using trigger

these are my objects:
       Object                              Field
Reservation__c          Seat__c(lookup)
     Seat__c                Taken__c(checkbox)
i need to update Taken__c whenever a new record of Reservation__c is created or updated.
 
i tried this code but didn't work:
trigger updateSeatTaken on Reservation__c (before insert, before update, after insert, after update) {
    for (Reservation__c r : trigger.new) {
           Seat__c takenSeat = [select name, taken__c from Seat__c where id =: r.id];     
                   r.Seat__r.Taken__c = true;
                   update r;
    }
}

an error showseverytime i create or update a Reservation__c record:
Apex trigger updateSeatTaken caused an unexpected exception, contact your administrator: updateSeatTaken: execution of BeforeInsert caused by: System.QueryException: List has no rows for assignment to SObject: Trigger.updateSeatTaken: line 3, column 1

any help?
Best Answer chosen by sp13
Pavel.SlepenkovPavel.Slepenkov
you have a several seriously mistakes into your trigger.

1) SOQL inside the for loop. You need to group and prepare your data before using it insede the for loop, because you should bulkify your code. You can read this my answer on stackExchange  (http://salesforce.stackexchange.com/questions/21600/help-please-system-exception-too-many-soql-queries-101/21601#21601)

2) you're trying to update record which is under update operation and this will be a cause of recursive trigger invocation. In your context you need to update Seat__c records which should be queried and stored in a map before processing it inside the loop, but your trying to update Reservation__c.


The main trouble
3) you have a trouble into WHERE condition. The id of Seat__c record can't be the same as the id of Reservation__c record. So you need go throught the Reservation__c records and collect the Lookup field id after the you can query all needed records


trigger updateSeatTaken on Reservation__c (before insert, before update, after insert, after update) {
    List<Id> seatIdsForUpdate = new List<Id>();
    for (Reservation__c r : trigger.new) {
        seatIdsForUpdate.add(r.Seat__c);
    }

    List<Seat__c> seatsForUpdate = [SELECT taken__c
                                    FROM Seat__c
                                    WHERE Id IN: seatIdsForUpdate];

    for (Seat__c seat: seatsForUpdate) {
        seat.Taken__c = true;
    }
    update seatsForUpdate;

}







All Answers

AmulAmul
trigger updateSeatTaken on Reservation__c (after insert, after update) {

list<id> lstAllSeat=new list<id>();

    for (Reservation__c r : trigger.new) {
       if(r.Seat__c !=null)
    {
    lstAllSeat.add(r.Seat__c);
    }
          
    }
list<Seat__c> lstupdateSeat;
if(lstAllSeat.size()>0){
    Seat__c[] takenSeat = [select name, taken__c from Seat__c where id in: lstAllSeat];
    for(Seat__c seatobj:takenSeat){
     seatobj.taken__c=true;
     lstupdateSeat.add(seatobj);
       }

      if(lstupdateSeat.size()>0){
   update lstupdateSeat;
}  
   
                  
}



}
sp13sp13
hi @Amul
your code shows this error: Apex trigger updateSeatTaken caused an unexpected exception, contact your administrator: updateSeatTaken: execution of AfterInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.updateSeatTaken: line 17, column 1
Pavel.SlepenkovPavel.Slepenkov
you have a several seriously mistakes into your trigger.

1) SOQL inside the for loop. You need to group and prepare your data before using it insede the for loop, because you should bulkify your code. You can read this my answer on stackExchange  (http://salesforce.stackexchange.com/questions/21600/help-please-system-exception-too-many-soql-queries-101/21601#21601)

2) you're trying to update record which is under update operation and this will be a cause of recursive trigger invocation. In your context you need to update Seat__c records which should be queried and stored in a map before processing it inside the loop, but your trying to update Reservation__c.


The main trouble
3) you have a trouble into WHERE condition. The id of Seat__c record can't be the same as the id of Reservation__c record. So you need go throught the Reservation__c records and collect the Lookup field id after the you can query all needed records


trigger updateSeatTaken on Reservation__c (before insert, before update, after insert, after update) {
    List<Id> seatIdsForUpdate = new List<Id>();
    for (Reservation__c r : trigger.new) {
        seatIdsForUpdate.add(r.Seat__c);
    }

    List<Seat__c> seatsForUpdate = [SELECT taken__c
                                    FROM Seat__c
                                    WHERE Id IN: seatIdsForUpdate];

    for (Seat__c seat: seatsForUpdate) {
        seat.Taken__c = true;
    }
    update seatsForUpdate;

}







This was selected as the best answer
sp13sp13
hi @Pavel.Slepenkov

your code works when the lookup field Seat__c has no filter criteria. do you know how can i run your code when i create a filter criteria for the Seat__c lookup field? thanks :)
Pavel.SlepenkovPavel.Slepenkov
@sp13
What is the trouble with the filter criteria? Can you provide more details?
sp13sp13
i have this lookup filter for the Seat__c field: Seat: Taken EQUALS False(it shows all the Seat records where Taken__c is unchecked). When i try to save a new reservation record it shows the error for this criteria "Value does not exist or does not match filter criteria." and doesn't save but when i delete this criteria the new reservation record saves and the taken__c field of the seat updates. do you have an idea what the problem here is?
Pavel.SlepenkovPavel.Slepenkov
try to update the condition
List<Seat__c> seatsForUpdate = [SELECT taken__c
                                    FROM Seat__c
                                    WHERE Id IN: seatIdsForUpdate
                                    AND taken__c = false
];
sp13sp13
hi @Pavel.Slepenkov! thanks for the help :) i removed 'before insert, before update' and it worked perfectly with the filter criteria :D