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
Michael Kolodner 13Michael Kolodner 13 

Test Class Won't Compile

Newbie to coding here and I'm trying to make a test class. Here's the code I've currently got:
@IsTest
public class tempTestPBchanges {
    
    /* Test class for making sure Assignment PB replacements are the same as the three PBs that existed before.
* Temporary, for use only in Dev sandbox, not to be deployed. If you want to save the code, copy it into a text file.
*/
    
    public static string createTestApprenticeship() {
        //------ Set up Test Data
        
        //Create a School District (may not be necessary, as school district is only required in the UI?)
        
        //Create a School
        Account testSchoolAccount = new Account(Name = 'Test school');
        insert testSchoolAccount;
        
        //Create a Company
        Account testCompanyAccount = new Account (Name='Test Company');
        insert testCompanyAccount;
        
        //Create a Mentor
        Contact testMentorContact = new Contact (
            npsp__Primary_Affiliation__c = testCompanyAccount.Id,
            FirstName = 'Test', 
            LastName = 'MentorOne',
            Creativity__c = 2,
            Communication__c = 2,
            Following_directions__c = 0,
            Helping_Others__c = 0,
            Influencing_others__c = 0,
            Operating_Machines__c = 0,
            Planning__c = 0,
            Reading_and_Writing__c = 0,
            Research_and_Logic__c = 0,
            Singing_and_acting__c = 0,
            Solving_problems__c = 0,
            Understanding_Shapes__c = 0,
            Working_with_others__c = 0,
            Attention_to_detail__c = 0,
            Career_Area_Industry__c = 'Entertainment'
        );
        insert testMentorContact;
        
        //Create a Catalog Entry
        Catalog_Entry__c testCatalogEntry1 = new Catalog_Entry__c(
            Name = 'Test MentorOne Catalog Entry', 
            Account__c = testCompanyAccount.Id,
            Primary_Mentor_Lookup__c = testMentorContact.Id
        );
        insert testCatalogEntry1;
        
        //Create a Student at the school
        Contact testStudentContact = new Contact (
            FirstName='Test', 
            LastName='Student', 
            npsp__Primary_Affiliation__c=testSchoolAccount.Id);
        insert testStudentContact;
        
        //Create a Program at the school
        yat__Program__c testProgram = new yat__Program__c(
            Name = 'Test Program SY19', 
            yat__Program_Year__c='SY19'
        );
        insert testProgram;
        
        //Create a Mentorship
        Apprenticeship__c testMentorship = new Apprenticeship__c(
            Name = 'Test Student Mentorship',
            Program__c = testProgram.Id,
            School__c = testSchoolAccount.Id,
            Student_Name__c = testStudentContact.Id,
            StageName__c = 'Student Requested',
            Creativity__c = 2,
            Communication__c = 2,
            Following_directions__c = 2,
            Helping_Others_New__c = 2,
            Influencing_others__c = 2,
            Operating_Machines__c = 2,
            Planning__c = 2,
            Reading_and_Writing__c = 2,
            Research_and_Logic__c = 2,
            Singing_and_acting__c = 2,
            Solving_problems__c = 2,
            Understanding_Shapes__c = 2,
            Working_with_others__c = 2,
            Attention_to_detail__c = 2,
            Innovative__c = 2,
            Creative__c = 2,
            Building__c = 2,
            Organization__c = 2,
            Scientific__c = 2,
            Social__c = 2
        );
        insert testMentorship;
        
        //------ Here ends the basic data creation
        
        //------ Testing the PBs
        //Create an Assignment
        Assignment_New__c testAssign = new Assignment_New__c (
            Name = 'testAssign',
            Catalogue_Entry__c = testCatalogEntry1.Id,
            Apprenticeship_Name__c = testMentorship.Id,
            Status__c = 'Possible Match'
        );
        insert testAssign;
        
    //Got to have a return for this method. Right now it's the assignment status. 
    //But it probably should be the Id so you can work with it later. But I can't make the system asserts work if it's an Id.        
        return testAssign.Status__c;
    }
    
    //Test 1: Assert that Assignment is not updated by the IsNew portions of the PB because it was not in the Matched Stage
    @isTest
    public static void testInsertAssignment() {
        String testAssignStatus = createTestApprenticeship();
        System.assertequals('Possible Match', testAssignStatus);
    }
Right now createTestApprenticeship() returns a string, the Status__c field of the inserted Assignment. But I would prefer to have it return an Id. Then in the test method I think I should be able to get the Status__c of that inserted Assignment by just using testAssignStatus.Status__c
But when I switch the return of the createTestApprenticeship() method to be Id, change the last line of that method to be 
return testAssign.Id;

and then change the system.assert to be
System.assertequals('Possible Match', testAssignStatus);
it won't compile. I get the error on that last line, in fact.

What am I missing, please?
Raj VakatiRaj Vakati
try this  .. you are missing end { 
 
@IsTest
public class tempTestPBchanges {
    
    /* Test class for making sure Assignment PB replacements are the same as the three PBs that existed before.
* Temporary, for use only in Dev sandbox, not to be deployed. If you want to save the code, copy it into a text file.
*/
    
    public static string createTestApprenticeship() {
        //------ Set up Test Data
        
        //Create a School District (may not be necessary, as school district is only required in the UI?)
        
        //Create a School
        Account testSchoolAccount = new Account(Name = 'Test school');
        insert testSchoolAccount;
        
        //Create a Company
        Account testCompanyAccount = new Account (Name='Test Company');
        insert testCompanyAccount;
        
        //Create a Mentor
        Contact testMentorContact = new Contact (
            npsp__Primary_Affiliation__c = testCompanyAccount.Id,
            FirstName = 'Test', 
            LastName = 'MentorOne',
            Creativity__c = 2,
            Communication__c = 2,
            Following_directions__c = 0,
            Helping_Others__c = 0,
            Influencing_others__c = 0,
            Operating_Machines__c = 0,
            Planning__c = 0,
            Reading_and_Writing__c = 0,
            Research_and_Logic__c = 0,
            Singing_and_acting__c = 0,
            Solving_problems__c = 0,
            Understanding_Shapes__c = 0,
            Working_with_others__c = 0,
            Attention_to_detail__c = 0,
            Career_Area_Industry__c = 'Entertainment'
        );
        insert testMentorContact;
        
        //Create a Catalog Entry
        Catalog_Entry__c testCatalogEntry1 = new Catalog_Entry__c(
            Name = 'Test MentorOne Catalog Entry', 
            Account__c = testCompanyAccount.Id,
            Primary_Mentor_Lookup__c = testMentorContact.Id
        );
        insert testCatalogEntry1;
        
        //Create a Student at the school
        Contact testStudentContact = new Contact (
            FirstName='Test', 
            LastName='Student', 
            npsp__Primary_Affiliation__c=testSchoolAccount.Id);
        insert testStudentContact;
        
        //Create a Program at the school
        yat__Program__c testProgram = new yat__Program__c(
            Name = 'Test Program SY19', 
            yat__Program_Year__c='SY19'
        );
        insert testProgram;
        
        //Create a Mentorship
        Apprenticeship__c testMentorship = new Apprenticeship__c(
            Name = 'Test Student Mentorship',
            Program__c = testProgram.Id,
            School__c = testSchoolAccount.Id,
            Student_Name__c = testStudentContact.Id,
            StageName__c = 'Student Requested',
            Creativity__c = 2,
            Communication__c = 2,
            Following_directions__c = 2,
            Helping_Others_New__c = 2,
            Influencing_others__c = 2,
            Operating_Machines__c = 2,
            Planning__c = 2,
            Reading_and_Writing__c = 2,
            Research_and_Logic__c = 2,
            Singing_and_acting__c = 2,
            Solving_problems__c = 2,
            Understanding_Shapes__c = 2,
            Working_with_others__c = 2,
            Attention_to_detail__c = 2,
            Innovative__c = 2,
            Creative__c = 2,
            Building__c = 2,
            Organization__c = 2,
            Scientific__c = 2,
            Social__c = 2
        );
        insert testMentorship;
        
        //------ Here ends the basic data creation
        
        //------ Testing the PBs
        //Create an Assignment
        Assignment_New__c testAssign = new Assignment_New__c (
            Name = 'testAssign',
            Catalogue_Entry__c = testCatalogEntry1.Id,
            Apprenticeship_Name__c = testMentorship.Id,
            Status__c = 'Possible Match'
        );
        insert testAssign;
        
    //Got to have a return for this method. Right now it's the assignment status. 
    //But it probably should be the Id so you can work with it later. But I can't make the system asserts work if it's an Id.        
        return testAssign.Status__c;
    }
    
    //Test 1: Assert that Assignment is not updated by the IsNew portions of the PB because it was not in the Matched Stage
    @isTest
    public static void testInsertAssignment() {
        String testAssignStatus = createTestApprenticeship();
        System.assertequals('Possible Match', testAssignStatus);
    }
}

 
Michael Kolodner 13Michael Kolodner 13
No, that's not it. I have the final } and it compiles fine. But I want to change it to this, which refuses to compile at line 117 with "Variable does not exist: Status__c":
@IsTest
public class tempTestPBchanges {
    
    /* Test class for making sure Assignment PB replacements are the same as the three PBs that existed before.
* Temporary, for use only in Dev sandbox, not to be deployed. If you want to save the code, copy it into a text file.
*/
    
    public static Id createTestApprenticeship() {
        //------ Set up Test Data
        
        //Create a School District (may not be necessary, as school district is only required in the UI?)
        
        //Create a School
        Account testSchoolAccount = new Account(Name = 'Test school');
        insert testSchoolAccount;
        
        //Create a Company
        Account testCompanyAccount = new Account (Name='Test Company');
        insert testCompanyAccount;
        
        //Create a Mentor
        Contact testMentorContact = new Contact (
            npsp__Primary_Affiliation__c = testCompanyAccount.Id,
            FirstName = 'Test', 
            LastName = 'MentorOne',
            Creativity__c = 2,
            Communication__c = 2,
            Following_directions__c = 0,
            Helping_Others__c = 0,
            Influencing_others__c = 0,
            Operating_Machines__c = 0,
            Planning__c = 0,
            Reading_and_Writing__c = 0,
            Research_and_Logic__c = 0,
            Singing_and_acting__c = 0,
            Solving_problems__c = 0,
            Understanding_Shapes__c = 0,
            Working_with_others__c = 0,
            Attention_to_detail__c = 0,
            Career_Area_Industry__c = 'Entertainment'
        );
        insert testMentorContact;
        
        //Create a Catalog Entry
        Catalog_Entry__c testCatalogEntry1 = new Catalog_Entry__c(
            Name = 'Test MentorOne Catalog Entry', 
            Account__c = testCompanyAccount.Id,
            Primary_Mentor_Lookup__c = testMentorContact.Id
        );
        insert testCatalogEntry1;
        
        //Create a Student at the school
        Contact testStudentContact = new Contact (
            FirstName='Test', 
            LastName='Student', 
            npsp__Primary_Affiliation__c=testSchoolAccount.Id);
        insert testStudentContact;
        
        //Create a Program at the school
        yat__Program__c testProgram = new yat__Program__c(
            Name = 'Test Program SY19', 
            yat__Program_Year__c='SY19'
        );
        insert testProgram;
        
        //Create a Mentorship
        Apprenticeship__c testMentorship = new Apprenticeship__c(
            Name = 'Test Student Mentorship',
            Program__c = testProgram.Id,
            School__c = testSchoolAccount.Id,
            Student_Name__c = testStudentContact.Id,
            StageName__c = 'Student Requested',
            Creativity__c = 2,
            Communication__c = 2,
            Following_directions__c = 2,
            Helping_Others_New__c = 2,
            Influencing_others__c = 2,
            Operating_Machines__c = 2,
            Planning__c = 2,
            Reading_and_Writing__c = 2,
            Research_and_Logic__c = 2,
            Singing_and_acting__c = 2,
            Solving_problems__c = 2,
            Understanding_Shapes__c = 2,
            Working_with_others__c = 2,
            Attention_to_detail__c = 2,
            Innovative__c = 2,
            Creative__c = 2,
            Building__c = 2,
            Organization__c = 2,
            Scientific__c = 2,
            Social__c = 2
        );
        insert testMentorship;
        
        //------ Here ends the basic data creation
        
        //------ Testing the PBs
        //Create an Assignment
        Assignment_New__c testAssign = new Assignment_New__c (
            Name = 'testAssign',
            Catalogue_Entry__c = testCatalogEntry1.Id,
            Apprenticeship_Name__c = testMentorship.Id,
            Status__c = 'Possible Match'
        );
        insert testAssign;
        
    //Got to have a return for this method. Right now it's the assignment status. 
    //But it probably should be the Id so you can work with it later. But I can't make the system asserts work if it's an Id.        
        return testAssign.Id;
    }
    
    //Test 1: Assert that Assignment is not updated by the IsNew portions of the PB because it was not in the Matched Stage
    @isTest
    public static void testInsertAssignment() {
        String testAssignStatus = createTestApprenticeship();
        System.assertequals('Possible Match', testAssignStatus.Status__c);
    }
    
    
    //Test2: Change Assignment to Matched
    
    //Assert that PB puts mentor email in Current Mentor Email (on contact) fills Match Score (on Assignment)
    //Assert that PB called flow that created ACRs and closed other assignments
    //Change Assignment to Completed
    //Assert that when assignment is moved to completed, PB sets apprenticeship to Finished.
    

    
}

 
Michael Kolodner 13Michael Kolodner 13
(Oops--I had one more mistake in what I just pasted in. It failes to compile just as much if the first word on line 116 is "Id" instead of "String". 
Raj VakatiRaj Vakati


Try this code .. change your method retunr type as string and return the status insted of id 
 
@IsTest
public class tempTestPBchanges {
    
    /* Test class for making sure Assignment PB replacements are the same as the three PBs that existed before.
* Temporary, for use only in Dev sandbox, not to be deployed. If you want to save the code, copy it into a text file.
*/
    
    public static String createTestApprenticeship() {
        //------ Set up Test Data
        
        //Create a School District (may not be necessary, as school district is only required in the UI?)
        
        //Create a School
        Account testSchoolAccount = new Account(Name = 'Test school');
        insert testSchoolAccount;
        
        //Create a Company
        Account testCompanyAccount = new Account (Name='Test Company');
        insert testCompanyAccount;
        
        //Create a Mentor
        Contact testMentorContact = new Contact (
            npsp__Primary_Affiliation__c = testCompanyAccount.Id,
            FirstName = 'Test', 
            LastName = 'MentorOne',
            Creativity__c = 2,
            Communication__c = 2,
            Following_directions__c = 0,
            Helping_Others__c = 0,
            Influencing_others__c = 0,
            Operating_Machines__c = 0,
            Planning__c = 0,
            Reading_and_Writing__c = 0,
            Research_and_Logic__c = 0,
            Singing_and_acting__c = 0,
            Solving_problems__c = 0,
            Understanding_Shapes__c = 0,
            Working_with_others__c = 0,
            Attention_to_detail__c = 0,
            Career_Area_Industry__c = 'Entertainment'
        );
        insert testMentorContact;
        
        //Create a Catalog Entry
        Catalog_Entry__c testCatalogEntry1 = new Catalog_Entry__c(
            Name = 'Test MentorOne Catalog Entry', 
            Account__c = testCompanyAccount.Id,
            Primary_Mentor_Lookup__c = testMentorContact.Id
        );
        insert testCatalogEntry1;
        
        //Create a Student at the school
        Contact testStudentContact = new Contact (
            FirstName='Test', 
            LastName='Student', 
            npsp__Primary_Affiliation__c=testSchoolAccount.Id);
        insert testStudentContact;
        
        //Create a Program at the school
        yat__Program__c testProgram = new yat__Program__c(
            Name = 'Test Program SY19', 
            yat__Program_Year__c='SY19'
        );
        insert testProgram;
        
        //Create a Mentorship
        Apprenticeship__c testMentorship = new Apprenticeship__c(
            Name = 'Test Student Mentorship',
            Program__c = testProgram.Id,
            School__c = testSchoolAccount.Id,
            Student_Name__c = testStudentContact.Id,
            StageName__c = 'Student Requested',
            Creativity__c = 2,
            Communication__c = 2,
            Following_directions__c = 2,
            Helping_Others_New__c = 2,
            Influencing_others__c = 2,
            Operating_Machines__c = 2,
            Planning__c = 2,
            Reading_and_Writing__c = 2,
            Research_and_Logic__c = 2,
            Singing_and_acting__c = 2,
            Solving_problems__c = 2,
            Understanding_Shapes__c = 2,
            Working_with_others__c = 2,
            Attention_to_detail__c = 2,
            Innovative__c = 2,
            Creative__c = 2,
            Building__c = 2,
            Organization__c = 2,
            Scientific__c = 2,
            Social__c = 2
        );
        insert testMentorship;
        
        //------ Here ends the basic data creation
        
        //------ Testing the PBs
        //Create an Assignment
        Assignment_New__c testAssign = new Assignment_New__c (
            Name = 'testAssign',
            Catalogue_Entry__c = testCatalogEntry1.Id,
            Apprenticeship_Name__c = testMentorship.Id,
            Status__c = 'Possible Match'
        );
        insert testAssign;
        
    //Got to have a return for this method. Right now it's the assignment status. 
    //But it probably should be the Id so you can work with it later. But I can't make the system asserts work if it's an Id.        
        return testAssign.Status__c;
    }
    
    //Test 1: Assert that Assignment is not updated by the IsNew portions of the PB because it was not in the Matched Stage
    @isTest
    public static void testInsertAssignment() {
        String testAssignStatus = createTestApprenticeship();
        System.assertequals('Possible Match', testAssignStatus.Status__c);
    }
    
    
    //Test2: Change Assignment to Matched
    
    //Assert that PB puts mentor email in Current Mentor Email (on contact) fills Match Score (on Assignment)
    //Assert that PB called flow that created ACRs and closed other assignments
    //Change Assignment to Completed
    //Assert that when assignment is moved to completed, PB sets apprenticeship to Finished.
    

    
}

 
Michael Kolodner 13Michael Kolodner 13
Thanks, Raj. I think, maybe, I'm not expressing the question well. I know that I can return a string of Status__c. That's how it was in the first code snippet I posted. But I want, in later steps, to be able to actually get to other fields of the inserted record in addition to string (and to use the inserted record's Id in lookup fields on other records I'm going to insert). And I would like to do that without having to do a SOQL query, since (I think) I should be able to avoid that step by returning Id...
Keegan PhillipsKeegan Phillips
Thank you for such valuable information. It is very important for me.
Deacon NelsonDeacon Nelson
Thank you very much for such valuable information. Did any of you guys read The Glass Menagerie? This is one of the most famous monumental plays, written by the American playwright Tennessee Williams, which I learned about at https://samploon.com/free-essays/the-glass-menagerie/ The plot revolves around the main character who talks about events of your past life. I advise you to read this book.