You need to sign in to do that
Don't have an account?
Thomas Miller
Trigger Involving Traversing from Child To Parent's Field
Introduction
Hi everyone and thanks for stopping by! I'd like someone with a bit more knowledge and experience to do their best to asist me here if possible, but I also would like to state I do not want to be spoon fed. Pointing me in the right direction would be the general idea, and maybe a line or 2 of code on specifified sections. As the entire problem will end up being under 10 lines, I would like to be able to solve parts I can once key issues are addressed. Thank you all in advance for your time and contributions!
Problem Description
Write an Apex Trigger so each time a new Review record, API Review__c, is created, the related Media, API Media__c, has its field, API Reviews_Completed__c, incremented by 1.
Other Relevant Information & Thoughts
Code (gives index out of bounds error - System.QueryException: List has no rows for assignment to SObject - on SOQL query below)
As far as the error goes, I figure it's because the query gives the id field in addition to what I'm asking for, but if a list is required, please explain how I then go about taking that object out of the list in order to access the object's Reviews_Completed__c API to increment it.
Best,
Tom
Hi everyone and thanks for stopping by! I'd like someone with a bit more knowledge and experience to do their best to asist me here if possible, but I also would like to state I do not want to be spoon fed. Pointing me in the right direction would be the general idea, and maybe a line or 2 of code on specifified sections. As the entire problem will end up being under 10 lines, I would like to be able to solve parts I can once key issues are addressed. Thank you all in advance for your time and contributions!
Problem Description
Write an Apex Trigger so each time a new Review record, API Review__c, is created, the related Media, API Media__c, has its field, API Reviews_Completed__c, incremented by 1.
Other Relevant Information & Thoughts
- the parent record is API Media__c
- the child record is API Review__c
- there is a lookup relationship going from child-Review to parent-Media.
- Media object has a default 0, number field, API Reviews_Completed__c
- Ask for any other information if needed!
Code (gives index out of bounds error - System.QueryException: List has no rows for assignment to SObject - on SOQL query below)
As far as the error goes, I figure it's because the query gives the id field in addition to what I'm asking for, but if a list is required, please explain how I then go about taking that object out of the list in order to access the object's Reviews_Completed__c API to increment it.
trigger testTrig on Review__c (before insert) { // Following should also check every Review__c object r, in Trigger.New, and // if the API Review_Submission_Date__c API is empty, put the current date - this part works // This portion wasn't mentioned above but it's in my code as part of the exercise I'm working on. //To hold Media sObj rec ids List<Id> recId = new List<Id>(); // Loop to both add all Media ids to a list as well as change a blank submission date to today's date. for (Review__c r : Trigger.New) { recId.add(r.Media__r.id); if (r.Review_Submission_Date__c == null) r.Review_Submission_Date__c = Date.today(); } // Now that all Media ids are listed, query for 1 record such that the query result is // of Media type and has an id that is also part of Trigger.New, meaning in list recId // The only needed field is Reviews_Completed__c, which is going to be incremented by 1 // for each (API) Review__c record creation. Media__c m = [SELECT id, Reviews_Completed__c FROM Media__c WHERE id IN :recId LIMIT 1]; m.Reviews_Completed__c++; insert m; }
Best,
Tom
trigger testTrig on Review__c (before insert) {
/*
* The objective of this Trigger is to modify a Review record just
* prior to insert and 1) change a blank date to today's date and
* 2) update the parent Media record's Reviews_Completed field
* to 1 higher than its current value.
*/
//Create list of type Id to hold ids of Review obj parent record
List<Id> id = new List<Id>();
//Iterate through all Review obj within trigger
for (Review__c r : Trigger.New) {
//Add the Media__c field, the id of the parent, of each Review__c in trigger to list
id.add(r.Media__c);
// Assign today's date to the Review__c obj in the trigger
// If the Review Submission Date field is empty, assign today's date
if (r.Review_Submission_Date__c == null)
r.Review_Submission_Date__c = Date.today();
}
//Query for the desired Media records where the id of the
//record found matches the id of the records in the trigger, and our list
List<Media__c> m = [SELECT Reviews_Completed__c FROM Media__c WHERE id = :id];
//Increment field of the only Media obj in our list, thus index 0, and update the record
m[0].Reviews_Completed__c++;
update m;
}
:)
Best,
Tom
All Answers
Why wouldn't you do after Review__c insert so that you can get hold of Media__c Ids? I think the following code should work. Please select as the best answer if this helped.
trigger testTrig on Review__c (before insert) {
/*
* The objective of this Trigger is to modify a Review record just
* prior to insert and 1) change a blank date to today's date and
* 2) update the parent Media record's Reviews_Completed field
* to 1 higher than its current value.
*/
//Create list of type Id to hold ids of Review obj parent record
List<Id> id = new List<Id>();
//Iterate through all Review obj within trigger
for (Review__c r : Trigger.New) {
//Add the Media__c field, the id of the parent, of each Review__c in trigger to list
id.add(r.Media__c);
// Assign today's date to the Review__c obj in the trigger
// If the Review Submission Date field is empty, assign today's date
if (r.Review_Submission_Date__c == null)
r.Review_Submission_Date__c = Date.today();
}
//Query for the desired Media records where the id of the
//record found matches the id of the records in the trigger, and our list
List<Media__c> m = [SELECT Reviews_Completed__c FROM Media__c WHERE id = :id];
//Increment field of the only Media obj in our list, thus index 0, and update the record
m[0].Reviews_Completed__c++;
update m;
}
:)
Best,
Tom