共計(jì) 15222 個(gè)字符,預(yù)計(jì)需要花費(fèi) 39 分鐘才能閱讀完成。
本篇內(nèi)容主要講解“Apex 和 Database 相關(guān)知識(shí)點(diǎn)有哪些”,感興趣的朋友不妨來(lái)看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓丸趣 TV 小編來(lái)帶大家學(xué)習(xí)“Apex 和 Database 相關(guān)知識(shí)點(diǎn)有哪些”吧!
salesforce.com 是世界上第一個(gè)提出云計(jì)算平臺(tái)的公司, 同時(shí), 它還引入了世界上第一門云計(jì)算編程語(yǔ)言 Apex。
1. Get Started with Apex
Learning Objectives
完成本單元后,您將能夠:
描述 Apex 編程語(yǔ)言的主要功能。
保存一個(gè) Apex 類并使用 Anonymous.Apex 調(diào)用方法。
使用開(kāi)發(fā)者控制臺(tái)檢查調(diào)試日志。
Apex 的特點(diǎn):
代碼托管 - 在服務(wù)器 Lightning Platform 上保存,編譯和執(zhí)行 Apex。
Apex 是不區(qū)分大小寫的語(yǔ)言
Apex 命名規(guī)范(這里我用的是 Java 的規(guī)范)
類名首字母大寫,如果類名由多個(gè)單詞組成,每個(gè)單詞的首字母都要大寫。如:
public class MyFirstClass{}
變量名、方法名首字母小寫, 如果名稱由多個(gè)單詞組成,除第一個(gè)單詞外,其他每個(gè)單詞的首字母都要大寫。如:
int index=0;
public void toString(){}
常量名全部大寫
Apex 支持以下數(shù)據(jù)類型:
Primitive (Integer, Double, Long, Date, Datetime, String, ID, or Boolean) 原始數(shù)據(jù)類型(整數(shù),雙精度,長(zhǎng)整型,日期,日期時(shí)間,字符串,ID 或布爾值)
Collections (Lists, Sets and Maps)
sObject
這是 Salesforce 中的特殊數(shù)據(jù)類型。
它類似于 SQL 中的表,并且包含與 SQL 中的列類似的字段。
有兩種類型的 sObjects:Standard 和 Custom。
例如,Account 是一個(gè)標(biāo)準(zhǔn)的 sObject ; 任何其他用戶定義的對(duì)象(如我們創(chuàng)建的 Customer 對(duì)象)是 Custom sObject。
Enums 枚舉
Classes, Objects and Interfaces 類,對(duì)象和接口
Reference:
Apex 數(shù)據(jù)類型 -W3School
Trailhead Apex Practice https://trailhead.salesforce.com/content/learn/modules/apex_database/apex_database_intro
Create an Apex class with a method that returns an array (or list) of strings. Create an Apex class with a method that returns an array (or list) of formatted strings (Test 0 , Test 1 , …). The length of the array is determined by an integer parameter.
The Apex class must be called StringArrayTest and be in the public scope
The Apex class must have a public static method called generateStringArray
The generateStringArray method must return an array (or list) of strings
The method must accept an incoming Integer as a parameter, which will be used to determine the number of returned strings
The method must return a string value in the format Test n where n is the index of the current string in the array
/*
Apply To: Trailhead Apex Practice
Created On: 05-14-2021
Function:
Developer Date Version Description
-------------------------------------------------
charles 05-14-2021 V1.0 Initial Version
//apex class
class StringArrayTest{
//Apex method that takes in an Integer parameter and returns a List of Strings
public static void generateStringArray(Integer n){
//Create a List of Strings
List String result = new List String
//Interate through a for loop to populate the array of Strings
for(Integer i=0; i i++){ result.add( Test +i);
System.debug(result[i]);
return result;
}
In the Developer Console, click Debug | Open Execute Anonymous Window.
In the window that opens, enter the following.
StringArrayTest.generateStringArray(5);
In the sample code above, you create an Apex class called StringArrayTest. You create a method called generateStringArray which takes in an Integer parameter and returns a List of Strings. If you fully understand the code above, then you should be able to complete this challenge with a breeze. However, if you re not too familar with the code above, I d recommend going through to the tutorial pages I linked above and read through to learn Apex first. I wish you best of luck and continue your journey to become Platform Dev Certified!
2. Use sObjects
Salesforce 中的每條記錄 (行) 都在 Apex 中本地表示為 sObject。Salesforce 中的標(biāo)準(zhǔn)和自定義對(duì)象記錄映射到 Apex 中的 sObject 類型。以下是 Apex 中用于標(biāo)準(zhǔn)對(duì)象的一些常見(jiàn) sObject 類型名稱。
Account
Contact
Lead
Opportunity
創(chuàng)建一個(gè) sObject, 您需要聲明一個(gè)變量, 并將其分配給一個(gè) sObject 實(shí)例。變量的數(shù)據(jù)類型是 sObject 類型。以下示例創(chuàng)建一個(gè)類型為 Account 的 sObject 變量,并將其分配給名稱為 charles 的新帳戶。
Account acct = new Account(Name= charles
sObject 和字段名
對(duì)于自定義對(duì)象和自定義字段,API 名稱始終以__c 后綴結(jié)尾。對(duì)于自定義關(guān)系型 (Relationship) 字段,API 名稱以__r 后綴結(jié)尾。例如:
標(biāo)簽為 Merchandise 的自定義對(duì)象的 API 名稱為 Merchandise__c。
標(biāo)有 Description 標(biāo)簽的自定義字段的 API 名稱為 Description__c。
標(biāo)有 Items 標(biāo)簽的自定義關(guān)系型字段的 API 名稱為 Items__r。
創(chuàng)建 sObjects 并添加字段
在插入 Salesforce 記錄之前,必須首先在內(nèi)存中將其創(chuàng)建為 sObject。與其他任何對(duì)象一樣,sObject 是使用 new 運(yùn)算符創(chuàng)建的:
ccount acct = new Account();
API 對(duì)象名稱為 Apex 中 sObject 變量的數(shù)據(jù)類型。在此示例中,Account 是 acct 變量的數(shù)據(jù)類型。acct 變量引用的 Account 為空,因?yàn)槲覀兩形刺畛淦淙魏巫侄巍S袃煞N添加字段的方法:通過(guò)構(gòu)造函數(shù)或使用. 表示法。
添加字段的最快方法是在構(gòu)造函數(shù)中將它們指定為“名稱 / 值”對(duì)。例如,此語(yǔ)句創(chuàng)建一個(gè)新帳戶 sObject,并使用字符串值填充其“名稱”字段。
Account acct = new Account(Name= charles
Name 字段是帳戶唯一必填的字段,這意味著必須先填充該字段,然后才能插入新記錄。但是,您也可以為新記錄填充其他字段。此示例還添加了電話號(hào)碼和員工人數(shù)。
Account acct = new Account(Name= charles
, Phone= (415)555-1212 , NumberOfEmployees=10000);
或者,您可以使用點(diǎn)表示法將字段添加到 sObject。以下內(nèi)容與前面的示例等效,盡管它需要花費(fèi)更多的代碼行。
Account acct = new Account();
acct.Name = charles
acct.Phone = (415)555-1212
acct.NumberOfEmployees = 10000;
通常,在使用 sObjects 時(shí),您使用特定的 sObject 數(shù)據(jù)類型,例如,標(biāo)準(zhǔn)對(duì)象使用 Account 或 Book 定制對(duì)象使用 Book__c。但是,當(dāng)您不知道方法要處理的 sObject 類型時(shí),可以使用通用的 sObject 數(shù)據(jù)類型。
使用通用 sObject 數(shù)據(jù)類型聲明的變量可以引用任何 Salesforce 記錄,無(wú)論是標(biāo)準(zhǔn)記錄還是自定義對(duì)象記錄。
這個(gè)例子顯示了如何通用 sObject 變量可以分配給任何 Salesforce 對(duì)象: 一個(gè)名為 Book__c 的帳戶和一個(gè)自定義對(duì)象。
sObject sobj1 = new Account(Name= Trailhead
sObject sobj2 = new Book__c(Name= Workbook 1
相反,使用特定 sObject 數(shù)據(jù)類型聲明的變量只能引用相同類型的 Salesforce 記錄。
在處理通用 sObject 時(shí),有時(shí)需要將 sObject 變量轉(zhuǎn)換為特定的 sObject 類型。這樣做的好處之一是能夠使用點(diǎn)符號(hào)來(lái)訪問(wèn)字段,而點(diǎn)符號(hào)在通用 sObject 上不可用。由于 sObject 是所有特定 sObject 類型的父類型,因此可以將通用 sObject 強(qiáng)制轉(zhuǎn)換為特定 sObject。本示例說(shuō)明如何將通用 sObject 強(qiáng)制轉(zhuǎn)換為 Account。
// Cast a generic sObject to an Account
Account acct = (Account)myGenericSObject;
// Now, you can use the dot notation to access fields on Account
String name = acct.Name;
String phone = acct.Phone;
3. Manipulate Records with DML
Learning Objectives
完成本單元后,您將能夠:
使用 DML 插入,更新和刪除記錄。
批量執(zhí)行 DML 語(yǔ)句。
使用 upsert 插入或更新記錄。
捕獲 DML 異常。
使用數(shù)據(jù)庫(kù)方法插入帶有部分成功選項(xiàng)的新記錄并處理結(jié)果。
知道何時(shí)使用 DML 語(yǔ)句以及何時(shí)使用數(shù)據(jù)庫(kù)方法。
對(duì)相關(guān)記錄執(zhí)行 DML 操作。
使用數(shù)據(jù)處理語(yǔ)言(縮寫為 DML)在 Salesforce 中創(chuàng)建和修改記錄。DML 通過(guò)提供簡(jiǎn)單的語(yǔ)句來(lái)插入,更新,合并,刪除和還原記錄,從而提供了一種直接的記錄管理方法。
因?yàn)?Apex 是一種以數(shù)據(jù)為中心的語(yǔ)言,并且保存在 Lightning Platform 上,所以它可以直接訪問(wèn) Salesforce 中的數(shù)據(jù)。與其他需要額外設(shè)置才能連接到數(shù)據(jù)源的編程語(yǔ)言不同,使用 Apex DML,管理記錄變得非常容易!通過(guò)調(diào)用 DML 語(yǔ)句,您可以快速對(duì) Salesforce 記錄執(zhí)行操作。
本示例將 charles 帳戶添加到 Salesforce。首先創(chuàng)建一個(gè)帳戶 sObject,然后將其作為參數(shù)傳遞給 insert 語(yǔ)句,該語(yǔ)句將記錄保留在 Salesforce 中。
// Create the account sObject
Account acct = new Account(Name = charles ,Phone = (415)555-1212 ,NumberOfEmployees=10000);
// Insert the account by using DML
insert acct;
DML 語(yǔ)句
以下 DML 語(yǔ)句可用。
insert
update
upsert
delete
undelete
merge
每個(gè) DML 語(yǔ)句都接受單個(gè) sObject 或一個(gè) sObject 列表(或數(shù)組)。在 sObjects 列表上進(jìn)行操作是一種處理記錄的更有效方法。
除了幾個(gè)語(yǔ)句外,所有這些語(yǔ)句都是熟悉的數(shù)據(jù)庫(kù)操作。upsert 和 merge 語(yǔ)句特定于 Salesforce,并且非常方便。
upsert DML 操作使用指定的字段來(lái)確定是否存在現(xiàn)有對(duì)象,如果沒(méi)有指定字段,則使用 ID 字段在單個(gè)語(yǔ)句中創(chuàng)建新記錄并更新 sObject 記錄。
除了幾個(gè)語(yǔ)句外,所有這些語(yǔ)句都是熟悉的數(shù)據(jù)庫(kù)操作。upsert 和 merge 語(yǔ)句特定于 Salesforce,并且非常方便。
merge 語(yǔ)句將多達(dá)三個(gè)相同 sObject 類型的記錄合并到其中一個(gè)記錄中,刪除其他記錄,并重新關(guān)聯(lián)任何相關(guān)記錄。
ID 字段自動(dòng)分配給新記錄
插入記錄時(shí),系統(tǒng)會(huì)為每個(gè)記錄分配一個(gè) ID。除了將 ID 值保留在數(shù)據(jù)庫(kù)中之外,ID 值還將自動(dòng)填充到在 DML 調(diào)用中用作參數(shù)的 sObject 變量上。
本示例說(shuō)明如何獲取與插入帳戶相對(duì)應(yīng)的 sObject 上的 ID。
/*
* Apply To: DML Practice
* Created On: 05-15-2021
* Developer Date Version Description
* -------------------------------------------------
* charles 05-15-2021 V1.0 Initial Version
*/
public class DMLPractice1 { public static void testDML(){
//Create the account sObject
Account acct = new Account(Name= charles
,Phone = (415)555-1212 ,NumberOfEmployees=100);
Insert acct;
//Get the new ID on the inserted sObject argument
ID acctID = acct.Id;
System.debug(ID= +acctID);
//Debug log result(the ID will be different in your case)
}
}
Beyond the Basics
因?yàn)槭纠械?sObject 變量在 DML 調(diào)用之后包含 ID,所以您可以重用此 sObject 變量以執(zhí)行進(jìn)一步的 DML 操作,例如更新,因?yàn)橄到y(tǒng)將能夠通過(guò)匹配 ID 將 sObject 變量映射到其對(duì)應(yīng)的記錄。您可以從數(shù)據(jù)庫(kù)中檢索記錄以獲取其字段,包括 ID 字段,但是 DML 無(wú)法做到這一點(diǎn)。您需要使用 SOQL 編寫查詢。您將在另一個(gè)單元中學(xué)習(xí) SOQL。
批量 DML
您可以在單個(gè) sObject 上執(zhí)行批量 DML 操作,也可以在 sObject 列表上批量執(zhí)行 DML 操作。建議執(zhí)行批量 DML 操作,因?yàn)檫@有助于避免達(dá)到調(diào)控器限制,例如,每個(gè) Apex 事務(wù)的 DML 限制為 150 條語(yǔ)句。設(shè)置此限制是為了確保公平訪問(wèn) Lightning Platform 中的共享資源。在 sObject 列表上執(zhí)行 DML 操作被視為一個(gè) DML 語(yǔ)句,而不是每個(gè) sObject 的一個(gè)語(yǔ)句。
DML Practice1
Create a method for inserting accounts. To pass this challenge, create an Apex class that inserts a new account named after an incoming parameter. If the account is successfully inserted, the method should return the account record. If a DML exception occurs, the method should return null.
The Apex class must be called AccountHandler and be in the public scope
The Apex class must have a public static method called insertNewAccount
The method must accept an incoming string as a parameter, which will be used to create the Account name
The method must insert the account into the system and then return the record
The method must also accept an empty string, catch the failed DML and then return null
/*
* Apply To: DML Practice
* Created On: 05-15-2021
*
* Developer Date Version Description
* -------------------------------------------------
* charles 05-15-2021 V1.0 Initial Version
*
*/
public class AccountHandler { public static Account insertNewAccount(String accountName){ Account acct = new Account(Name = accountName);
try {
insert acct;
}catch(DmlException e){ System.debug( A DML exception has occurred: +e.getMessage());
return null;
}
return acct;
}
}
如果發(fā)現(xiàn)問(wèn)題或有更好的方法歡迎交流討論。
DML Practice2
Create a class and one method that creates a specified number of new accounts and adds them to the database. Create a public Apex class named AccountHandler
Add a public static method to the class:
Name: insertAccount
Include a parameter for the number of new accounts:
Data type: Integer
Create a list of Account records:
List name: addAccounts
Use a while loop to add n new Accounts to the list, where n is a value that is incremented by 1 during each iteration of the loop:
Name: Acme Inc n
AccountNumber: A000n
Hint: You did something like this when you created three new Tea Factory stores. Write one DML statement that inserts all the records into the database at one time Run your insertAccount method
/*
* Apply To: DML Practice
* Created On: 05-20-2021
*
* Developer Date Version Description
* -------------------------------------------------
* charles 05-20-2021 V1.0 Initial Version
*
*/
public class AccountHandler { public static void insertAccount(Integer n){
List Account addAccounts = new List Account
for(Integer i = 0; i i++){ Account acct = new Account();
acct.Name = Acme In
acct.AccountNumber = A000
addAccounts.add(acct);
}
try{
insert addAccounts;
}catch(DMLException e){ System.debug( A DML Exception has occurred +e.getMessage());
}
}
}
在匿名窗口中執(zhí)行下面的代碼:
AccountHandler.insertAccount(10);
效果演示:
執(zhí)行上述代碼前: 我的 Acounts 下面只有 3 條記錄
執(zhí)行上述代碼后: 我的 Accounts 下面多了 10 條記錄
結(jié)果 Trailhead 報(bào)錯(cuò)了,我才知道理解錯(cuò)了,我以為 Acme Inc n 的意思是 Acme Inc 1 , Acme Inc 2 , Acme Inc 3….
改正后的做法:
/*
* Apply To: DML Practice
* Created On: 05-20-2021
*
* Developer Date Version Description
* -------------------------------------------------
* charles 05-20-2021 V2.0 Initial Version
*
*/
public class AccountHandler { public static void insertAccount(Integer n){
List Account addAccounts = new List Account
for(Integer i = 0; i i++){ Account acct = new Account();
acct.Name = Acme Inc
acct.AccountNumber = A000
addAccounts.add(acct);
}
try{
insert addAccounts;
}catch(DMLException e){ System.debug( A DML Exception has occurred +e.getMessage());
}
}
}
效果演示:成功在 Accounts 上插入了 10 條數(shù)據(jù)
4. Write SOQL Queries
Learning Objectives
在 Apex 中編寫 SOQL 查詢。
通過(guò)使用開(kāi)發(fā)人員控制臺(tái)中的查詢編輯器執(zhí)行 SOQL 查詢。
通過(guò)使用匿名 Apex 執(zhí)行嵌入在 Apex 中的 SOQL 查詢。
查詢相關(guān)記錄。
Reference
Use sObjects and DML Learning Objectives
編寫 SOQL 查詢
要從 Salesforce 中讀取記錄,您必須編寫查詢。Salesforce 提供了 Salesforce 對(duì)象查詢語(yǔ)言(簡(jiǎn)稱 SOQL),可用于讀取保存的記錄。SOQL 與標(biāo)準(zhǔn) SQL 語(yǔ)言類似,但是為 Lightning Platform 定制的。
由于 Apex 可以直接訪問(wèn)存儲(chǔ)在數(shù)據(jù)庫(kù)中的 Salesforce 記錄,因此您可以將 SOQL 查詢嵌入到 Apex 代碼中,并以簡(jiǎn)單的方式獲取結(jié)果。當(dāng) SOQL 嵌入 Apex 中時(shí),稱為內(nèi)聯(lián) SOQL。
要將 SOQL 查詢包含在 Apex 代碼中,請(qǐng)將 SOQL 語(yǔ)句包裝在方括號(hào) [] 中,然后將返回值分配給 sObjects 數(shù)組。例如,以下內(nèi)容檢索具有兩個(gè)字段 Name 和 Phone 的所有帳戶記錄,并返回一個(gè) Account sObjects 數(shù)組。
Account [ ] acct = [select Name,Phone from Account];
預(yù)備知識(shí)
本單元中的某些查詢期望組織擁有客戶和聯(lián)系人。在運(yùn)行查詢之前,請(qǐng)創(chuàng)建一些示例數(shù)據(jù)。
在開(kāi)發(fā)人員控制臺(tái)
在窗口中插入以下代碼片段, 然后單擊執(zhí)行。
/*
* Apply To: SOQL Practice
* Created On: 05-16-2021
*
* Developer Date Version Description
* -------------------------------------------------
* charles 05-16-2021 V1.0 Initial Version
*
*/
public class SOQLPractice1 { public static void testSOQL(){
// Add account and related contact
Account acct = new Account(
Name= SFDC Computing ,
Phone= (415)555-1212 ,
NumberOfEmployees=50,
BillingCity= San Francisco
insert acct;
// Once the account is inserted, the sObject will be
// populated with an ID.
// Get this ID.
ID acctID = acct.ID;
// Add a contact to this account.
Contact con = new Contact(
FirstName= Carol ,
LastName= Ruiz ,
Phone= (415)555-1212 ,
Department= Wingo ,
AccountId=acctID);
insert con;
// Add account with no contact
Account acct2 = new Account(
Name= The SFDC Query Man ,
Phone= (310)555-1213 ,
NumberOfEmployees=50,
BillingCity= Los Angeles ,
Description= Expert in wing technologies.
insert acct2;
}
}
擴(kuò)展
與其他 SQL 語(yǔ)言不同,您不能使用 * 來(lái)查詢 所有記錄。您必須指定要顯式獲取的每個(gè)字段。如果您嘗試訪問(wèn)未在 SELECT 子句中指定的字段,則會(huì)收到錯(cuò)誤消息,因?yàn)樯形礄z索到該字段。
您無(wú)需在查詢中指定 Id 字段,因?yàn)闊o(wú)論在查詢中是否指定,它始終會(huì)在 Apex 查詢中返回。例如:SELECT Id,Phone FROM Account 和 SELECT Phone FROM Account 是等效的語(yǔ)句。如果您要檢索的是唯一字段,則可能只有一次需要指定 ID 字段,因?yàn)槟仨氈辽倭谐鲆粋€(gè)字段:SELECT Id FROM Account。在查詢編輯器中運(yùn)行查詢時(shí),您可能還需要指定 ID 字段,因?yàn)槌侵付ǎ駝t不會(huì)顯示 ID 字段。
在 SOQL 查詢中訪問(wèn)變量
如果 Apex 中的 SOQL 語(yǔ)句前面帶有冒號(hào)(:),則它們可以引用 Apex 代碼變量和表達(dá)式。在 SOQL 語(yǔ)句中使用局部變量稱為 bind。
本示例說(shuō)明了如何使用 targetDeparment WHERE 子句中的變量。
String targetDepartment = Wingo
Contact[] techContacts = [SELECT FirstName,LastName
FROM Contact WHERE Department=:targetDepartment];
SOQL Practice
Create an Apex class that returns contacts based on incoming parameters. For this challenge, you will need to create a class that has a method accepting two strings. The method searches for contacts that have a last name matching the first string and a mailing postal code matching the second. It gets the ID and Name of those contacts and returns them.
The Apex class must be called ContactSearch and be in the public scope
The Apex class must have a public static method called searchForContacts
The method must accept two incoming strings as parameters
The method should then find any contact that has a last name matching the first string, and mailing postal code (API name: MailingPostalCode) matching the second string
The method should finally return a list of Contact records of type List that includes the ID and Name fields
/*
* Apply To: SOQL Practice
* Created On: 05-16-2021
*
* Developer Date Version Description
* -------------------------------------------------
* charles 05-16-2021 V1.0 Initial Version
*
*/
public class ContactSearch { public static List Contact searchForContacts(String parm1,String parm2){ List Contact contactList = [select ID,Name from Contact where( LastName =: parm1 AND MailingPostalCode =: parm2)];
return contactList;
}
}
4. Write SOSL Queries
Learning Objectives
描述 SOSL 和 SOQL 之間的區(qū)別。
使用 SOSL 查詢跨多個(gè)對(duì)象搜索字段。
通過(guò)使用開(kāi)發(fā)人員控制臺(tái)中的查詢編輯器執(zhí)行 SOSL 查詢。
編寫 SOSL 查詢 Salesforce 對(duì)象搜索語(yǔ)言(SOSL)是一種 Salesforce 搜索語(yǔ)言,用于在記錄中執(zhí)行文本搜索。使用 SOSL 在 Salesforce 中跨多個(gè)標(biāo)準(zhǔn)和自定義對(duì)象記錄搜索字段。SOSL 與 Apache Lucene 相似。將 SOSL 查詢添加到 Apex 很簡(jiǎn)單 - 您可以將 SOSL 查詢直接嵌入到 Apex 代碼中。當(dāng) SOSL 嵌入 Apex 中時(shí),稱為內(nèi)聯(lián) SOSL。
6. Salesforce Developer Console Shortcut Key
Ctrl+. : 表示代碼自動(dòng)補(bǔ)全功能
Ctrl+D : 表示刪除光標(biāo)所在的行
Ctrl+Alt+N : 將光標(biāo)放在一條語(yǔ)句上,然后點(diǎn)擊右上角的’Go To’就會(huì)跳到相應(yīng)的語(yǔ)句中
Shift+Tab : 表示格式化選中的代碼
7.Define Sets and Maps
Learning Objectives
After completing this unit, you’ll be able to:
Create sets and maps.
Describe how lists, sets, and maps differ.
Decide when to use a set instead of a list.
如您所知,列表是具有相同數(shù)據(jù)類型的項(xiàng)目的有序集合。每個(gè)項(xiàng)目都有一個(gè)稱為索引的位置。這使按編號(hào)索引檢索列表中的項(xiàng)目變得容易。但是 Apex 的收藏不僅僅是列表。集合的其他兩種類型是集合和映射。
Set
到目前為止,您已經(jīng)創(chuàng)建了一種 Collection 類型,列表。set 集合是相同類型的無(wú)序唯一項(xiàng)集合。與 List 列表類似,Set 集合是一組稱為元素的項(xiàng),所有元素都具有相同的數(shù)據(jù)類型,如字符串、整數(shù)甚至 Account。與 List 列表不同,集合不維護(hù)其元素的特定順序。因?yàn)樵厥菬o(wú)序的,所以 Set 集合不能有任何重復(fù)。如果你試圖添加一個(gè)已經(jīng)在 Set 集合中的元素,你不會(huì)得到一個(gè)錯(cuò)誤,但是新值不會(huì)添加到 Set 集合中。
請(qǐng)記住,在循環(huán)遍歷 List 列表時(shí),總是按照添加到 List 列表中的項(xiàng)的順序訪問(wèn)它的項(xiàng)。當(dāng)循環(huán)遍歷一個(gè) Set 集合時(shí),因?yàn)樵厥菬o(wú)序的,所以可以以隨機(jī)順序訪問(wèn)元素。
/*
* Apply To: Set Practice
* Created On: 05-20-2021
*
* Developer Date Version Description
* -------------------------------------------------
* charles 05-20-2021 V1.0 Initial Version
*
*/
public class SetPractice { public static void testSet(){
Set String teaTypes = new Set String
teaTypes.add( Black
teaTypes.add( White
teaTypes.add( Herbal
System.debug(teaTypes);
}
}
匿名窗口執(zhí)行:
SetPractice.testSet();
Map
與 List 列表或 Set 集合相比,Map 是更復(fù)雜的集合。映射中的每個(gè)項(xiàng)目都有兩個(gè)部分:鍵和值,稱為鍵值對(duì)。鍵和值可以是任何數(shù)據(jù)類型。盡管每個(gè)鍵都是唯一的,但是值可以在映射中重復(fù)。想象一下電話國(guó)家代碼的映射。國(guó)家 / 地區(qū)代碼是鍵,國(guó)家 / 地區(qū)名稱是值。每個(gè)國(guó)家 / 地區(qū)代碼都是唯一的,但是國(guó)家 / 地區(qū)可以重復(fù)(因?yàn)橐粋€(gè)國(guó)家 / 地區(qū)可能包含多個(gè)國(guó)家 / 地區(qū)代碼)。
put 方法
要將鍵值對(duì)添加到 Map,請(qǐng)使用 put 方法,如下所示:
put 方法需要兩個(gè)參數(shù):鍵和值。讓我們創(chuàng)建一個(gè)茶類型(鍵)及其風(fēng)味特征(值)的映射。
Tea Types and Flavor Profiles
Create a Map
/*
* Apply To: Map Practice
* Created On: 05-20-2021
*
* Developer Date Version Description
* -------------------------------------------------
* charles 05-20-2021 V1.0 Initial Version
*
*/
public class Tea { public static void orderTea(){
Map String,String teaTypes = new Map String,String
teaTypes.put( Black , Earthy
teaTypes.put( White , Sweet
teaTypes.put( herbal , Sweet
System.debug(teaTypes);
}
}
重點(diǎn)
List 代表一類的有序數(shù)據(jù)列表。數(shù)據(jù)序號(hào)從 0 開(kāi)始。與 JAVA 不同的是:List 是一個(gè)類,并且不存在 ArrayList 等子類。即實(shí)例化
eg:List String list1 = new List String
List 可以通過(guò)自身構(gòu)造函數(shù)實(shí)例化,也可以通過(guò)數(shù)組進(jìn)行實(shí)例化。
以下為 List 主要方法:
注:set()方法在設(shè)置插入位置以前應(yīng)確保長(zhǎng)度大于需要插入的位置,否則將拋出異常。
Set 代表一類數(shù)據(jù)的無(wú)序列表。與 JAVA 不同的是:Set 是一個(gè)類,不存在 HashSet 等子類。即實(shí)例化
eg:Set String set1 = new Set String
到此,相信大家對(duì)“Apex 和 Database 相關(guān)知識(shí)點(diǎn)有哪些”有了更深的了解,不妨來(lái)實(shí)際操作一番吧!這里是丸趣 TV 網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!