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
pbattissonpbattisson 

Issue when attempting to pass values between controllers

Hey

 

I have a VF page with a VF component on it. The component has an attribute which is of the page controllers type. I am attempting to pass the page controller into the component controller and then have the component controller populate a value on the page controller. 

 

The Component is to allow the adding of an attachment to an object and the object is referenced on the page's controller:

 

Component Markup:

 

<apex:component controller="UploadComponentController" >
	<apex:attribute name="pageController" description="This is the controller of the page which the component is on." type="UploadPageController" required="true" assignTo="{!pageController}"/>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.pack.js"></script>
<script type="text/javascript" >
	$(document).ready(function() {
		
	if($('div.trigger').length > 0) {
		$('div.trigger').click(function() {
			if ($(this).hasClass('open')) {
				$(this).removeClass('open');
				$(this).addClass('close');
				$(this).next().slideDown(200);
				return false;
			} else {
				$(this).removeClass('close');
				$(this).addClass('open');
				$(this).next().slideUp(200);
				return false;
			}			
		});
	}
	
});
</script>
    <apex:form enctype="multipart/form-data">
      <div class="trigger open"><a href="#">Upload</a></div> 
			<div class="cnt"> 
				    <apex:pageBlock >
				 
				      <apex:pageBlockSection showHeader="false" columns="2" id="block1">
				 
				        <apex:pageBlockSectionItem >
				          <apex:outputLabel value="File Name" for="fileName"/>
				          <apex:inputText value="{!attachment.name}" id="fileName"/>
				        </apex:pageBlockSectionItem>
				 
				        <apex:pageBlockSectionItem >
				          <apex:outputLabel value="File" for="file"/>
				          <apex:inputFile value="{!attachment.body}" filename="{!attachment.name}" id="file"/>
				        </apex:pageBlockSectionItem>
				 
				        <apex:pageBlockSectionItem >
				          <apex:outputLabel value="Description" for="description"/>
				          <apex:inputTextarea value="{!attachment.description}" id="description"/>
				        </apex:pageBlockSectionItem>
				 
				      </apex:pageBlockSection>
				 
				    </apex:pageBlock>
	</div> 
    </apex:form>
 
    <style type="text/css">
	    .trigger,.trigger a {
			display: block;
			width: 60px;
			height: 20px;
			text-indent: -999999em;
			overflow: hidden;
	    }
  		.trigger {
			background: url({!$Resource.Button}) no-repeat 0px 0px;
		}
		.close {
			background: url({!$Resource.Button}) no-repeat 0px -20px;
		}
		.cnt {
			display: none;
			padding: 10px;
			margin: 10px;
			background: #f9f9f9;
		}
    </style>
</apex:component>

 

 

Component Controller:

 

public with sharing class UploadComponentController 
{
	public UploadPageController pageController {get; set;}
	
	
	public Attachment getAttachment()
	{
		return pageController.attach;
	}
	
	public void setAttachment(Attachment a)
	{
		pageController.attach = a;
	}
	
}

 

 

Page Markup:

 

 

<apex:page controller="UploadPageController">
	<apex:pageBlock title="New TestObject">
	<apex:pageMessages />
 		<apex:pageBlockSection >
 			<apex:form >
				<apex:outputLabel value="Object Name" for="objName"/>
				<apex:inputText value="{!name}" id="objName" />
			</apex:form>
		</apex:pageBlockSection>
		<apex:pageBlockSection >
			<c:uploadComponent pageController="{!controller}" />
		</apex:pageBlockSection>
		<apex:pageBlockSection >
			<apex:form >
				<apex:commandButton action="{!save}" value="Save"/>
			</apex:form>
		</apex:pageBlockSection>
	</apex:pageBlock>  
</apex:page>

 

 

Page Controller:

 

public with sharing class UploadPageController
{ 
	public Attachment attach {get; set;}
	
  	private TestObject__c m_testObj = new TestObject__c();
  		
  	public UploadPageController()
  	{
  		attach = new Attachment();
  	}
  		
	public string getName()
	{
		return m_testObj.Name; 
	}
	
	public void setName(string oName)
	{
		m_testObj.Name = oName; 
	} 
	
	public UploadPageController getController()
	{
		return this;
	}
	
	public void setController(UploadPageController con)
	{
		
	}
	
	public PageReference save()
	{
		insert m_testObj;
 		
    	attach.OwnerId = UserInfo.getUserId();
    	attach.ParentId = m_testObj.Id; // the record the file is attached to
    	attach.IsPrivate = false;
 		
    	try 
    	{
      		insert attach;
    	} 
    	catch (DMLException e) 
    	{
      		ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error uploading attachment'));
      		return null;
    	}
    	finally 
    	{
      		attach = new Attachment(); 
    	}
 
    	ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,'Attachment uploaded successfully'));
    	
		PageReference page = new ApexPages.StandardController(m_testObj).view();
		page.setRedirect(true);
		return page;
	}
}

 

 

I get an error of "Cannot insert attachment, fields missing : File Name, Body" or somethng to that affect. Any ideas? I can't see why this isn't working!

 

Thanks 

 

Paul

Pradeep_NavatarPradeep_Navatar

In error message it is clearly showing that "Cannot insert attachment, fields missing : File Name, Body", means File Name, Body is the required field in attachment so you have to give these fields in your code before insert the attachment. You have to give these fields to insert an attachment :

 

            transient Attachment a = new Attachment();

            a.body = filebody;

            a.name = filename;

            a.parentid = parentid;

            insert a;

pbattissonpbattisson

Pradeep

 

If you look on the component's controller they should be set on the page controller's attachment and be passed back to this attachment. When the save method is called it then updates this attachment to populate the parent id and some other fields not populated by the component. The component should populate the filename and body fields (when run as a standalone VF page it works fine, it seems to be the communication between itself as a comoponent and a master page that is the issue).

 

Paul

Marty Y. ChangMarty Y. Chang

I think the solution in the following thread will help you.

 

custom component updating main controller?

 

The documentation article the solution refers to is this one: Controller Component Communication.