0

Depends on the execution user's profile I am creating a page that will change the layout of the VF page. There are five layouts.

I prepared a Visualforce page for 5 patterns of layout

Depending on the profile of the executing user, I was thinking about how to include one from that five pattern layout with <apex: include>.

However, since this page uses standardController, If the SObject field is not described on the main unit side (the side which is included), it will result in an error.

So, by adding the field to be referenced with the addFields method of standardcontroller, we can avoid it, but adding it to addFields for each layout is also a little troublesome.

If you implement the layout of the main VF page 8 with apex: outputPanel renderd, a huge VF will be created and the source will become tiresome, Do you take a method to addFields,

1
  • 1
    Having the component pages (that are included) use Field Sets can be very useful, as your controller can access and query any and all fields from those Field Sets at initialization time.
    – David Reed
    Apr 10 '18 at 13:43
3

My first thought is to write a controller extension that adds all the fields of the SObject:

public YourController(ApexPages.StandardController sc) {

    // One way to find the SObjectType
    Id id = sc.getId();
    SObjectType t = id.getSObjectType();

    // Add all the fields
    Set<String> fields = t.getDescribe().fields.getMap().keySet();
    if (!Test.isRunningTest()) {
        sc.addFields(new List<String>(fields));
    }
}
4
  • Thank you very much.I learned a lot today. I apologize for my lack of ability, But please let me ask you a few question there is [Test.isRunningTest ()] in the code you indicated What is the reason for needing this?
    – user51376
    Apr 11 '18 at 1:59
  • @ppap No apologies needed here - we are all learning new stuff every day. The check is needed because that method call fails in the unit test environment so needs to be avoided there.
    – Keith C
    Apr 11 '18 at 17:20
  • thank you. Although it is a reference level, do you know whether it is a bug in SF that fails in the test?
    – user51376
    Apr 12 '18 at 8:27
  • @ppap Remove the Test.isRunningTest and see what message you get. I think of it as more of a "by design" choice than a bug: presumably there is some mechanism that doesn't work in the test environment so SF have blocked the call. Also its several years since I tried it so the problem may or may not still exist.
    – Keith C
    Apr 12 '18 at 8:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy