[Salesforce]Visualforceアクション

Visualforceアクションを紹介します。

Visualforceコントローラ

public with sharing class lifecycle {

    private final Account acct;
    Integer EmpAdd;

    public lifecycle(myController controller) {
        this.acct = (Account)controller.getAccount();
    }

    public String getGreeting() {
        return acct.name + ' Current Information';
    }
    
    public void resetEmp() {
        acct.numberofemployees = 10;
        update acct;
    }
}

Visualforceページ

<apex:page controller="myController" tabStyle="Account" extensions="lifecycle" action="{!resetEmp}">
    <apex:messages />
    <apex:pageBlock title="{!greeting}">
        <apex:outputLabel value="{!$ObjectType.account.fields.Name.label}: " 
                          for="acctName"/>
        <apex:outputField value="{!account.name}" id="acctName"/>
        <br/>
        <apex:outputLabel 
              value="{!$ObjectType.account.fields.NumberOfEmployees.label}: "
              for="emps"/>
        <apex:outputField value="{!account.NumberOfEmployees}" id="emps"/>
        <br/>
    </apex:pageBlock>
    <apex:pageBlock title="Variable values">
        <c:editMode value="{!$CurrentPage.parameters.key}"/>
    </apex:pageBlock>
    <apex:form rendered="{!$CurrentPage.parameters.key = 'true'}">
        <apex:pageBlock title="Update the Account" id="thePageBlock">
            <apex:pageBlockSection columns="1">
                <apex:inputField id="aName" value="{!account.name}"/>
                <apex:inputField value="{!account.NumberOfEmployees}"/>
                <apex:pageBlockSectionItem>
                    <apex:outputLabel value="{!$ObjectType.account.fields.Industry.label}"
                        for="acctIndustry"/>
                    <apex:actionRegion>
                        <apex:inputField value="{!account.Industry}" id="acctIndustry">
                            <apex:actionSupport event="onchange" rerender="thePageBlock"
                                status="status"/>
                        </apex:inputField>
                    </apex:actionRegion>
                </apex:pageBlockSectionItem>
            </apex:pageBlockSection>
            <apex:pageBlockButtons location="bottom">
                <apex:commandButton action="{!save}" value="Save"/>
                <apex:commandButton action="{!cancel}" value="Cancel" immediate="true"/>
            </apex:pageBlockButtons>
        </apex:pageBlock>
    </apex:form>
</apex:page>

上記のVisualforceページ以下がアクションです。

action="{!resetEmp}"

[Salesforce]StandardController クラス

StandardController クラス紹介します。

Visualforceページコントローラ

public class myControllerExtension {

    private final Account acct;
    
    // The extension constructor initializes the private member
    // variable acct by using the getRecord method from the standard
    // controller.
    public myControllerExtension(ApexPages.StandardController stdController) {
        this.acct = (Account)stdController.getRecord();
    }

    public String getGreeting() {
        return 'Hello ' + acct.name + ' (' + acct.id + ')';
    }
}

Visualforceページ

<apex:page standardController="Account" extensions="myControllerExtension">
    {!greeting} <p/>
    <apex:form>
        <apex:inputField value="{!account.name}"/> <p/>
        <apex:commandButton value="Save" action="{!save}"/>
    </apex:form>
</apex:page>

[Salesforce]Exception

[Salesforce]Exceptionについて紹介します。

getCause:
例外オブジェクトとして例外の原因を返します。

getLineNumber:
例外が発生した箇所の行番号を返します。

getMessage: 
ユーザに表示されるエラーメッセージを返します。

getStackTraceString: 
文字列としてスタック追跡を返します。

getTypeName: 
DMLException、ListException、MathException などの例外種別を返します。

try {
    xxx
} catch(Exception e) {
    System.debug('Exception type: ' + e.getTypeName());
    System.debug('Message: ' + e.getMessage());
    System.debug('Cause: ' + e.getCause());
    System.debug('Line number: ' + e.getLineNumber());
    System.debug('Stack trace: ' + e.getStackTraceString());
}

[Salesforce]Id重複のListをMapに入れることでId重複解消策

Id重複のListをMapに入れることでId重複解消策を紹介します。

Account acc = [SELECT Id, Name FROM Account LIMIT 1];
List<Account> accList = new List<Account>();
accList.add(acc);
accList.add(acc);
System.debug('accList:'+accList);

Map<Id, Account> mapAccIdToAcc = new Map<Id, Account>();
mapAccIdToAcc.putAll(accList);
System.debug('mapAccIdToAcc:'+mapAccIdToAcc);

ログは以下です。

19:07:46:010 USER_DEBUG [97]|DEBUG|accList:(Account:{Id=0016T00002kM9AVQA0, Name=Slテスト名変更}, Account:{Id=0016T00002kM9AVQA0, Name=Slテスト名変更})
19:07:46:010 USER_DEBUG [101]|DEBUG|mapAccIdToAcc:{0016T00002kM9AVQA0=Account:{Id=0016T00002kM9AVQA0, Name=Slテスト名変更}}

[Salesforce]プロセスビルダーでToDoの「期日」項目表示

プロセスビルダーでToDoの「期日」項目表示

Salesforceの画面では「ToDo」で「期日」と表示される項目が、プロセスビルダーでは「期日のみ」と表示されてしまいます。

これは、SalesforceのAPIがこの項目(ActivityDate)に関しては、項目の表示ラベルと異なる値を返すことが原因です。

[Salesforce]オブジェクトのレコードタイプ名からレコードタイプ表示ラベル取得

オブジェクトのレコードタイプ名からレコードタイプ表示ラベル取得例を紹介します。

String RecordTypeDevName = 'testDevName';
String objName = 'testObject__c';

List<RecordType> recordTypeList = [
    SELECT Id
      , Name
      FROM RecordType
      WHERE SobjectType = : objName
      AND DeveloperName = : RecordTypeDevName
  ];

for(RecordType rtobj : recordTypeList){
    system.debug('rtobj.Name:'+rtobj.Name);
}

[Salesforce]オブジェクトAPI参照名からオブジェクト表示ラベル取得

オブジェクトAPI参照名からオブジェクト表示ラベル取得例を紹介します。

    String searchObjName = 'Account';
    Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe();
    for (String sKey : gd.keySet()) {
        Schema.DescribeSObjectResult targetObject = gd.get(sKey).getDescribe();
        if(targetObject.getName() == searchObjName){
			System.debug('targetObject.getLabel()----->targetObject.getName()::::::'+targetObject.getLabel()+'----->'+targetObject.getName());
        }
    }

[Salesforce]オブジェクト表示ラベルからオブジェクトAPI参照名取得

オブジェクトAPI参照名からオブジェクト表示ラベル取得例を紹介します。

    String searchObjLabelName = '取引先';
    Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe();
    for (String sKey : gd.keySet()) {
        Schema.DescribeSObjectResult targetObject = gd.get(sKey).getDescribe();
        if(targetObject.getLabel() == searchObjLabelName){
			System.debug('targetObject.getLabel()----->targetObject.getName()::::::'+targetObject.getLabel()+'----->'+targetObject.getName());
        }
    }