0

I have few fieldsets which are collection of checkbox, i want to retrieve their values and save as child records to cases. This is the code, please help me in writing the save action.

Controller

public class ShowDocumentChecklistController {
    private string caseID = '';
    public Account acc { get; set; }
    public Case caseItem { get; set; }
    public DocumentList__c dl { get; set; }


    public ShowDocumentChecklistController() {
        list<case>caseList = new list<case>();
        this.dl = getdl();

        caseID = System.currentPageReference().getParameters().get('caseId');
        if (caseList.isEmpty() == false) {
            caseItem = caseList[0];
        } else {
            ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.ERROR, 'The case ID is invalid.');
            ApexPages.addMessage(msg);

        }
    }

    public List <Schema.FieldSetMember> getFields() {
        return SObjectType.DocumentList__c.FieldSets.Red_Items.getFields();
    }

    private DocumentList__c getdl() {
        String query = 'SELECT ';

        for (Schema.FieldSetMember f: this.getFields()) {
            query += f.getFieldPath() + ', ';
        }

        query += 'Id, Name FROM DocumentList__c where Case__c = :caseID order by LastModifiedDate DESC LIMIT 1';

        return Database.query(query);
    }

    public pageReference saveRecord() {
        DocumentList__c dl = new DocumentList__c(); 

      ? ? ? ?
        insert dl;
        return returnToOrigination();
    }

    public pageReference cancelAction() {
        return returnToOrigination();
    }

    private pageReference returnToOrigination() {
        PageReference page = new PageReference('/' + caseID);
        page.setRedirect(true);
        return page;
    }
}

VF page

 <apex:page controller="ShowDocumentChecklistController">
     <apex:form >
        <apex:sectionHeader title="Document Checklist"/>
        <apex:pageBlock >
           <apex:pageBlockButtons >
           <apex:commandButton value="Cancel" action="{!cancelAction}"/>
           <apex:commandButton value="Save" action="{!saveRecord}"/>
           </apex:pageBlockButtons>
        <apex:pageblocksection>
        <apex:repeat value="{!$ObjectType.DocumentList__c.FieldSets.Green_Items}" var="g">  
                                              <apex:inputfield value="{!documentlist[g]}">  
                                              </apex:inputfield> 
                                          </apex:repeat>
        </apex:pageblocksection>
      </apex:pageBlock>
     </apex:form>
    </apex:page>
2

try this...

public class ShowDocumentChecklistController{


    public DocumentList__c docList{ get; set; }

    public ShowDocumentChecklistController() {
    String caseID = System.currentPageReference().getParameters().get('caseId');

        this.docList= getDocumentList(caseID);
    }

    public List<Schema.FieldSetMember> getFields() {
        return SObjectType.DocumentList__c.FieldSets.Red_Items.getFields();
    }

    private DocumentList__c getDocumentList(String caseID) {


        String query = 'SELECT ';
        for(Schema.FieldSetMember f : this.getFields()) {
            query += f.getFieldPath() + ', ';
        }
        query += 'Id, Name FROM DocumentList__c where Case__c = :caseID order by LastModifiedDate DESC LIMIT 1';
        return Database.query(query);
    }

    public pageReference saveRecord(){

        update docList;
    }

}

  <apex:pageBlock title="abc">
      <apex:pageBlockSection title="abcd">
          <apex:inputField value="{!docList.Name}"/>
      </apex:pageBlockSection>

      <apex:pageBlockSection title="xyz">
          <apex:repeat value="{!fields}" var="f">
              <apex:inputField value="{!docList[f.fieldPath]}"/>
          </apex:repeat>
      </apex:pageBlockSection>

    </apex:pageBlock>

</apex:form>  

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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