• Thomas Miller
  • NEWBIE
  • 10 Points
  • Member since 2014

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 3
    Questions
  • 3
    Replies
Hi all,

Quick question about how to utilize the data I'm getting back from my query. There seems to be some confusion on my side as to how to either make the type correct, because I must return a specific type, or maybe how to modify my query slightly so I am getting back the desired type. There is a lookup relationship between two objects, Review__c and Media__c, the latter being the parent. The query functions in its current state, but I am completely at a loss as to how I might access the data in the "Reviews__r" column, column 2, of said query.

SOQL Query
 
List<Media__c> Med = new List<Media__c>();

				Med = [SELECT id, (SELECT Review_Submission_Date__c, 
				Comments__c, Media__c, Member__c, Rating__c FROM Reviews__r) FROM Media__c  
				WHERE id =:recId LIMIT 50];

The above is my query, and it will return a single row. I need the data contined in row 1, column 2, but am unsure how to extract it from the above List<Media__c> and place into a List<Review__c> type to be returned by my getter. If any clarification is needed to help solve this, just say so. Any help is much appreciated!

Best,
Tom

 
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
  • 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!
There are two main points I'm unsure about, so some clarification there is really what I'm looking for, in addition to the only lines of code I want. First, the part where I use __r to, at least I think, grab the id of the parent-Media record. I'm doing that because I want to use it for my SOQL query later on, the id = :recId part, but I'm not 100% sure it's correctly grabbing the Media recId for later use. The second part I'm unsure about is how I'm meant to access the Reviews_Completed__c field. This goes into the idea of taking a list and breaking it down into single objects to access their fields, which I thought I had done before without any problem. I'm not sure if my SOQL query should be a list of <Media__c> type, or a single Media__c sObject variable. If i can just do LIMIT 1, since the only record that matters is the one I'm currently saving and about to allow insertation, then I would think I don't need a list, in which case I should be able to increment it by simply doing sObject.FieldToIncrement++; My code is below and includes the list based case since I've explained the other way I had imagined it may work. Neither are working. Again, any help is much appreciated!

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



 
Thought this challenge was pretty straight forward, but I'm not sure what to do based on the error message; here is the challenge copy&paste:

Create an Apex class that returns an array (or list) of formatted strings ('Test 0', 'Test 1', ...). The length of the array is determined by an integer parameter.The Apex class must be called 'StringArrayTest' and be in the public scope.
The Apex class must have a public static method called 'generateStringArray'.
The 'generateStringArray' method must return an array (or list) of strings. Each string must have a value in the format 'Test n' where n is the index of the current string in the array. The number of returned strings is specified by the integer parameter to the 'generateStringArray' method.




Here's my Apex Class:


public class StringArrayTest {
    public static String[] generateStringArray(Integer n) {
        String[] s = new List<String>();
        for(Integer i = 0; i < n; i++) {
            s.add('Test' + i);
        }
        return s;
    }
}

And here's the error message upon checking that class upon:

Executing the 'generateStringArray' method failed. Either the method does not exist, is not static, or does not return the proper number of strings.
Hi all,

Quick question about how to utilize the data I'm getting back from my query. There seems to be some confusion on my side as to how to either make the type correct, because I must return a specific type, or maybe how to modify my query slightly so I am getting back the desired type. There is a lookup relationship between two objects, Review__c and Media__c, the latter being the parent. The query functions in its current state, but I am completely at a loss as to how I might access the data in the "Reviews__r" column, column 2, of said query.

SOQL Query
 
List<Media__c> Med = new List<Media__c>();

				Med = [SELECT id, (SELECT Review_Submission_Date__c, 
				Comments__c, Media__c, Member__c, Rating__c FROM Reviews__r) FROM Media__c  
				WHERE id =:recId LIMIT 50];

The above is my query, and it will return a single row. I need the data contined in row 1, column 2, but am unsure how to extract it from the above List<Media__c> and place into a List<Review__c> type to be returned by my getter. If any clarification is needed to help solve this, just say so. Any help is much appreciated!

Best,
Tom

 
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
  • 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!
There are two main points I'm unsure about, so some clarification there is really what I'm looking for, in addition to the only lines of code I want. First, the part where I use __r to, at least I think, grab the id of the parent-Media record. I'm doing that because I want to use it for my SOQL query later on, the id = :recId part, but I'm not 100% sure it's correctly grabbing the Media recId for later use. The second part I'm unsure about is how I'm meant to access the Reviews_Completed__c field. This goes into the idea of taking a list and breaking it down into single objects to access their fields, which I thought I had done before without any problem. I'm not sure if my SOQL query should be a list of <Media__c> type, or a single Media__c sObject variable. If i can just do LIMIT 1, since the only record that matters is the one I'm currently saving and about to allow insertation, then I would think I don't need a list, in which case I should be able to increment it by simply doing sObject.FieldToIncrement++; My code is below and includes the list based case since I've explained the other way I had imagined it may work. Neither are working. Again, any help is much appreciated!

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