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
Hengky IlawanHengky Ilawan 

Call getContent and assign to a custom field

Hi,

I have a visualforce page that has a picklist form element (Billing_Entity__c), when it is onChange I need to assign a custom long text field (Declaration__c) whereby the value is from another Visualforce page (SOF_Declaration) which returns text/plain contentType.
Both pages are using the same standardController and extensions.

Problem: Instead of returning the correct content from SOF_Declaration, it simply return the caller's HTML page. So now the page becomes nested. 
Note: I have tried to run the code in getSOFDeclarationString method succesfully in the dev console; it returned the expected text. 

Here is the visualforce:
 
<apex:actionRegion>
    <apex:selectList value="{!Service_Order__c.Billing_Entity__c}" size="1" id="BillingEntity" 
        onChange="changeBillingEntity()">
        <apex:selectOptions value="{!Entities}"/>
        <apex:actionFunction name="changeBillingEntity" action="{!changeBillingEntity}" 
            reRender="elSOFClause" />
    </apex:selectList>
</apex:actionRegion>

....

<apex:outputPanel layout="block" style="padding-bottom:10px" id="elSOFClause">
    <apex:outputText value="{!SOFDeclaration}" escape="false"/>
</apex:outputPanel>

Controller:
 
private String getSOFDeclarationString() {
  PageReference pr = Page.SOF_Declaration;
  pr.getParameters().put('id', SO.Id);
  return pr.getContent().toString();
}

public PageReference changeBillingEntity() {
  SO.Declaration__c = getSOFDeclarationString();
  return null;
}

public String getSOFDeclaration() {
  if (SO.Declaration__c != null) {
    return SO.Declaration__c.replaceAll('\n *\n', '<br/><br/>');
  }
  return '';
}

I tried to change the PageReference instantiation by hardcoding the URL and record ID as the following, and it was working well (scratching my head....)
PageReference pr = new PageReference('https://c.cs5.visual.force.com/apex/SOF_Declaration?id=a06O000000L9pFMIAZ')
return pr.getContent().toString();
Any idea why is that?
Thanks!
ShashankShashank (Salesforce Developers) 
I  tried with some simple VF and the issue did not get replicated. If you are still faicng thee issue, could you may be post your full code (both the VF pages and their controllers) here so that I can replicate the issue in my test org and check whats going wrong?