[Salesforce]Get Object API reference name and Prefix from object label

    String searchObjLabelName = '取引先企業';
	String targetObjectName = '';

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

    String objName = targetObjectName;	
    schema.sObjectType sObjType = Schema.getGlobalDescribe().get(objName);
    system.debug('prefix:'+(sObjType.getDescribe().getKeyPrefix()));

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

    String searchObjLabelName = '取引先企業';
	String targetObjectName = '';

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

    String objName = targetObjectName;	
    schema.sObjectType sObjType = Schema.getGlobalDescribe().get(objName);
    system.debug('prefix:'+(sObjType.getDescribe().getKeyPrefix()));

[Salesforce][ENG]Get Prefix from ObjectName

Share to get the Prefix from Salesforce’s ObjectName.

Execute the following on the “Open Execute Anonymous Window” screen of the “Debug” tab on the development console screen.

    String objName ='Account';	
    schema.sObjectType sObjType = Schema.getGlobalDescribe().get(objName);
    system.debug('prefix:'+(sObjType.getDescribe().getKeyPrefix()));

The DebugLog result is below.

prefix:001

[Salesforce][ENG]Get object API reference name from object label

Here is an example of getting an object display label from an object API reference name.

    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());
        }
    }

[Salesforce]リストビューで表示されたレコードを一括処理

リストビューで表示されたレコードを一括してコピー例を紹介します。

コントローラ

public with sharing class CustomAccountSetController {
    public List<Account> selectedAccs {get;set;}
    public CustomAccountSetController(ApexPages.StandardSetController controller) {
        List<Account> accs = controller.getSelected();
        this.selectedAccs = [SELECT Id, Name FROM Account WHERE Id IN :(new Map<Id, Account>(accs)).keySet()];
    }
    public void execute() {
   system.debug('execute');
    }
}

Visualforceページ

<apex:page standardController="Account" recordSetvar="accs" extensions="CustomAccountSetController">
    <apex:form>
        <apex:pageBlock title="選択済み取引先" >
            <apex:pageBlockButtons location="bottom">
                <apex:commandButton action="{!execute}" value="実行" oncomplete="history.back();" />
            </apex:pageBlockButtons>
            <apex:pageBlockTable value="{!selectedAccs}" var="acc">
                <apex:column value="{!acc.Name}"/>
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>

カスタムボタン

取引先リストビュー

[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>