1

I have a custom object, Deal Registration, that can have a list of Products added to it. I create a junction object DealRegProducts. While on a Deal Registration, the user would scroll to the related lists section and press the "Add" button to add 1 or multiple products to that specific Deal Registration.

I have everything working as it should except that i can only get it to work if i hard-code my DealRegID. Obviously, that is not ideal, so i was wondering if someone could help me in getting the DealRegID so that i can pass that variable through my controller?

On the DealRegProducts related list section on the Deal Registration page, i have a button "Add". When pressed, my VF page is fired off. On the VF page, the user selects either "Add" or "Cancel". The only way i can get either button to work is if i hard-code the DealRegID that i get from the URL while on the Deal Registration object page. How can i store the DealRegID as a variable in my controller?

Here is the VF code:

<apex:page standardController="Deal_Registration_Products__c" extensions="CCW_WrapperSampleController" recordSetVar="Deal_Registration_Products__r">
<apex:form >
    <apex:pageBlock >
    <apex:pageMessages />
    <apex:pageBlockButtons >
        <apex:commandButton action="{!Add}" disabled="{!IF((featureList.size==0),true,false)}" value="add"/>
        <apex:commandButton action="{!cancel}" immediate="true" value="cancel"/>
    </apex:pageBlockButtons>
    <apex:pageBlockTable rendered="{!IF((featureList.size!=0),true,false)}" value="{!featureList}" var="ft">
        <apex:column headerValue="select">
            <apex:inputCheckbox value="{!ft.selected}"/>
        </apex:column>
        <apex:column headerValue="name">{!ft.feat.Name}</apex:column>
    </apex:pageBlockTable>
    <apex:outputText rendered="{!IF((featureList.size==0),true,false)}"><b>No_records_found_</b></apex:outputText>
    </apex:pageblock>
</apex:form>

And here is the controller:

public with sharing class CCW_WrapperSampleController{ 
public CCW_WrapperSampleController(ApexPages.StandardSetController controller) {
    system.debug('Test');
    dealRegId = ApexPages.currentPage().getParameters().get(dealRegId);
    system.debug(dealRegId);
}


public CCW_WrapperSampleController(ApexPages.StandardController controller) {
    dealRegId = ApexPages.currentPage().getParameters().get('Id');
    system.debug('Test3');
}



//Variable Declaration
public list<featureWrapperClass> featureWrapperList{get;set;}
public string dealRegId;

//constructor
public CCW_WrapperSampleController(){
    dealRegId= ApexPages.currentPage().getParameters().get(dealRegId);
    system.debug('Test2');
}

public list<featureWrapperClass> getFeatureList(){
    featureWrapperList = new list<featureWrapperClass>();
    set<id> removeIdSet = new set<id>();
    for(Deal_Registration_Products__c junc:[SELECT Deal_Registration__c, Product__c
                                              FROM Deal_Registration_Products__c
                                              WHERE Deal_Registration__c =:'a0RJ0000009mVrh']){
        removeIdSet.add(junc.Product__c);
    }
    for(Product2 feat:[SELECT id, name
                                           FROM Product2 
                                           WHERE id NOT IN: removeIdSet]){
        featureWrapperList.add(new featureWrapperClass(feat));
    }
   return featureWrapperList;
}


public pageReference Add(){
    list<Deal_Registration_Products__c> juncList = new list<Deal_Registration_Products__c>();
    for(featureWrapperClass featSelected :featureWrapperList){
        //get only the selected records
        if(featSelected.selected){
        //create new junction record for every selected Feature.
            Deal_Registration_Products__c junctionRecord = new Deal_Registration_Products__c(Deal_Registration__c= 'a0RJ0000009mVrh',
                                                                    Product__c    = featSelected.feat.id);
            juncList.add(junctionRecord);
        }
    }
    if(juncList.size()>0){
        //this will create a recor din your junction object
        insert juncList;
        pageReference pageRef = new PageReference('/'+'a0RJ0000009mVrh');
        return pageRef;
    }
    else{
        Apexpages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Please_select_a_record'));
        return null;
    }
}


public pageReference cancel(){
    pageReference pageRef = new PageReference('/'+ 'a0RJ0000009mVrh');
    pageRef.setRedirect(false);
    return pageRef;
}
//A wrapper class
public class featureWrapperClass{
    public boolean selected{get;set;}
    public Product2 feat{get;set;}
    public featureWrapperClass(Product2 f){
        this.feat = f;
        this.selected= false;     
    }       
}

}

3 Answers 3

1

Can you not do ApexPages.currentPage().getParameters().get('Id') to get the Deal Registraion Id?

0

Ok I see what you are saying. That you want to pass Id when you click the New button on related list. The only way I can think of doing it is to create a Custom Button on related list and on click write a java script to extract the Id. Once you have the Id in java script, call you VF page by passing the Id. Hope this helps.

4
  • This is exactly how i would like to implement it. this is the syntax that i am using for the custom button on my junction object, DealRegProducts: {URLFOR("/apex/TestPage2", !Id)}. When i click on the "New" button on the related list, i get the following error: URLFOR is not defined. A couple things - this button should be created on the junction object, right? Also, what is causing that error?
    – RayRay
    Dec 4, 2014 at 1:00
  • try this in java script
    – get2arpit
    Dec 4, 2014 at 4:10
  • url = 'apex/VFPAGE/Id='+IdofTheDealRegristraion; window.top.location.href = url;
    – get2arpit
    Dec 4, 2014 at 4:12
  • @RayRay, I think u r using wrong syntax, u missed {!URLFOR}, try using that. Jun 28, 2015 at 17:31
0

@RayRay, below is what I would recommend:

Update the "Add" custom link button on DealRegProducts object with the following parameters:

  • Behavior: Display in existing window without sidebar and header
  • Content Source: URL
  • In the big text area section, write: /apex/AddDealRegProducts?id={!Deal_Registration__c.Id} (Replace AddDealRegProducts with your own VF Page name)

With the above change, when you click on the "Add" button on the related list, Salesforce will resolve the {!Deal_Registration__c.Id} to the Id of the current record you are on.

You must log in to answer this question.

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