Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

I have a very simple page

<apex:page controller="myController1" >
<c:compo value="hello"></c:compo>
</apex:page>

with an empty controller

public with sharing class myController1 {
}

Component compo is

<apex:component controller="compocontroller">
<apex:attribute name="value" type="string" description="tu" assignTo="{!valueclassvar}"/>
</apex:component>

Its controller is

public class compocontroller

{
public  string valueclassvar{
get{ 
     system.debug('getmethodvalueclassvar');
     return valueclassvar;
   }
set{ 
    system.debug('setmethodvalueclassvar');
   }

 }

 }

When i check the debug log i can see setter gets executed twice.does anyone know why. I know the reason why its executed first(because of assignto attribute in component..but why its getting executed for second time..anyone any clue?

Debug log is

30.0 APEX_CODE,DEBUG;APEX_PROFILING,INFO;CALLOUT,INFO;DB,INFO;SYSTEM,DEBUG;VALIDATION,INFO;VISUALFORCE,INFO;WORKFLOW,INFO
04:11:17.018 (18766000)|EXECUTION_STARTED
04:11:17.018 (18807000)|CODE_UNIT_STARTED|[EXTERNAL]|066i0000004EBS1|VF: /apex/orderofexe
04:11:17.034 (34126000)|CODE_UNIT_STARTED|[EXTERNAL]|01pi0000005trqA|compocontroller <init>
04:11:17.034 (34148000)|SYSTEM_MODE_ENTER|true
04:11:17.034 (34726000)|METHOD_ENTRY|[1]|01pi0000005trqA|compocontroller.compocontroller()
04:11:17.034 (34740000)|METHOD_EXIT|[1]|compocontroller
04:11:17.034 (34815000)|CODE_UNIT_FINISHED|compocontroller <init>
04:11:17.034 (34952000)|CODE_UNIT_STARTED|[EXTERNAL]|compocontroller set(valueclassvar,hello)
04:11:17.034 (34962000)|SYSTEM_MODE_ENTER|true
04:11:17.034 (34972000)|CODE_UNIT_STARTED|[EXTERNAL]|compocontroller set(valueclassvar,hello)
04:11:17.035 (35103000)|SYSTEM_METHOD_ENTRY|[10]|System.debug(ANY)
04:11:17.035 (35145000)|USER_DEBUG|[10]|DEBUG|setmethodvalueclassvar
04:11:17.035 (35154000)|SYSTEM_METHOD_EXIT|[10]|System.debug(ANY)
04:11:17.035 (35168000)|CODE_UNIT_FINISHED|compocontroller set(valueclassvar,hello)
04:11:17.035 (35177000)|CODE_UNIT_FINISHED|compocontroller set(valueclassvar,hello)
04:11:17.038 (38362000)|CODE_UNIT_STARTED|[EXTERNAL]|01pi0000005tsxw|myController1 <init>
04:11:17.038 (38382000)|SYSTEM_MODE_ENTER|true
04:11:17.038 (38425000)|METHOD_ENTRY|[1]|01pi0000005tsxw|myController1.myController1()
04:11:17.038 (38437000)|SYSTEM_MODE_ENTER|false
04:11:17.038 (38444000)|SYSTEM_MODE_EXIT|false
04:11:17.038 (38452000)|METHOD_EXIT|[1]|myController1
04:11:17.038 (38471000)|SYSTEM_MODE_ENTER|false
04:11:17.038 (38483000)|SYSTEM_MODE_EXIT|false
04:11:17.038 (38488000)|SYSTEM_MODE_ENTER|false
04:11:17.038 (38491000)|SYSTEM_MODE_EXIT|false
04:11:17.038 (38501000)|CODE_UNIT_FINISHED|myController1 <init>
04:11:17.062 (62475000)|CODE_UNIT_STARTED|[EXTERNAL]|compocontroller set(valueclassvar,hello)
04:11:17.062 (62498000)|SYSTEM_MODE_ENTER|true
04:11:17.062 (62520000)|CODE_UNIT_STARTED|[EXTERNAL]|compocontroller set(valueclassvar,hello)
04:11:17.062 (62622000)|SYSTEM_METHOD_ENTRY|[10]|System.debug(ANY)
04:11:17.062 (62635000)|USER_DEBUG|[10]|DEBUG|setmethodvalueclassvar
04:11:17.062 (62643000)|SYSTEM_METHOD_EXIT|[10]|System.debug(ANY)
04:11:17.062 (62659000)|CODE_UNIT_FINISHED|compocontroller set(valueclassvar,hello)
04:11:17.062 (62671000)|CODE_UNIT_FINISHED|compocontroller set(valueclassvar,hello)
04:11:17.342 (66472000)|CUMULATIVE_LIMIT_USAGE
share|improve this question

2 Answers 2

up vote 3 down vote accepted

This is documented behaviour - see:

http://www.salesforce.com/us/developer/docs/pages/Content/pages_controller_methods.htm

In the setter section, it states (bold is mine):

It’s a best practice for setter methods to be idempotent, that is, to not have side effects. For example, don’t increment a variable, write a log message, or add a new record to the database. Visualforce doesn’t define the order in which setter methods are called, or how many times they might be called in the course of processing a request. Design your setter methods to produce the same outcome, whether they are called once or multiple times for a single page request.

share|improve this answer
    
Thanks Bob..Can you please see this query as well,salesforce.stackexchange.com/questions/34787/… documentation related to that particular question states otherwise..Not sure whether its me following document wrongly :( –  sfdc99999 May 10 at 17:00

Maybe this document will help?

"After custom components are created, all assignTo attributes on those custom components are executed. The assignTo method sets selectedValue on the attribute to the value attribute. The value attribute is set to false, so selectedValue is set to false."

http://www.salesforce.com/us/developer/docs/pages/Content/pages_controller_lifecycle_example.htm

share|improve this answer
    
as stated in question--i can understand the reason why setter is called for first time(same logic as this document says,assignto reasons)..My specific question is why its executed second time –  sfdc99999 May 10 at 16:42
    
From the logs it looks like 1. On component initialization and 2 on page initialization. Thats how I read it. If I am wrong someone will be along shortly to correct me...Interested to see what the answer is. –  Eric May 10 at 18:12

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.