2

I have the following code:

public with sharing class ExportCheckPageController {

public boolean checkYes {
    get{if(checkYes == null)
        checkYes = false;
        return checkYes;}
    set;}
public boolean checkNo{
    get{if(checkNo == null)
        checkNo = false;
        return checkNo;}
    set;}
/*public boolean q1{
  get{if(q1 == null)
  q1 = false;
  return q1;}
  set;} */ 

public Export_Check__c exportCheck {get; set;}
public Opportunity opp {get;set;}
private account acc;
private Id sgiRT;
private Id apacRT;
private Id oppId;
private List<Export_Check__c> exportList = new List<Export_Check__c>();
private List<Opportunity> oppList = new List<Opportunity>();
private ApexPages.standardController controller;

public ExportCheckPageController(ApexPages.StandardController stdController){
    controller = stdController;
    exportCheck = new Export_Check__c();
    opp = new Opportunity();
    acc = new Account();
    opp =(Opportunity)stdController.getRecord();
    oppId =stdController.getid();
    system.debug(oppId);

    if((ApexPages.currentPage().getParameters().get('oppId') != null) && (ApexPages.currentPage().getParameters().get('oppId') != '')){
        oppId = ApexPages.currentPage().getParameters().get('oppId');
        system.debug(oppId);
    }
    sgiRT = [SELECT Id FROM RecordType WHERE Name = 'SGI Opportunity'].Id;
    //apacRT = [SELECT Id FROM RecordType WHERE Name = 'SGI APAC Opportunity'].Id;
    loadOppExports();
    system.debug(oppId);
}

public void loadOppExports(){
    if(oppId != null){
        //Add all other fields from opportunity i may need & from Opp Account
        //Gonna have to do a few more quries to get all info from acount and contacts
        opp = [Select Id, Name, Account.Id, isExportCompleted__c, (Select Id, Name, ExportStatus__c, CheckNO__c, CheckYes__c, OppExportCheck__c from Export_Checks__r) From Opportunity where id =: oppId];
        acc = [select Id, Owner.Id, Parent.Id, Parent.Name, Name, BillingStreet, BillingCity, BillingState, BillingPostalCode, BillingCountry, 
               Street_Address__c, City__c, State_Province__c, Zip_Postal_Code__c, Country_Code__c, Account_Intergraph_Line_of_Business__c, Industry, Type, 
               Phone, Account_Category__c, RecordTypeId,(select Id, account.id, Name, firstname, lastname, account.name, Account_Industry__c, Department, 
                                                         MailingStreet, MailingCity, MailingPostalCode, MailingState, MailingCountry, Email, Phone from contacts) 
               from Account where account.id in (Select Opportunity.AccountId From Opportunity where id =: opp.id)];
        system.debug(opp);
        system.debug(acc);
        //Check List size - but there should only be one- maybe need to revisit this later
        if(!opp.Export_Checks__r.isEmpty()){
            for(Export_Check__c ec: opp.Export_Checks__r){
                if(ec.Id != null){
                    //exportCheck = new Export_Check__c();
                    exportCheck = ec;
                    exportCheck.OppExportCheck__c = ec.OppExportCheck__c;
                    system.debug(exportCheck.OppExportCheck__c);
                }
                else{
                    //exportCheck = new Export_Check__c();
                    exportCheck.OppExportCheck__c = opp.Id;
                    system.debug(exportCheck.OppExportCheck__c);
                }
            }
        }
        else{
            //exportCheck = new Export_Check__c();
            exportCheck.OppExportCheck__c = opp.Id;
            system.debug(exportCheck.OppExportCheck__c);
        }
    }
}

public PageReference redirect(){
    PageReference pr;
    if(opp.RecordTypeId == sgiRT ){
        if(!opp.isExportCompleted__c && exportCheck.Id == null){
            pr = new PageReference('/apex/ExportCheckPage1?oppId='+ opp.Id);
            pr.setRedirect(true); 
        }
    }
    else{
        pr = new PageReference('/' + opp.id);
        pr.getParameters().put('nooverride', '1');
    }
    return pr;
}     

public void setExportCheck(){
    if(checkYes){
        opp.isExportCompleted__c = false;
        exportCheck.Name = opp.Name;
        exportCheck.Company_Name__c = acc.Name;
        exportCheck.Country__c = acc.Country_Code__c;
        exportCheck.City__c = acc.City__c; 
        exportCheck.State__c = acc.State_Province__c;
        exportCheck.Street_1__c = acc.Street_Address__c;
        exportCheck.Zip__c = acc.Zip_Postal_Code__c;
        exportCheck.CheckYes__c = checkYes;
        exportCheck.CheckNO__c = checkNo;
        exportCheck.ExportStatus__c = 'Under Review';
        //eventually we will want to set all of Export Yes/No questions here

    }
    else if(checkNo){
        opp.isExportCompleted__c = true;
        exportCheck.Name = opp.Name;
        exportCheck.Company_Name__c = acc.Name;
        exportCheck.Country__c = acc.Country_Code__c;
        exportCheck.City__c = acc.City__c; 
        exportCheck.State__c = acc.State_Province__c;
        exportCheck.Street_1__c = acc.Street_Address__c;
        exportCheck.Zip__c = acc.Zip_Postal_Code__c;
        exportCheck.CheckNO__c = checkNo;
        exportCheck.CheckYes__c = checkYes;
        exportCheck.Export1__c = 'No';
        exportCheck.Export2__c = 'No';
        exportCheck.Export3__c = 'No';
        exportCheck.Export4__c = 'No';
        exportCheck.Export5__c = 'No';
        exportCheck.Export6__c = 'No';
        exportCheck.Export7__c = 'No';
        exportCheck.ExportStatus__c = 'Approved';

    }
    else{
        ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'You must select one of the Yes or No options below. '));
    }
    if(!exportList.isEmpty()){
        exportList.clear();
        exportList.add(exportCheck);
        system.debug(checkNo);
        system.debug(checkYes);
        system.debug(exportCheck);
        system.debug(opp);

    }
    else{
        exportList.add(exportCheck);
        system.debug(checkNo);
        system.debug(checkYes);
        system.debug(exportCheck);
        system.debug(opp);

    }
}
public PageReference startApproval(){
    //This method will start he approval process - Do an insert of an record and kick off the approval process.
    //Phase 1 - Start he approval process, display info messages on page to tell the user what the next steps to take
    //Phase 2 - Add Steps for the rest of the attachments to be completed in Salesforce and then send to Compliance team for approval
    //Phase 3 - Do a web service call out to Tradebeam to do export screening and and store this informationon the object. 
    Database.SaveResult[] insertEx;
    system.debug(exportList);
    system.debug(Limits.getHeapSize());
    system.debug(Limits.getLimitHeapSize());
    insertEx = database.insert(exportList, false);
    for (Database.SaveResult sr: insertEx){
        if(sr.isSuccess()){
            system.debug('Insert Succeeded. Export ID: '+ sr.getId());
        }
        else{
            for(database.Error err: sr.getErrors()){
                system.debug('The following error has occured. ');
                system.debug(err.getStatusCode() + ': ' + err.getMessage());
                system.debug('Error fields: ' + err.getFields());

            }
        }
    }
    try{
        system.debug(opp);
        update opp;
    }
    catch(DMLException ex){
        system.debug('Error+++ '+ ex);
    }

    PageReference redirect =  new PageReference('/apex/StandardOpportunity?id='+ opp.Id);
    redirect.setRedirect(true);
    return redirect;
    //return Page.StandardOpportunity;
}
}

