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
CBradley1985CBradley1985 

Compile Error: Test methods must be in test classes at line 171 column 24

Hello,

 

I am extremely new to APEX so please don't laugh at me...

 

I have a piece of code that we paid for and the API Version is 23.0 and I need to copy this code and create a new class so that I can use a different button that we have to run the seperate code after I sweek a couple things. (Mainly just values on the new opportunity.)

 

And with version 28 or higher I get these errors. I have tried to find a place to get the Force.com IDE v.27 or earlier and haven't had much luck.

 

1. If you could point me to a place that I can get this earlier version so I can use eclipse to impliment into the sandbox with the IDE plug-in that would be fantastic.

 

2. Or if you want to take a peek at the piece of code and tell me what needs to be changed to make it work I'd love to learn. (Trust me, I'm not one to just say give me the answer...I want to learn it!)

 

This seems to be the piece that's getting me into trouble. Again, I appreciate ANY help.

 

Thanks!


Chris

 

    @isTest

    public static void testCloneOpportunity()

    {

        OpportunityLineItem line = [select OpportunityId from OpportunityLineItem limit 1];

        CloneOpportunity.clone(line.OpportunityId);

    }

Best Answer chosen by Admin (Salesforce Developers) 
nathaForcenathaForce

Hi Chris,

 

Looking a little further, this question was asked (and answered) on this thread: http://salesforce.stackexchange.com/questions/12516/spring-13-test-methods-must-be-in-test-classes-error where salesforce prevents test methods from being implemented within regular classes (starting version 28).

 

Here is a link to a tutorial on creating test classes: http://wiki.developerforce.com/page/An_Introduction_to_Apex_Code_Test_Methods

and a little bit of information on "SeeAllData", which you should get familiar with: http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_annotation_isTest.htm

All Answers

CBradley1985CBradley1985

Here is the whole code if it helps any...

 

global with sharing class CloneOpportunity 
{
    public static final String PRICEBOOK_NAME = 'CTM Price Book';
    
    WebService static String clone(String oppId)
    {
        final String ACTIVITY_HISTORY_SUBJECT = 'Products Missing from ' + PRICEBOOK_NAME;
        final String COMMONS = 'The following products are not associated with the ' + PRICEBOOK_NAME;
        
        // Clone opportunity
        String pricebook2Id = [select Id from Pricebook2 where Name = :PRICEBOOK_NAME limit 1].Id;
        RecordType recordType = [select Id from RecordType where SobjectType = 'Opportunity' and Name = 'Renewal Opportunity' limit 1];
        String oppQuery = getOpportunityQuery(oppId);
        Opportunity sourceOpp = (Opportunity)Database.query(oppQuery);
        Opportunity newOpportunity = sourceOpp.clone(false, true, false, false);
        newOpportunity.RecordTypeId = recordType.Id;
        newOpportunity.Pricebook2Id = pricebook2Id;
        newOpportunity.Contract_Number__c = '';
        newOpportunity.Record_Type__c = 'Quote';
        newOpportunity.StageName = 'Needs Analysis';
        newOpportunity.Thank_You_Card_Email__c = '';
        newOpportunity.Thank_You_Card_Contact__c = null;
        newOpportunity.Purchase_Order__c = '0' ;
        newOpportunity.GreatPlainsCustomerID__c ='';
        if(newOpportunity.Renewal_Cycle_Follow_Up_Date__c != null)
        {
            newOpportunity.Renewal_Cycle_Follow_Up_Date__c = newOpportunity.Renewal_Cycle_Follow_Up_Date__c.addYears(1);
        }
        if(newOpportunity.Contract_Start_Date__c != null)
        {
            newOpportunity.Contract_Start_Date__c = newOpportunity.Contract_Start_Date__c.addYears(1);
        }
        if(newOpportunity.Contract_End_Date__c != null)
        {
            newOpportunity.Contract_End_Date__c = newOpportunity.Contract_End_Date__c.addYears(1);
        }
        if(newOpportunity.Distribution_Dates_Start_Date__c != null)
        {
            newOpportunity.Distribution_Dates_Start_Date__c = newOpportunity.Distribution_Dates_Start_Date__c.addYears(1);
        }
        if(newOpportunity.Distribution_Dates_End_Date__c != null)
        {
            newOpportunity.Distribution_Dates_End_Date__c = newOpportunity.Distribution_Dates_End_Date__c.addYears(1);
        }
        if(newOpportunity.Bill_Dates_Start_Date__c != null)
        {
            newOpportunity.Bill_Dates_Start_Date__c = newOpportunity.Bill_Dates_Start_Date__c.addYears(1);
        }
        if(newOpportunity.Bill_Dates_End_Date__c != null)
        {
            newOpportunity.Bill_Dates_End_Date__c = newOpportunity.Bill_Dates_End_Date__c.addYears(1);
        }
        if(newOpportunity.CloseDate != null)
        {
            newOpportunity.CloseDate = newOpportunity.CloseDate.addYears(1);
        }
        insert newOpportunity;
        
        // Clone opportunity products
        Set<String> productIds = getProductIdsFromLineItem(oppId);
        Map<String, String> ctmProductIds = new Map<String, String>();
        for(PricebookEntry entry : [select Id, Product2Id from PricebookEntry where Pricebook2Id = :pricebook2Id and Product2Id in :productIds and CurrencyIsoCode = :newOpportunity.CurrencyIsoCode])
        {
            ctmProductIds.put(entry.Product2Id, entry.Id);
        }
        
        String lineItemQuery = getOpportunityLineItemQuery(oppId);
        OpportunityLineItem[] sourceLineItems = (List<OpportunityLineItem>)Database.query(lineItemQuery);
        List<OpportunityLineItem> newLineItems = new List<OpportunityLineItem>();
        List<String> lineItemsInActivity = new List<String>();
        String productNames = '';
        
        for(OpportunityLineItem item : sourceLineItems)
        {
            if(ctmProductIds.keySet().contains(item.PricebookEntry.Product2Id))
            {
                OpportunityLineItem newLineItem = item.clone(false, true, false, false);
                newLineItem.OpportunityId = newOpportunity.Id;
                newLineItem.PricebookEntryId = ctmProductIds.get(item.PricebookEntry.Product2Id);
                if(newLineItem.Billing_Start_Date__c != null)
                {
                    newLineItem.Billing_Start_Date__c = newLineItem.Billing_Start_Date__c.addYears(1);
                }
                if(newLineItem.Billing_End_Date__c != null)
                {
                    newLineItem.Billing_End_Date__c = newLineItem.Billing_End_Date__c.addYears(1);
                }
                if(newLineItem.Contract_Start_Date__c != null)
                {
                    newLineItem.Contract_Start_Date__c = newLineItem.Contract_Start_Date__c.addYears(1);
                }
                if(newLineItem.Contract_End_Date__c != null)
                {
                    newLineItem.Contract_End_Date__c = newLineItem.Contract_End_Date__c.addYears(1);
                }
                newLineItems.add(newLineItem);
            }
            else
            {
                String productName = ' • ' + item.PricebookEntry.Product2.Name + '\r\n';
                lineItemsInActivity.add(productName);
                productNames += productName;
            }
        }
        if(newLineItems.size() > 0)
        {
            insert newLineItems;
        }
        
        if(lineItemsInActivity.size() > 0)
        {
            Task task = new Task();
            task.WhatId = newOpportunity.Id;
            task.Subject = ACTIVITY_HISTORY_SUBJECT;
            task.ActivityDate = Date.today().addDays(-1);
            task.Status = 'Completed';
            task.Type= 'SF Message';
            task.Description = COMMONS + ':\r\n';
            for(String lineItemName : lineItemsInActivity)
            {
                task.Description += lineItemName;
            }
            insert task;
        }
        return productNames + '^o^' + newOpportunity.Id;
    }
    
    /**
     * Get the opportunity query.
     */
    private static String getOpportunityQuery(String oppId)
    {
        String result = 'select ';
        for(String fieldApiName : Schema.SObjectType.Opportunity.fields.getMap().keySet())
        {
            result += fieldApiName + ',';
        }
        result = result.substring(0, result.length() - 1) + ' from Opportunity where Id = \'' + oppId + '\'';
        return result;
    }
    
    /**
     * Get the opportunity product query.
     */
    private static String getOpportunityLineItemQuery(String oppId)
    {
        String result = 'select ';
        for(String fieldApiName : Schema.SObjectType.OpportunityLineItem.fields.getMap().keySet())
        {
            result += fieldApiName + ',';
        }
        result = result.replace('totalprice,', '');
        result = result + 'PricebookEntry.Product2Id, PricebookEntry.Product2.Name from OpportunityLineItem where OpportunityId = \'' + oppId + '\'';
        return result;
    }
    /**
     * Get the product IDs in opportunity.
     */
    private static Set<String> getProductIdsFromLineItem(String oppId)
    {
        Set<String> result = new Set<String>();
        for(OpportunityLineItem item : [select PricebookEntry.Product2Id from OpportunityLineItem where OpportunityId = :oppId])
        {
            result.add(item.PricebookEntry.Product2Id);
        }
        return result;
    }
    
    @isTest
    public static void testCloneOpportunity()
    {
        OpportunityLineItem line = [select OpportunityId from OpportunityLineItem limit 1];
        CloneOpportunity.clone(line.OpportunityId);
    }
}

nathaForcenathaForce

Hi Chris,

For right now, and to unblock you so that you can go further in your implementation, you can change the API version of your test class by navigating to 

Setup->App Setup->Develop->Apex Classes

Once you find your test class, click "Edit", and the "Version Settings" tab. There, you should be able to select a prior API version that will allow you to compile the code. 

You should have this same option if you have not created the class yet, and create it in the UI ("New" button)

Hope that helps

 

Nathalie

nathaForcenathaForce

Hi Chris,

 

Looking a little further, this question was asked (and answered) on this thread: http://salesforce.stackexchange.com/questions/12516/spring-13-test-methods-must-be-in-test-classes-error where salesforce prevents test methods from being implemented within regular classes (starting version 28).

 

Here is a link to a tutorial on creating test classes: http://wiki.developerforce.com/page/An_Introduction_to_Apex_Code_Test_Methods

and a little bit of information on "SeeAllData", which you should get familiar with: http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_annotation_isTest.htm

This was selected as the best answer
CBradley1985CBradley1985

Thank you very much!

 

I will be working on this in the coming days so hopefully I should be able to get this going.

 

Appreciate the help info!

 

Chris