You need to sign in to do that
Don't have an account?
How to prefill a custom object?
Hi all,
I have a problem where i am trying to prefill a custom object whenever the Edit Button is clicked.
Here is my Controller Code. I am instantiating object in the constructor class. I pass the ID of the object through the URL:
public class newScoreRuleController { // Declarations public List<ScoringRule__c> ScoringRule { get; private set;} public List<MappingRule__c> mappingRule {get; private set;}public newScoreRuleController () { // Give me blank lines of my Object String op = ApexPages.currentPage().getParameters().get('op'); ScoringRule = new List<ScoringRule__c>(); for (integer i = 0; i < 8; i++) { ScoringRule.add( new ScoringRule__c() ); } if (op!='edit') { MappingRule = new List<MappingRule__c>(); for (integer i = 0; i < 4; i++) { MappingRule.add( new MappingRule__c() ); } } else {// Edit Operation chosen .. So prefill it MappingRule = new List<MappingRule__c>(); MappingRule.add([Select rating__c, ScoreVal2__c, ScoreVal1__c, ScoreOperator2__c, ScoreOperator1__c, RuleSetId__c, Mapping_Field__c From mappingRule__c n where RuleSetId__c =: getRuleSetId()]); //MappingRule = mp; }}
// Get me the Rule Set ID
public String getRuleSetId() {
String ruleId = ApexPages.currentPage().getParameters().get('id');
return ruleId;
}
}
Here is my Visualforce Code:
<apex:dataTable value="{!mappingRule}" var="mr"> <apex:column > <apex:facet name="header">Score</apex:facet> <apex:outputText value="Score"/> </apex:column> <apex:column > <apex:facet name="header">Operator</apex:facet> <apex:inputField value="{!mr.ScoreOperator1__c}"/> </apex:column> <apex:column > <apex:facet name="header">Score Value 1</apex:facet> <apex:inputText value="{!mr.ScoreVal1__c}"/> </apex:column> <apex:column > <apex:facet name="header">Operator</apex:facet> <apex:inputField value="{!mr.ScoreOperator2__c}"/> </apex:column> <apex:column > <apex:facet name="header">Score Value 2</apex:facet> <apex:inputText value="{!mr.ScoreVal2__c}"/> </apex:column> <apex:column > <apex:facet name="header">Rating</apex:facet> <apex:inputField value="{!mr.rating__c}"/> </apex:column> </apex:dataTable>
However it doesnt prefill the Object. It doesnt complain either. So i really dont know whats happening?
Any help is appreciated!!!
Regards
Mukul
Ok! So i was able to fix it. It seems like the Constructor was not calling the Set Method.
I created seperate get and set methods and it worked!
Regards
Mukul
All Answers
Hi Mukul,
Is it possible that the query [Select rating__c, ScoreVal2__c, ScoreVal1__c, ScoreOperator2__c, ScoreOperator1__c, RuleSetId__c, Mapping_Field__c From mappingRule__c n where RuleSetId__c =: getRuleSetId()] isn't returning any rows?
- A J
Hi successforce,
Thanks for replying.
It returns rows. I have tried printing it out to the debug log.
Regards
Mukul
Anyone can help me on this issue??
Its very urgent.
Please help me!!
Hi there,
You could try a couple of things to debug the problem. Remove the 'private' access modifier for the setter methods.
Place the code in newScoreRuleController () method in a try - catch block and see if it throws an exception.
Add a system.debug method in the else block to display the contents of MappingRule variable.
Let me know the outcome of these steps.
-A J
Hi successforce,
I removed the private from the Set Method. I have tried putting debug statements but it seems to return the right thing in the debug log. However it doesnt prefill the object in the VF Page. :-(
Here is my modified code:
public List<MappingRule__c> mappingRule {get; set;}
// Constructor Method
public newScoreRuleController () {
// Give me blank lines of my Object
try {
String op = ApexPages.currentPage().getParameters().get('op');
if (op!='edit') {
MappingRule = new List<MappingRule__c>();
for (integer i = 0; i < 4; i++) {
MappingRule.add( new MappingRule__c() );
System.debug('HERE');
}
}
else {
// Prefill it
MappingRule = [select rating__c, ScoreVal2__c, ScoreVal1__c,
ScoreOperator2__c, ScoreOperator1__c, RuleSetId__c,
Mapping_Field__c From mappingRule__c n where RuleSetId__c =: getRuleSetId()];
System.debug('mapping rile: '+ MappingRule);
// for (MappingRule__c m : MappingRule) {
// System.debug('Score val1: ' + m.ScoreVal1__c);
// }
}
} catch (Exception ex) {
System.debug(ex.getMessage());
ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Query Error: ' + ex.getMessage()));
// return null;
}
}
Ok! So i was able to fix it. It seems like the Constructor was not calling the Set Method.
I created seperate get and set methods and it worked!
Regards
Mukul
Nice work!
it's a bit strange that you had to do it that way, but if it works go with it.