and the requirements changed to add this line of code((opp.RecordTypeId == sgiRT || opp.RecordTypeId == apacRT) && opp.OwnerId == UserInfo.getUserId() ) in the Redirect method and replace if(opp.RecordTypeId == sgiRT ) but now I get the error of: SObject row was retrieved via SOQL without querying the requested field: Opportunity.OwnerId

As you can see I am querying Opportunity.OwnerId in the SOQL query and I have the field listed in my VF page. I'm confused to why it is erroring out.

7
  • 1
    First you are not quering OwnerId. And second I can't see you are using opp.OwnerId in any of your code.. Can you pls let us know in which line you are getting error?
    – Ratan Paul
    Jan 28, 2016 at 17:00
  • Are you getting a trigger error?
    – Adrian Larson
    Jan 28, 2016 at 17:03
  • the error is coming from this line 'if ((opp.RecordTypeId == sgiRT || opp.RecordTypeId == apacRT) && opp.OwnerId == UserInfo.getUserId() )' Jan 28, 2016 at 17:09
  • @AdrianLarson not sure by what you mean with a trigger error? Jan 28, 2016 at 17:09
  • @user3708392 That line does not appear in your code in the OP so your question is somewhat confusing.
    – Adrian Larson
    Jan 28, 2016 at 17:11

1 Answer 1

6

When using a StandardController, there are a few different ways to manipulate the way it queries for the record. Note your line in the constructor:

 opp = (Opportunity)stdController.getRecord();

The above performs a query, pulling down all fields used on the page. In addition to what fields you use on the page, you can use the addFields method (though it cannot be used in a test).

List<String> fieldsToQuery = new List<String> { 'OwnerId' };
if (!Test.isRunningTest()) controller.addFields(fieldsToQuery);
// getRecord

Another option is to actually include them on the page through various forms of dummy markup. One example:

<apex:variable var="ownerId" value="{!Opportunity.OwnerId}" />

As for the query in your startApproval method, since you already have your Opportunity data, it is probably more straightforward to query for the child data directly instead of overwriting your saved opp.

List<Export_Check__c> childExportChecks = [SELECT ... FROM Export_Check__c];
7
  • Thank you for your help @Adrian Larson! I'm now getting an error to check and make sure an Id is null, on this like 'if(!opp.isExportCompleted__c && exportCheck.Id == null) '. It's saying I'm trying to Attempt to de-reference a null object. How would I go about making sure an Id is null? Jan 28, 2016 at 17:33
  • Do you have a null value in exportCheck? The record itself has to be initialized before you can check the value of its Id.
    – Adrian Larson
    Jan 28, 2016 at 17:38
  • it is null. How would I initialize it? Jan 28, 2016 at 17:44
  • I think you need to ask a separate question around that. It really depends on what you are trying to accomplish. You could pull it from a query, or you could create one like exportCheck = new Export_Check__c().
    – Adrian Larson
    Jan 28, 2016 at 17:46
  • I assume I need to do that in the redirect method? I have this if statement that is instantiating it ' if(!opp.Export_Checks__r.isEmpty()){ exportCheck = opp.Export_Checks__r[0]; } else{ exportCheck = new Export_Check__c(); exportCheck.OppExportCheck__c = opp.Id; system.debug(exportCheck.OppExportCheck__c);' Jan 28, 2016 at 18:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .