ラベル

ラベル Visualforce の投稿を表示しています。 すべての投稿を表示
ラベル Visualforce の投稿を表示しています。 すべての投稿を表示

2021-09-17

Salesforce - Attachment


添付ファイルに関するメモ書き
  • VFPage から任意のオブジェクトレコードに添付ファイルをアップロードしたい
  • VFPage に任意のオブジェクトレコードの添付ファイル一覧を表示したい(ファイル名のみ)





■ 添付ファイルのアップロード 

VFPage

<apex:form>

	<apex:outputLabel value="ファイル" for="file" />
	<apex:inputFile value="{!attachment.Body}" filename="{!attachment.Name}" id="file" />

	<apex:commandButton value="アップロード" action="{!doUpload}" />

</apex:form>

Apex

public class SampleController {

	private Id recordId;

	public Attachment attachment { get; set; }

	public SampleController(ApexPages.StandardController controller) {

		this.recordId = controller.getId();

		this.attachment= new Attachment();
	}

	public PageReference doUpload() {

		if(this.attachment.Body != null && String.isNotBlank(this.attachment.Name) ) {

			this.attachment.ParentId = this.recordId;

			insert this.attachment;
		}
		return null;
	}
}

■ 添付ファイル一覧の表示 

VFPage

<apex:dataList value="{!attachmentList}" var="attachment">
	<apex:outputText value="{!attachment.Name}"/>
</apex:dataList>

Apex

public class SampleController {

	private Id recordId;

	public List<Attachment> attachmentList { get; set; }

	public SampleController(ApexPages.StandardController controller) {

		this.recordId = controller.getId();

		this.attachmentList = [SELECT Id, Name FROM Attachment WHERE ParentId = :this.recordId ORDER BY CreatedDate ASC];
	}
}


ref:

2021-08-29

Salesforce - Visualforce - apex:selectList


apex:selectList のメモ書き
  • 任意の選択リスト型の入力項目を表示する
※ 2021/09/06: 「SelectOption」のラベルと値がの設定が逆だったため修正





■ select 

VFPage

<apex:selectList label="選択リスト" value="{!option}" size="1" >
        <apex:selectOptions value="{!options}"/>
</apex:selectList>

※ 複数選択リストにする場合は、「apex:selectList コンポーネント」に「multiselect="true"」属性を追加する
※ 「size="1"」表示される選択肢を 1 個にする

Apex

public class SampleController {

	public String option { get; set; }
	public List<SelectOption> options { get; set; }

	public SampleController(ApexPages.StandardController controller) {

		this.option = '';
		this.options = this.getSelectOption(`オブジェクト名`, `選択リストのフィールド名`);

	}

	public List<SelectOption> getSelectOption(Strin sObjectName, String fieldName) {
		List<SelectOption> retval = new List<SelectOption>();
        retval.add(new SelectOption('', '下記から選択してください。', true));	// ラベルを表示・非選択
		Schema.DescribeFieldResult f = Schema.getGlobalDescribe().get(sObjectName).getDescribe().fields.getMap().get(fieldName).getDescribe();
		List<Schema.PicklistEntry> pickList = f.getPicklistValues();
		for(Schema.PicklistEntry pick : pickList) {
			if(pick.isActive()) {
				retval.add(new SelectOption(pick.getValue(), pick.getLabel()));		// ラベルを表示・選択可能
			}
		}
		return retval;
	}

}



ref:

2021-08-27

Salesforce - Visualforce - apex:outputText


apex:outputText のメモ書き
  • 日付型の項目をフォーマット指定して表示する





■ date 

VFPage

<apex:outputText label="日付" value="{0,date,yyyy/MM/dd}" >
	<apex:param value="{!NOW}" />
</apex:outputText>



ref:

Salesforce - Visualforce - apex:input


apex:input のメモ書き
  • 任意の日付型の入力項目を表示する(入力値の最大値・最小値の制限も加える)





■ type : date 

VFPage

<apex:page docType="html-5.0" StandardController="Contact" extensions="SampleController">

<apex:input label="日付" type="date" value="{!sampleDate}" html-min="{!sampleMinDate}" html-max="{!sampleMaxDate}"  />

</apex:page>

※ 「type 属性」を使うには「apex:page コンポーネント」の「docType 属性」を指定する
※ 「html-min 属性」「html-max 属性」はパススルー属性

Apex

public class SampleController {

	public Date sampleDate { get; set; }
	public String sampleMinDate { get; private set; }
	public String sampleMaxDate { get; private set; }

	public SampleController(ApexPages.StandardController controller) {

		this.sampleMinDate = String.valueOf(System.today());
		this.sampleMaxDate = String.valueOf(System.today().addYears(1));

	}
}



ref: