0

I am returning an error that says: 'Variable does not exist: o.Id'.

I am unsure what the problem is.

Thanks,

public with sharing class closedWonNewBusinessRedirect{

    // property
    public Opportunity closedWonNBOpp {get; set;}

    // constructor for extension
    public closedWonNewBusinessRedirect(ApexPages.StandardController controller){
        closedWonNBOpp = (Opportunity)controller.getRecord();                                  
    }

    public PageReference createOppAndRedirect(){
        try{

            // Field Mappings
            Opportunity o = new Opportunity();
            o.RecordTypeId = Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('New Business').getRecordTypeId();
            o.AccountId = closedWonNBOpp.AccountId;
            o.OwnerId = UserInfo.getUserId();
            o.CloseDate = closedWonNBOpp.CloseDate;
            o.Billing_Effective_Date__c = closedWonNBOpp.Billing_Effective_Date__c;
            o.Amount = closedWonNBOpp.Amount;
            o.StageName = closedWonNBOpp.StageName;
            o.Probability = closedWonNBOpp.Probability;
            o.Description = closedWonNBOpp.Description;

            // Values to populate the Opportunity name with: 
            String dateCreated = String.valueOf(Date.TODAY());
            String createdByName = UserInfo.getName();
            String accountName = [SELECT Name FROM Account WHERE Id =:closedWonNBOpp.accountId].Name;

            // Opportunity Name:
            o.Name = 'New Business created on ' + dateCreated + ' by ' + createdByName + ' for ' + accountName + '.';
            insert o;

             } catch (DmlException e){
            System.debug('The following exception has occurred from the closedWonNewBusinessRedirect.apxc: ' + e.getMessage());
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.FATAL, 'There has been an error with this page. Please email 123@gmail.com to log this issue.'));
             }

        // PageReferences        
        PageReference pageRedirectForClosedWonOpp = new PageReference('/apex/closedWonToContractPage');
        PageReference pageRedirectForNonClosedWonOpp = new PageReference('/'+o.Id); // THIS IS WHERE THE ERROR IS

        if(closedWonNBOpp.StageName == 'Closed Won'){
            pageRedirectForClosedWonOpp.setRedirect(true);
            return pageRedirectForClosedWonOpp; 
        } else { 
            pageRedirectForNonClosedWonOpp.setRedirect(true);
            return pageRedirectForNonClosedWonOpp;  
        }
        }
    }    
3

You have created an instance of opportunity inside your try block and you are accessing o.id outside the try block. The scope of the o variable is restricted only within the try block

Please move the opportunity instantiate line outside the try block.

 Opportunity o = new Opportunity();

 try{

            // Field Mappings

}
  • most welcome, happy I could help :) @AndyHitchings – Rao Sep 22 '15 at 20:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.