Memos About Salesforce

Salesforceにハマってたこと!

Apex 曜日取得 曜日判定

こんにちは、管理人の@Salesforce.Zです。

画面開発だけはないと思いますが、あるデータに基づてい、計算を行い、という設定自動がよくあることです。

今回、日付をメソッドに渡し、土日の判定を行うことを共有します。

読んだら得ること

★ APEXでの土日の判定

目次

APEXでの土日の判定

Salesforceの組織設定で、祝日(Holidya)を設定し、APEX側で取得し判定にも使えます。

サンプルAPEX

public with sharing class UtilDate {
    public static List<String> yobiList = new List<String>{'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'};
    /*
     *@説明 曜日判定
     *@vdDate 判定したい日付
     *@return  {Sun,Mon,Tue,Wed,Thu,Fri,Sat}のうちの一つ
     */
    public static String getYobi(Date vdDate){
        datetime tDate = datetime.newInstance(vdDate.year(), vdDate.month(), vdDate.day());
        String yobi = tDate.format('E');
        return yobi;
    }
    public static String getYobi(Date vdDate){
        datetime tDate = datetime.newInstance(vdDate.year(), vdDate.month(), vdDate.day());
        String week = tDate.format('E');
        if(week == 'Sun'){
            return '日';
        }else if(week =='Mon'){
            return '月';
        }else if(week =='Tue'){
            return '火';
        }else if(week =='Wed'){
            return '水';
        }else if(week =='Thu'){
            return '木';
        }else if(week =='Fri'){
            return '金';
        }else if(week =='Sat'){
            return '土';
        }
        return null;
    }
    public static Date setResult(Datetime targetDT, Integer plusMinusMonth){
        Date caclDate =  addMonths(targetDT, plusMinusMonth);
        Date setDate = caclDate;
        setDate = processSatSun(caclDate);
        // 祝日の場合、直前の営業日を設定する
        while (isHoliday(setDate, holidayList)){
            setDate = setDate.addDays(SUB_1_DAY);
            // 設定後に土日の可能性がある
            setDate = processSatSun(setDate);
        }
        return setDate;
    }

    public static Date processSatSun(Date targetDate){
        Date setResult = targetDate;
        // 土日の場合、直前の営業日「金曜日」を設定
        if(getYobi(targetDate) == yobiList[SUNDAY_INDEX]){
            setResult = targetDate.addDays(SUB_2_DAY);
        }
        // 土日の場合、直前の営業日「金曜日」を設定
        if(getYobi(targetDate) == yobiList[SATURDAY_INDEX]){
            setResult = targetDate.addDays(SUB_1_DAY);
        }
        return setResult;
    }

    /*
     *@説明 ±月の同日を設定
     *      引数「d」がdate型の場合に事前dateToDTメソッドで変換する
     *@return  Date 月を加算し、元日付と同じ日
     */
    public static Date addMonths(Datetime d, Integer m) {
        Date resultDT = NULL;
        if (d != null) {
            Date dDate = d.dateGMT();
            resultDT = dDate.addMonths(m);
        }
        return resultDT;
    }
}

How To Use

APEXクラス

public with sharing class VF_ctrl {
    public Contact con {get;set;}
    public VF_ctrl() {
        this.con = new con();
    }

    public void setDate(){
        // inputDatetimeField__cは取引先責任者に用意した項目
        this.con.Birthdate = UtilDate.setResult(UtilDate.addMonths(con.inputDatetimeField__c), 1);
    }
}

VF側

<apex:page id="page_id" sidebar="false" controller="VF_ctrl" >
    <apex:form id="form_id">
        <apex:pageBlock title="test" id="pbid">
            <apex:pageBlockSection title="sectionName" columns="1" id="basicid">
                <apex:pageBlockSectionItem >
                        <apex:outputLabel value="入力" for="inputdataId" />
                        <apex:actionRegion>
                        <apex:inputField value="{!con.inputDatetimeField__c}" id="inputdataId" >
                            <apex:actionSupport event="onchange" action="{!autoSetFieldValue}" reRender="autosetId"/>
                        </apex:inputField>
                        </apex:actionRegion>
                </apex:pageBlockSectionItem>
                <apex:pageBlockSectionItem >
                        <apex:outputLabel value="{!$ObjectType.Contact.Fields.Birthdate.Label}" for="autosetId" />
                        <apex:outputText value="{!con.Birthdate}" id="autosetId" />
                </apex:pageBlockSectionItem>
            </apex:pageBlockSection
        </apex:pageBlock>
    </apex:form
</apex:page

最後に

今回、apex:actionRegionタグとapex:actionSupport タグを使い、画面上で、随時に自動入力結果を反映できるように しています。