1

I have the following issue, I had a redirect that was working perfectly, see the below code:

PageReference pr;

if(opp.RecordTypeId == sgiRT || opp.RecordTypeId == apacRT){
    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;

The redirect is suppose to make the user go to a visualforce page and answer a question and then go to the opportunity if it meets the compliance information.

The requirements have changed. Now the org just wants it to where it's the opportunity owner to go to the visualforce page so I added the following code to check to see if the user id and the owner id matched(should be the code that is bolded):

 PageReference pr;

if(**(UserInfo.getuserId() == opp.ownerid)** &&
    (opp.RecordTypeId == sgiRT || opp.RecordTypeId == apacRT)) {

    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; 

But now this logic isn't working: if(!opp.isExportCompleted__c && exportCheck.Id == null) and I'm stumped to why it isn't working. It's no longer redirecting the user to the visualforce page.

If I comment out the line of code here: if(!opp.isExportCompleted__c && exportCheck.Id == null)

It will compare the user Id and the owner Id, but when the user selects their answer on the visualforce page it will just refresh the visualforce page and not go back to the opportunity. I need help as to what I'm doing wrong.

To summarize, once a user selects an opportunity, I need to compare the user id and the owner id. If they match then send the user to the compliance page, answer a question and then direct them opportunity. If they don't match simply take them to the opportunity.

Here's all the code in the controller:

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);
    system.debug(sgiRT);
}

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, Owner.Id, Name, Account.Id, isExportCompleted__c, RecordTypeId, (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((UserInfo.getuserId() == opp.ownerid) &&
        (opp.RecordTypeId == sgiRT || opp.RecordTypeId == apacRT)){
            //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){
        system.debug(checkNo);
        system.debug(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){
        system.debug(checkNo);
        system.debug(checkYes);
        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{
        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;
}
}
4
  • 1
    Do you have any unit test to cover this? What do the tests say? You can use runAs() to simulate it running as different users. You have a fairly complex comparison, checking on user ID, record type, and then a nested if where you check fields in opportunity and exportCheck. You'd need to show us the code where you get the opportunity and the exportCheck objects. What happens if the condition in the nested if is not met? what are you returning?
    – mkorman
    Jan 19, 2016 at 14:45
  • @mkorman thank you for your response! I do understand that this is quite complex. I didn't create this code so it's taken me a while to understand it. I updated my OP with all the code in the controller. Unfortunately the original coder didn't create unit tests for this piece. Jan 19, 2016 at 15:03
  • @mkorman does the code help? Jan 19, 2016 at 19:29
  • It does help a bit, but it's a bit convoluted. One of the things I do when trying to find an elusive bug is first cleanup the code: remove commented out code, extract methods to make methods shorter and clearer to understand, create methods to evaluate conditions, and generally the issues become easier to debug. I would suggest doing that, creating some unit tests around this, and then posting your updated code
    – mkorman
    Jan 20, 2016 at 16:10

0

You must log in to answer this question.

Browse other questions tagged .