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
thecoldfusionthecoldfusion 

Update selected fields

Hello,

I am having a requirement where an apex class should update some of the fields of a given custom object.

=============================================================================================

Here is my setup:

Key Points:
- Page implements tabbed navigation of logical sets of fields for view, edit, save etc.
- Fields a__c through e__c, here, are compacted and represent groups of related fields.


Extended controller:
public AssetController
{
   ...
   ...
   private Id aId;
   private Asset__c asset;
   
   public ControllerA controllerA {public get; private set;}
   public ControllerB controllerB {public get; private set;}
   .
   .
   .
   public ControllerE controllerE {public get; private set;}
   
   public Controller1(ApexPages.StandardController controller)
   {
      aId = ((Asset__c) controller.getRecord()).id;
      this.asset = [SELECT a__c, b__c, c__c, d__c, e__c FROM Asset__c WHERE Id = :this.aId];
      this.controllerA = new ControllerA(this, this.asset);
      this.controllerB = new ControllerB(this, this.asset);
      .
      .
      .
      this.controllerE = new ControllerE(this, this.asset);
}
...
...
...
// this method to be used to make sure records are saved
// when user navigates away from current page
// not used at the moment
private AssetController::save()
{
// to be filled
}
}

Helper classes:
public class ControllerA
{
    private AssetController ac;
    public Asset__c asset {public get; private set;}
    public Integer viewMode {public get; public set;}

public ControllerA(AssetController ac, Asset__c asset) { this.ac = ac; this.asset = asset; this.viewMode = 1; // 1 = view, 2 = edit
// enums would do fantastic job but i can't get enums in vf page :(
}

// called from VF component
public PageReference save() {
// *****************************************
// here, i need to update asset.a__c only... // *****************************************
} }

.
.
.

public class ControllerE
{
private AssetController ac;
public Asset__c asset {public get; private set;}

public ControllerA(AssetController ac, Asset__c asset)
{
this.ac = ac;
this.asset = asset;
this.viewMode = 1; // 1 = view, 2 = edit
// enums would do fantastic job but i can't get enums in vf page :(
}

// called from VF component
public PageReference save()
{
// *****************************************
// here, i need to update asset.e__c only...
// *****************************************
}
}

 
Page: AssetsPage

<apex:page standardController="Asset__c" extensions="AssetController"> <apex:tabPanel switchType="client" selectedTab="tabA"> <apex:tab label="GroupA" name="tabA"> <c:LogicalGroupA controllerA="{!controllerA}" /> </apex:tab> . . . <apex:tab label="GroupE" name="tabE"> <c:LogicalGroupE controllerE="{!controllerE}" /> </apex:tab> </apex:tabPanel> </apex:page>


Component: LogicalGroupA

<apex:component>
    <apex:attribute required="true" type="ControllerA" name="controllerA" description="" />
    <apex:outputPanel id="groupPanel" layout="block">
    <!------ this is where i wanted to use enums for view/edits ------>
<apex:form rendered="{!(controllerA.viewMode == 1)}"> <apex:pageBlock> <apex:pageBlockButtons> <apex:commandButton value="Edit" rerender="groupPanel"> <apex:param value="2" assignTo="{!controllerA.viewMode}" /> </apex:commandButton> </apex:pageBlockButtons> <apex:pageBlockSection> <apex:outputField value="{!controllerA.asset.a__c}" /> </apex:pageBlockSection> </apex:pageBlock> </apex:form>
<!------ this is where i wanted to use enums for view/edits ------>
<apex:form rendered="{!(controllerA.viewMode == 2)}"> <apex:pageBlock> <apex:pageBlockButtons> <apex:commandButton value="Save" rerender="groupPanel" action="{!controllerA.save}"> <apex:param value="2" assignTo="{!controllerA.viewMode}" /> </apex:commandButton> </apex:pageBlockButtons> <apex:pageBlockSection> <apex:inputField value="{!controllerA.asset.a__c}" /> </apex:pageBlockSection> </apex:pageBlock> </apex:form> </apex:component>

same logic for LogicalGroupB through LogicalGroupE

=============================================================================================

If i "update this.asset" in ControllerA::save() method, I get

Error:

System.Exception: DML currently not allowed

Class.ControllerA.save: line __, column __
External entry point


Qs:
1. Is there a work around to the error? Why such exception from action method? Does it have to be in AssetController::save()?
2. How to update partial fields (a__c only), since my custom object (asset) already has fields (b__c,... e__c) in ControllerA through ControllerE?
3. Is there a way to use enums in VF pages/components?
4. A 1 to 1 relation maped object would solve the purpose, I think, since all those 5 fields would come from 5 different custom objects. Is there such provision in salesforce platform? (Other than having a Parent-Child relationship and making sure only one child object will ever be created!)


Regards,
thecoldfusion