You need to sign in to do that
Don't have an account?
sp13
how to update field using trigger
these are my objects:
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?
Object Field
Reservation__c Seat__c(lookup)
Seat__c Taken__c(checkbox)
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;
}
}
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?
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
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;
}
}
}
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
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;
}
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 :)
What is the trouble with the filter criteria? Can you provide more details?
List<Seat__c> seatsForUpdate = [SELECT taken__c
FROM Seat__c
WHERE Id IN: seatIdsForUpdate
AND taken__c = false
];