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
RavitejaRaviteja 

Update dynamic query Results

HI, 

    This the example of using fieldset in apex, given in salesforce documentation.I want some enhancement to this code is that to update the values using different button.Working with Field Sets Using Apex

 

class:

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

public class MerchandiseDetails {

public Merchandise__c merch { get; set; }

public MerchandiseDetails() {
this.merch = getMerchandise();
}

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

private Merchandise__c getMerchandise() {
String query = 'SELECT ';
for(Schema.FieldSetMember f : this.getFields()) {
query += f.getFieldPath() + ', ';
}
query += 'Id, Name FROM Merchandise__c LIMIT 1';
return Database.query(query);
}
}

 

Vf:

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

<apex:page controller="MerchandiseDetails">
<apex:form >

<apex:pageBlock title="Product Details">
<apex:pageBlockSection title="Product">
<apex:inputField value="{!merch.Name}"/>
</apex:pageBlockSection>

<apex:pageBlockSection title="Dimensions">
<apex:repeat value="{!fields}" var="f">
<apex:inputField value="{!merch[f.fieldPath]}"
required="{!OR(f.required, f.dbrequired)}"/>
</apex:repeat>
</apex:pageBlockSection>

</apex:pageBlock>

</apex:form>
</apex:page>

 

 

I want one button in vf  to update these values.

 

Thanks.....

Dhaval PanchalDhaval Panchal

Use Below codes:

 

VF Page:

 

<apex:page standardController="Merchandise__c" extensions="MerchandiseDetails">
	<apex:pageMessages/>
    <apex:form >
      <apex:pageBlock title="Product Details">
		  <apex:pageBlockButtons>
			   <apex:commandButton value="Save" action="{!Save}"/>
		  </apex:pageBlockButtons>
          <apex:pageBlockSection title="Product">
              <apex:inputField value="{!merch.Name}"/>
          </apex:pageBlockSection>
      
          <apex:pageBlockSection title="Dimensions">
              <apex:repeat value="{!fields}" var="f">
                  <apex:inputField value="{!merch[f.fieldPath]}" 
                      required="{!OR(f.required, f.dbrequired)}"/>
              </apex:repeat>
          </apex:pageBlockSection>
  
        </apex:pageBlock>

    </apex:form>  
</apex:page>

 

Controller:

 

public class MerchandiseDetails {

    public Merchandise__c merch { get; set; }
    
    public MerchandiseDetails() {
        this.merch = getMerchandise();
    }

    public List<Schema.FieldSetMember> getFields() {
        return SObjectType.Merchandise__c.FieldSets.Dimensions.getFields();
    }
	
	Public PageReference Save(){
		
		try{
			Update this.merch;
			PageReference P = new PageReference('/' + ApexPages.currentPage().getParameters().get('Id'));
			return P;
		}
		catch(Exception ex){
			ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,ex.getMessage()));
			return null;
		}
	}
	
    private Merchandise__c getMerchandise() {
        String query = 'SELECT ';
        for(Schema.FieldSetMember f : this.getFields()) {
            query += f.getFieldPath() + ', ';
        }
        query += 'Id, Name FROM Merchandise__c where id=\'' + ApexPages.currentPage().getParameters().get('Id') + '\'';
        return Database.query(query);
    }
}

 

Now go to Merchandise__c object, create new Button (Detail page button and select VF page created above).

Use this button on your page layout.