1

I have to pass the title of my apex:page block into my Controller. I could not find a way to do it. Any idea?

My code should look like that:

public PageReference saveFF() {       

 RecordType rt;
    if*(title == 'Motor')*
        rt = [Select Id From RecordType Where DeveloperName = 'Motor' And sObjectType = 'Fact_Finder__c'];

    FF.RecordTypeId = rt.Id;

insert (this.FF);    
return new PageReference('/' + recordId);    
  }      

Updates

 public string CurrentPageName {get;set;} 

 public PageReference saveFF() 
 { 

    RecordType rt; 
    if(pageName == 'HomeForm') 
        rt = [Select Id From RecordType Where DeveloperName = 'Home_Contents' And sObjectType = 'Fact_Finder__c']; 
        FF.RecordTypeId = rt.Id; 
    insert this.FF); 
    return new PageReference('/' + recordId); 
}

In the VF Page:

 <apex:commandButton action="{!saveFF}" value="Submit"styleClass="buttonsubmit" reRender="block" > 
 <apex:param name="p" value="{!$CurrentPage.Name}" assignTo="{!CurrentPageName}" />
  • I guess you have mutiple pageblock and you want to identify from which pageblock save method get called correct ? you may have multiple save buttons ? – Ratan Paul Jul 27 '16 at 6:23
  • Only one page block and one save button. But several VF pages on the same controller. – ultima67 Jul 27 '16 at 6:25
  • Then why you need pageblock title ? is it dynamic ? – Ratan Paul Jul 27 '16 at 6:27
  • No. I need the String to set my record type. As I said I have several VF pages on the Controller and depending on which VF page is saved it needs to have a different record type. I did not find another way than to pick the page name to set the record type. I also need this value to set other fields value in my controller. – ultima67 Jul 27 '16 at 6:35
  • SO basically.. you need to identify the current page. you can do it simply assign current page name to variable – Ratan Paul Jul 27 '16 at 6:40
1

So you have multiple pages and same controller for all.

So you can identify the page Name {!$CurrentPage.Name}. This will give you current page name in VF page.

To get this page name in controller

Take a string variable in controller called strCurrentPageName

public String strCurrentPageName {get;set;}

and in Vf page use

when you click on save

<apex:commandButton value="Save" action="{!saveFF}">
            <apex:param name="pageName"
                value="{!$CurrentPage.Name}"
                assignTo="{!strCurrentPageName }"/>
        </apex:commandButton>

Ex: PASSING PARAMETERS WITH A COMMANDBUTTON

Here value will be {!$CurrentPage.Name} and assignTo will be variable name.

Now when you click on save current pageName will be assign to strCurrentPageName variable. Do it for all the pages.

Now in save method check strCurrentPageName variable name and based in page name query recordType and assign the recordType.

Another solution to get the current pageName in controller it self

If you have parameter in url

then

String strCurrentpgName = ApexPages.currentPage().getUrl().substringBetween('apex/', '?');

if you don't have parameter in url

then use

String pageName = ApexPages.currentPage().getUrl().substringAfter('apex/');

Updates

As per your updated code.

Your controller code is correct but your page code is not correct your apex:param should be inside apex:commandbutton like this

 <apex:commandButton action="{!saveFF}" value="Submit" 
                                 styleClass="buttonsubmit" reRender="block" > 
     <apex:param name="p" value="{!$CurrentPage.Name}" assignTo="{!CurrentPageName}" />
 </apex:commandButton>
| improve this answer | |
  • Hi Ratan, thank you very much for this detailed answer. I have added the parameter to my command button, the string to my controller, but when I use it in my if statement to get the recordtype I get an error message 'Attempt to de-reference a null object'. Do I miss something? The 2nd solution does not work in my case, I am working in Lightning and cannot pass parameter in the URL. – ultima67 Jul 28 '16 at 4:41
  • @ultima67 can you add your updated code in your question so I can review the code. – Ratan Paul Jul 28 '16 at 5:19
  • In the controller : public string CurrentPageName {get;set;} public PageReference saveFF() { RecordType rt; if(pageName == 'HomeForm') rt = [Select Id From RecordType Where DeveloperName = 'Home_Contents' And sObjectType = 'Fact_Finder__c']; FF.RecordTypeId = rt.Id; insert this.FF); return new PageReference('/' + recordId); } In the VF Page: <apex:commandButton action="{!saveFF}" value="Submit"styleClass="buttonsubmit" reRender="block" /> <apex:param name="p" value="{!$CurrentPage.Name}" assignTo="{!CurrentPageName}" /> Sorry, not the better format for code! – ultima67 Jul 28 '16 at 5:50
  • @ultima67 check my updates.. – Ratan Paul Jul 28 '16 at 6:09
  • @ultima67 you're welcome :) – Ratan Paul Jul 28 '16 at 6:22

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.