Skip to content

Instantly share code, notes, and snippets.

@dhoechst
Last active August 29, 2015 14:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dhoechst/b437230ffd3d0bd6cd02 to your computer and use it in GitHub Desktop.
Save dhoechst/b437230ffd3d0bd6cd02 to your computer and use it in GitHub Desktop.
Custom Settings Update
<apex:page controller="SuperDuperSettingController">
<apex:pageMessages />
<apex:form >
<apex:pageBlock >
<apex:pageBlockSection >
<apex:pageBlockSectionItem >
<apex:outputLabel >Name</apex:outputLabel>
<apex:inputText value="{!setting.Name}"/>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
<apex:outputLabel >Price Value</apex:outputLabel>
<apex:inputText value="{!setting.PriceValue}"/>
</apex:pageBlockSectionItem>
</apex:pageBlockSection>
<apex:pageBlockButtons >
<apex:commandButton action="{!save}" value="Save" />
</apex:pageBlockButtons>
</apex:pageBlock>
</apex:form>
</apex:page>
public class SuperDuperSettingController {
public SuperDuperSetting__c setting {get; set;}
public SuperDuperSettingController() {
String settingId = ApexPages.currentPage().getParameters().get( 'id' );
if (settingId != null ) {
setting = [SELECT Id, Name FROM SuperDuperSetting__c WHERE Id = :settingId];
} else {
setting = new SuperDuperSetting__c();
}
}
public PageReference save() {
if (setting.Id != null) {
update setting;
} else {
insert setting;
}
PageReference pr = Page.SuperDuperSetting;
pr.getParameters().put('Id', setting.Id);
pr.setRedirect(true);
return pr;
}
}
public class SuperDuperSettingController {
public SettingProxy setting {get; set;}
public SuperDuperSettingController() {
String settingId = ApexPages.currentPage().getParameters().get( 'id' );
if (settingId != null ) {
setting = new SettingProxy([SELECT Id, Name, PriceValue__c FROM SuperDuperSetting__c WHERE Id = :settingId]);
} else {
setting = new SettingProxy();
}
}
public PageReference save() {
if (setting.Id != null) {
update setting.getSuperDuperSetting();
} else {
setting.insertSetting();
}
PageReference pr = Page.SuperDuperSetting;
pr.getParameters().put('Id', setting.Id);
pr.setRedirect(true);
return pr;
}
public class SettingProxy {
public Id Id {get;set;}
public String Name {get;set;}
public String PriceValue {get;set;}
public SettingProxy(SuperDuperSetting__c sdp) {
this.Id = sdp.Id;
this.Name = sdp.Name;
this.PriceValue = sdp.PriceValue__c;
}
public SettingProxy() {}
public SuperDuperSetting__c getSuperDuperSetting() {
return new SuperDuperSetting__c(
Id = this.Id,
Name = this.Name,
PriceValue__c = this.PriceValue
);
}
public void insertSetting() {
SuperDuperSetting__c sdp = this.getSuperDuperSetting();
insert sdp;
this.Id = sdp.Id;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment