function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Long DuongLong Duong 

Can I write a VF page/component, then add it to multiple pages (Account, Opportunity,..) without changing the code

Hi

I want to write a VF page or component and user can add it to any page that he want.

I tried but found that VF page must define a specific controller or I can't add this VF page into any page(Account, Opportunity,...)

Is there a way to write a VF page/component once and use it anywhere?

 

Thank you so much

Saurabh Gupta ManrasSaurabh Gupta Manras

Hello Long Duong,

yes you can write a vf page or component that can be called from multiple vf pages, but in order to show the vf page in detail page, the page should have standard controller as api name of the object so in order to achieve this you need to write a generic vf component, that can be used for any sobject and then one page for each object where you will only call the component. 

Please choose it as the Best answer, if you find it helpful .

 

Hemant_SoniHemant_Soni
Hi,
Yes you can use vf component for multiple object. Only you need to pass id as a parameter and then you can get whatever you want to show on vf page.It is very easy process.
Thanks
Hemant
Long DuongLong Duong

Thanks Saurabh Gupta 3039 and Hemant_Soni,

I'm using the same solution with yours.

I'm looking for a way to write once and use it anywhere without changing the code, but seem that it's impossible.

Hemant_SoniHemant_Soni
Hi,
Can you please share you code and your complete requirement i will made change in your code.And please dont loose hope.
Thanks
Hemant 
Long DuongLong Duong
//I use this page to show the component in Opportunity page
<apex:page standardController="Opportunity" docType="html-5.0" >
  <c:MyContent></c:MyContent>
</apex:page>
//I put layout in component, so I can use it in multiple pages
<apex:component controller="MyContentController" allowDML="true">    
    <apex:form>
           <apex:outputPanel>
               ...
               ...
           </apex:outputPanel>
    </apex:form>
</apex:component>
 
//I use this controller to control MyContent component
public with sharing class MyContentController {
    public MyContentController() {
    }
end

Hemant_Soni, I don't think code is need in this case, but it's here.

Complele requirement:

  • Current: if I want to show the component in Opportunity page, I must write a page for it with standardController="Opportunity"
  • Expected: I don't want change code to show the component in a new pape. Content of this component could be shown in any page which user want(Account, Opportunity,..).
Hemant_SoniHemant_Soni
Hi long,
You can use below code for your component.Below code is for any object.Only you need to create page for object and pass the id of object or record and code will give you what ever you want from code.:)
public static list<Wrapper> getSelectedRecord(list<String>lstSelectedRecord,String phoneFieldDefaultValue){
        
        Id reocrdId ;
        String SOQL = '';
        String objType = '';
        String glue = '';
        
        Map<String,String> fieldMap = new Map<String,String>();
        list<SelectOptionItem> selectOptionlist = new list<SelectOptionItem>();
        list<String>lstRecord = new list<String>();
        
        list<Wrapper>lstsObject = new list<Wrapper>();
        
        for(String oCon:lstSelectedRecord){
            for(String str:oCon.split(',')){
                lstRecord.add(str.replace('[','').replace(']','').trim());	
            }
        }
        
        reocrdId = Id.valueOf(lstRecord[0]);
        objType =  String.valueOf(reocrdId.getSobjectType());
        
        Map<String, Schema.SObjectField> fieldNameWiseAPIName = Schema.getGlobalDescribe().get(objType).getDescribe().fields.getMap();
        for(Schema.SObjectField field :fieldNameWiseAPIName.values()){
            
            schema.describeFieldResult dfieldResult = field.getDescribe();
            String fieldType = string.valueOf(dfieldResult.getType()); 
            
            if (fieldType == 'PHONE'){
                fieldMap.put(string.valueOf(dfieldResult.Name), string.valueOf(dfieldResult.Label));
            }
        }
        
        for(String fld : fieldMap.keySet()){
        	selectOptionlist.add(new SelectOptionItem(fld,fieldMap.get(fld)));
        }
        
        SOQL = 'select id,Name,FirstName,LastName, ';
        for(string s:fieldMap.keySet()){
            SOQL += glue+s;
            glue = ',';	
        }
        
        SOQL += ' from '+objType+' where Id IN :lstRecord';
            for(sObject oSPW: database.query(SOQL)){
            if(oSPW.get(phoneFieldDefaultValue) != null){
                lstsObject.add(new Wrapper(oSPW,oSPW.get(phoneFieldDefaultValue)+'',false,phoneFieldDefaultValue,'',selectOptionlist));
            }
            else{
                lstsObject.add(new Wrapper(oSPW,'',false,phoneFieldDefaultValue,'',selectOptionlist));
            }
        }
        return lstsObject;	
    }
It is just a code sample we can use this code in apex component.
If you think this kind of code help you then i will wrote code for you.
Please let me know it helps you.
Thanks
Hemant