• ikle asea
  • NEWBIE
  • 0 Points
  • Member since 2021

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 2
    Replies
Error says "You might not have the required permissions, or the named credential "ProjectService" might not exist."                                 t .
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