3

I am creating a url to prepopulate values on a custom form by parsing the labels from the html of the form. I am using the getContent() method of the page reference class to get the html of the form I want to pre-populate. The issue is that not all the fields on the form are present in the html returned using this method. If I view the source code of the form once it is on the screen, all the fields are represented. Is this a timing issue? Why can't I get the entire html of the page?

This is what is on my page:

<apex:page standardController="DCS_Request__c" extensions="CRM_Kathy" showHeader="false" sidebar="false" standardStylesheets="false">
    <apex:includeScript value="{!URLFOR($Resource.JQueryUI, 'js/jquery-1.8.0.min.js')}"/>

    <apex:pageblock >
        <apex:pageMessages />
        <apex:outputLink value="{!ReturnIdPath}" id="theLink">Return</apex:outputLink>
    </apex:pageblock>

    <script type="text/javascript">
        if ("{!url}") {
            window.location = '{!url}';
        }
    </script>
</apex:page>

And this is part of the class:

public CRM_Kathy(ApexPages.StandardController controller) {

    this.controller = controller; 
        url = createurl();  
    }

    public string url{get;set;}

    private Map<String, String> createDefaultValues() {
        PageReference p = new PageReference('/' + DCS_Request__c.SObjectType.getDescribe().getKeyPrefix() + '/e?nooverride=1');

        String html = p.getContent().toString();
        system.debug('this is the html = ' + html);
        Map<String, String> labelToId = new Map<String, String>(); //gets label of field and the field id from the html of the selected request

        Matcher m = Pattern.compile('<label for="(\\w+?)">(<span class="requiredMark">\\*</span>)?(.*?)</label>').matcher(html);
        while (m.find()) {
            String label = m.group(3);
            system.debug('this is the label from the html = ' + label);
            String id = m.group(1);
            system.debug('this is the id from the html = ' + id);
            labelToId.put(label, id);
        }

        system.debug('this is the label to id = ' + labeltoid);
        return labelToId;
    }
}
4
  • 2
    Is it not possible to populate the values by controller? Commented Jan 9, 2014 at 17:38
  • There may be a timing issue - could you paste in some code that shows where you're calling getContent()? Having said that. it's quite unusual to be parsing the HTML. Could you populate the values in the controller, as @ShebinMathew suggests? Otherwise, you could do this in JavaScript.
    – metadaddy
    Commented Jan 9, 2014 at 18:24
  • I am trying to get rid of using custom settings for populating field values on new custom forms and when I saw this solution I wanted to try it. It all works until I try populating fields on custom forms other than standard fields and that is when I realized that not all the html is being returned in order for me to get all the labels on the form.
    – Kathy
    Commented Jan 9, 2014 at 22:09
  • Are you sure it's returning the expected page layout? It looks like you're just trying to find the required fields; is that correct?
    – Mike Chale
    Commented Jan 9, 2014 at 22:12

1 Answer 1

1

The running user may not be able to access those fields on the page layout for a number of reasons:

  • record types
  • field-level security
  • CRUD permissions

However, the magic 00N numbers you're looking for can be accessed through the Tooling API.

This beats the pants off any other implementation out there, including scraping and hard-coding.

2
  • I just discovered this and you are right it would be exactly what I need, but I have never worked with the tooling API and reading the documentation I found didn't put me any closer to knowing how to use it. Do you have any reference material that would explain how to set this up to a beginner?
    – Kathy
    Commented Jan 10, 2014 at 16:59
  • @Kathy are you on LinkedIn? Commented Jan 10, 2014 at 21:25

You must log in to answer this question.

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