0

I have a Visualforce page using custom Apex as an extension. It is relatively simple, living on my "Milestone" object, displaying some related Services in a checkbox list, and my goal is to take whichever were selected and create a new separate junction object from the Milestone to the Service.

If I select 0 checkbox items, I redirect properly to the previous record page, which is where I want to be. If I select 1 or more of the checkbox items, when I click the button it redirects me to the same page. I can't figure out why. What's wrong with my createRelatedServices() function?

visualforce:

    <apex:pageBlock title="Select the services that are applicable to this milestone.">
        <apex:pageblockSection >
            <apex:selectCheckboxes value="{!selections}">
                <apex:selectOptions value="{!ServicesNameToID}" /> 
            </apex:selectCheckboxes>
        </apex:pageblockSection>
    </apex:pageBlock>


    <apex:commandButton action="{!createRelatedServices}" 
                        value="Add Related Service" id="button"/>
</apex:form>

Apex extension:

public class RelatedServiceController {

 public Map<String, ID> ServicesNameToID {get;set;}
 public pse__Milestone__C milestone {get;set;}
 public pse__Milestone__C queriedMilestone {get;set;}     
 public String[] selections {get;set;}

 public RelatedServiceController(ApexPages.StandardController stdController) {
    milestone = (pse__Milestone__C)stdController.getRecord();
    getServices();
 }

 public void getServices(){
    //Need to fully query the milestone to get project ID/opp ID
    queriedMilestone = [SELECT Name, ID, pse__Project__c, pse__Project__r.pse__Opportunity__c from pse__milestone__C where id = :milestone.ID];

    List<OpportunityLineItem> oppLineItems = [SELECT name, ID, Product2ID, ProductName__c from OpportunityLineItem  
                                              WHERE OpportunityID = :queriedMilestone.pse__project__r.pse__opportunity__c];

    ServicesNameToID = new Map<String, ID>();
    for(OpportunityLineItem oli: oppLineItems){
        ServicesNameToID.put(oli.ProductName__c, oli.Product2ID);
        //system.debug(oli.ProductName__c);
        //system.debug(oli.Product2ID);            
    }

}

public PageReference createRelatedServices(){

    List<Related_Service__c> relatedServicesToInsert = new List<Related_Service__c>();

    if(selections != null && selections.size() > 0){
        //Strings of the service name will be returned - match them up with the ID of that service using our map
        for (integer i = 0; i < selections.size(); i++){
            ID serviceToRelateID = ServicesNameToID.get(selections[i]);

            Related_Service__c rs = new Related_Service__c();
            rs.Milestone__c = milestone.ID;
            rs.Service__c = serviceToRelateID;
            rs.Name = 'test';
            relatedServicesToInsert.add(rs);
        }
    }

    insert relatedServicesToInsert;
    PageReference pageRef = new PageReference('/' + milestone.ID);
    pageRef.setRedirect(true);
    return pageRef;
  }
}
  • Move your questio to salesforce.meta.stackexchange.com – mario ruiz Jul 16 at 15:06
  • You're most probably running into an error. Try adding <apex:pageMessages /> tag on your VF page to see if any errors show up. – Aayush K Jul 16 at 15:17
  • @marioruiz its proper SF question and deserves to be here. Questions about Salesforce Stackexchange needs to be on Meta. – Pranay Jaiswal Jul 16 at 15:25
  • @AayushK thank you, after adding the pageMessages I saw that it was an issue with my selection string array not being initialized properly. After doing so it works. This can be closed. – user70249 Jul 16 at 15:31
  • Great. I've moved my comment to the answer section so that you can mark it as answered. – Aayush K Jul 16 at 15:50
0

Moving the comment to the answer so that it can be marked as answered.

You're most probably running into an error. Try adding <apex:pageMessages /> tag on your VF page to see if any errors show up.

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.