• CloudNerd
  • NEWBIE
  • 25 Points
  • Member since 2015
  • Founder
  • Cloud Nerd


  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 4
    Replies
I am trying to write a test class that inserts a new Opportunity and then a new custom object "Project" and then relates the opportunity to the Project.

I keep getting the following error in the code below when running the test:

"System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: MPM4_BASE__Milestone1_Project__c.Opportunity__r"
 
@isTest
private class newMilestoneTestClass {
    static testMethod void validatenewMilestone() {
       Opportunity o = new Opportunity(Name='Test Opp', CloseDate=date.today(), StageName='Closed Won');
       // Insert opportunity
       insert o;
        
       MPM4_BASE__Milestone1_Project__c p = new MPM4_BASE__Milestone1_Project__c(Name='Test Project', MPM4_BASE__Status__c='Set', Opportunity__c=o.Id);
       System.debug('Status before inserting new project: ' + p.MPM4_BASE__Status__c);
       System.debug('Opp is: ' + p.Opportunity__c);

       // Insert project
       insert p;
    
       // Retrieve the new project
       p = [SELECT MPM4_BASE__Status__c FROM MPM4_BASE__Milestone1_Project__c WHERE Id = :p.Id];

       // Test that the trigger correctly updated the related Opportunity
       System.assertEquals(o.Id, p.Opportunity__r.Id);
    }
    
}

 
Hello Dev Community,

We are getting the following error for the APEX Trigger code below:

"Error: Compile Error: unexpected token: '{' at line 35 column 64"

We are trying to iterate over a custom object "Project" and add child objects records "Milestones" based on the value of certain fields in the "Project" related object "Opportunity". I think there may be something wrong with the FOR loop syntax? Any help would be appreciated!
 
Trigger newMilestone on MPM4_BASE__Milestone1_Project__c (before insert){

    List<MPM4_BASE__Milestone1_Project__c> mileProj = [SELECT Id FROM MPM4_BASE__Milestone1_Project__c WHERE MPM4_BASE__Status__c='Set'];
    List<MPM4_BASE__Milestone1_Milestone__c> miles = new List<MPM4_BASE__Milestone1_Milestone__c>();
    
    for (MPM4_BASE__Milestone1_Project__c p : mileProj){
        if (p.Opportunity__r.OrderHardware__c == TRUE) {
            MPM4_BASE__Milestone1_Milestone__c morder = new MPM4_BASE__Milestone1_Milestone__c();
            morder.MPM4_BASE__Project__c = p.Id;
            morder.Name = 'Order Hardware';
            morder.MPM4_BASE__Kickoff__c = date.today();
            morder.OwnerId = '0051a000000STEt';
            miles.add(morder);
        }    else if (p.Opportunity__r.SchedulePSG__c == TRUE) {
                MPM4_BASE__Milestone1_Milestone__c porder = new MPM4_BASE__Milestone1_Milestone__c();
                porder.MPM4_BASE__Project__c = p.Id;
                porder.Name = 'Schedule PSG';
                porder.MPM4_BASE__Kickoff__c = date.today();
                porder.OwnerId = '0051a000000R80f';
                miles.add(porder);
        }    else if (p.Opportunity__r.ScheduleRental__c == TRUE) {
                MPM4_BASE__Milestone1_Milestone__c rorder = new MPM4_BASE__Milestone1_Milestone__c();
                rorder.MPM4_BASE__Project__c = p.Id;
                rorder.Name = 'Schedule Rental';
                rorder.MPM4_BASE__Kickoff__c = date.today();
                rorder.OwnerId = '0051a000000STEt';
                miles.add(rorder);
        }    else if (p.Opportunity__r.SendWelcome__c == TRUE) {
                MPM4_BASE__Milestone1_Milestone__c worder = new MPM4_BASE__Milestone1_Milestone__c();
                worder.MPM4_BASE__Project__c = p.Id;
                worder.Name = 'Schedule Welcome';
                worder.MPM4_BASE__Kickoff__c = date.today();
                worder.OwnerId = '0051a000000R80f';
                miles.add(worder);
        }    else (p.Opportunity__r.ScheduleOffsite__c == TRUE) {
                MPM4_BASE__Milestone1_Milestone__c oorder = new MPM4_BASE__Milestone1_Milestone__c();
                oorder.MPM4_BASE__Project__c = p.Id;
                oorder.Name = 'Schedule Offsite';
                oorder.MPM4_BASE__Kickoff__c = date.today();
                oorder.OwnerId = '0051a000000STEt';
                miles.add(oorder);
    }
   }
   
   insert miles;
}

 
I am trying to write a test class that inserts a new Opportunity and then a new custom object "Project" and then relates the opportunity to the Project.

I keep getting the following error in the code below when running the test:

"System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: MPM4_BASE__Milestone1_Project__c.Opportunity__r"
 
@isTest
private class newMilestoneTestClass {
    static testMethod void validatenewMilestone() {
       Opportunity o = new Opportunity(Name='Test Opp', CloseDate=date.today(), StageName='Closed Won');
       // Insert opportunity
       insert o;
        
       MPM4_BASE__Milestone1_Project__c p = new MPM4_BASE__Milestone1_Project__c(Name='Test Project', MPM4_BASE__Status__c='Set', Opportunity__c=o.Id);
       System.debug('Status before inserting new project: ' + p.MPM4_BASE__Status__c);
       System.debug('Opp is: ' + p.Opportunity__c);

       // Insert project
       insert p;
    
       // Retrieve the new project
       p = [SELECT MPM4_BASE__Status__c FROM MPM4_BASE__Milestone1_Project__c WHERE Id = :p.Id];

       // Test that the trigger correctly updated the related Opportunity
       System.assertEquals(o.Id, p.Opportunity__r.Id);
    }
    
}

 
On https://developer.salesforce.com/trailhead/apex_database/apex_database_sobjects, it gives 2 examples on how to populate fields on a new record: is either one better than the other, in terms of taking up memory, or any governance limits? thanks.

1) Account acct = new Account(Name='Acme', Phone='(415)555-1212', NumberOfEmployees=100); OR
2) Alternatively, you can use the dot notation to add fields to an sObject. The following is equivalent to the previous example, although it takes a few more lines of code.view source
print?
 Account acct = new Account();
 acct.Name = 'Acme';
 acct.Phone = '(415)555-1212';
 acct.NumberOfEmployees = 100;
hello helpers

i am experiencing a strange error when try to run an soql query within a trigger.

the query try to retrieve a record set from Attachment object and look like

String variable ='something';
list<Attachment> att =[select id, Name from Attachment where Name =:variable ];

the attachment object contain more then150000 records.

the query throw an exception saying that is non selectiv.
but 

when I execute the same query in developer console I there is no attacment having that name.

so my question is how could be non selectiv a query which would return zero records?

thanks in advance
csbaa
  • September 22, 2015
  • Like
  • 0
Hello Dev Community,

We are getting the following error for the APEX Trigger code below:

"Error: Compile Error: unexpected token: '{' at line 35 column 64"

We are trying to iterate over a custom object "Project" and add child objects records "Milestones" based on the value of certain fields in the "Project" related object "Opportunity". I think there may be something wrong with the FOR loop syntax? Any help would be appreciated!
 
Trigger newMilestone on MPM4_BASE__Milestone1_Project__c (before insert){

    List<MPM4_BASE__Milestone1_Project__c> mileProj = [SELECT Id FROM MPM4_BASE__Milestone1_Project__c WHERE MPM4_BASE__Status__c='Set'];
    List<MPM4_BASE__Milestone1_Milestone__c> miles = new List<MPM4_BASE__Milestone1_Milestone__c>();
    
    for (MPM4_BASE__Milestone1_Project__c p : mileProj){
        if (p.Opportunity__r.OrderHardware__c == TRUE) {
            MPM4_BASE__Milestone1_Milestone__c morder = new MPM4_BASE__Milestone1_Milestone__c();
            morder.MPM4_BASE__Project__c = p.Id;
            morder.Name = 'Order Hardware';
            morder.MPM4_BASE__Kickoff__c = date.today();
            morder.OwnerId = '0051a000000STEt';
            miles.add(morder);
        }    else if (p.Opportunity__r.SchedulePSG__c == TRUE) {
                MPM4_BASE__Milestone1_Milestone__c porder = new MPM4_BASE__Milestone1_Milestone__c();
                porder.MPM4_BASE__Project__c = p.Id;
                porder.Name = 'Schedule PSG';
                porder.MPM4_BASE__Kickoff__c = date.today();
                porder.OwnerId = '0051a000000R80f';
                miles.add(porder);
        }    else if (p.Opportunity__r.ScheduleRental__c == TRUE) {
                MPM4_BASE__Milestone1_Milestone__c rorder = new MPM4_BASE__Milestone1_Milestone__c();
                rorder.MPM4_BASE__Project__c = p.Id;
                rorder.Name = 'Schedule Rental';
                rorder.MPM4_BASE__Kickoff__c = date.today();
                rorder.OwnerId = '0051a000000STEt';
                miles.add(rorder);
        }    else if (p.Opportunity__r.SendWelcome__c == TRUE) {
                MPM4_BASE__Milestone1_Milestone__c worder = new MPM4_BASE__Milestone1_Milestone__c();
                worder.MPM4_BASE__Project__c = p.Id;
                worder.Name = 'Schedule Welcome';
                worder.MPM4_BASE__Kickoff__c = date.today();
                worder.OwnerId = '0051a000000R80f';
                miles.add(worder);
        }    else (p.Opportunity__r.ScheduleOffsite__c == TRUE) {
                MPM4_BASE__Milestone1_Milestone__c oorder = new MPM4_BASE__Milestone1_Milestone__c();
                oorder.MPM4_BASE__Project__c = p.Id;
                oorder.Name = 'Schedule Offsite';
                oorder.MPM4_BASE__Kickoff__c = date.today();
                oorder.OwnerId = '0051a000000STEt';
                miles.add(oorder);
    }
   }
   
   insert miles;
}