function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
sfdev1sfdev1 

Error : SObject row was retrieved via SOQL without querying the requested field: ChildCampaigns

I'm getting the following error on on a campaign trigger
 
Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger findParent2 caused an unexpected exception, contact your administrator: findParent2: execution of AfterUpdate caused by: System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: ChildCampaigns: Trigger.findParent2: line 6, column 30
  = Required InformationCampaign Information:
 
 
trigger findParent2 on Campaign (after update) {
   ID ParentID = null;
  
   for (Campaign cam1 : [Select id, name from Campaign]){
        system.debug('Loop 1 - campaign id : ' + cam1.id);
        for (Campaign cam2 : cam1.childcampaigns){
           system.debug('Loop 2 - campaign id : ' + cam2.id);
            if (cam2.id == cam1.id){
                  ParentID = cam1.id;     
            }
       }
   }
     system.debug('Parent for  is '+ParentID);
 }
Best Answer chosen by Admin (Salesforce Developers) 
SuperfellSuperfell
The error means you're trying to access something that you didn't include in the query, in this case the childcampaigns collection, you need to either remove the reference to that, or update your query to include that info.

All Answers

SuperfellSuperfell
The error means you're trying to access something that you didn't include in the query, in this case the childcampaigns collection, you need to either remove the reference to that, or update your query to include that info.
This was selected as the best answer
sfdev1sfdev1

How do I update your query to include ChildCampaigns?

I assume that the ChildCampaigns returns a colllection or array of Campaign objects.

 

 

Marco_kMarco_k
Your code should look like
for (Campaign cam1 : [Select id, name,
childcampaigns from Campaign]){

As you cannot refer to a field that you didn't query for an object
for (Campaign cam2 : cam1.childcampaigns){


Hope this helps.

Marco
sfdev1sfdev1
I tried the following : for (Campaign cam1 : [Select id, name,childcampaigns from Campaign]){
but I'm getting this error. childcampaigns is a function not a field name
 
Error: Compile Error: No such column 'childcampaigns' on entity 'Campaign'. If you are attempting to use a custom field, be sure to append the '__c' after the custom field name. Please reference your WSDL or the describe call for the appropriate names. at line 4 column 25
Ash Shah 16Ash Shah 16
If i have a soql that is using lookup relationship and master details both combined, is using 5 level deep field info, the soql query does produce the correct info , however when I try to use it in code it is giving error 

System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: Contact.Name

I put system.debug( junction_email__r.xyz_email__r.master_acc__r.membership__r.name); threw above error, however upto 

junction_email__r.xyz_email__r.master_acc__r.membership__c point it works.

So is it a big? Soql does produce result but when you try to consume the same field which is 5 level deep it doesn't work
PRD12PRD12
Hello , Did we find solutions for the above issue . I am facing the same issue can anyone please help me on What am I missing in the below code that's causing me an error "sObject row was retrieved via soql without querying the requested field MaintenancePlan:Week_Numbers__c"
 
        public List<MaintenancePlan> getMpsByVstIds_V2(Set<Id> vstIds, String weekSelected) {
            return [
                    SELECT AccountId,
                            Account.GTMU_CUST_ID__c,
                            Account.CUST_ID__c,
                            Account.Name,
                            Account.R12WK_AGGRD_SLS_AMT__c,
                            Day_of_the_Week__c,
                            FLNASCH2_Activity_Type__c,
                            Frequency,
                            FrequencyType,
                            LocationId,
                            Location.GTMU_ID__c,
                            Location.Name,
                            Location.PEP_ExternalID_Location__c,
                            Sequence__c,
                            Total_Duration__c,
                            Temporary_Account_Name__c,
                            Engg_Staging_Id__c,
                            Service_Weeks__c,
                    (
                            SELECT Activity_Duration_Quantity__c,
                                    Drive_Miles__c,
                                    Activity_Mandatory_End_By_Time__c,
                                    Activity_Mandatory_Start_By_Time__c,
                                    FLNASCH2_Activity_Type__c,
                                    FLNASCH2_Meal_Break_Duration__c,
                                    FLNASCH2_Rest_Period_Duration__c,
                                    route__r.PEP_ExternalID_Route__c
                            FROM Visit_Schedule_Activities__r
                            WHERE Visit_Schedule_Template__c IN :vstIds
                            AND Week_Number__c =:weekSelected
                            ORDER BY FLNASCH2_Activity_Type__c ASC
                            LIMIT 200
                    )
                    FROM MaintenancePlan
                    WHERE Id IN (
                            SELECT Maintenance_Plan__c
                            FROM Visit_Schedule_Activity__c
                            WHERE Visit_Schedule_Template__c IN :vstIds
                            AND Week_Number__c =:weekSelected
                    )
                    ORDER BY Sequence__c ASC
            ];
        }
    }