新聞中心
本篇內(nèi)容主要講解“Apex和Database相關知識點有哪些”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“Apex和Database相關知識點有哪些”吧!
漢壽網(wǎng)站制作公司哪家好,找成都創(chuàng)新互聯(lián)!從網(wǎng)頁設計、網(wǎng)站建設、微信開發(fā)、APP開發(fā)、響應式網(wǎng)站建設等網(wǎng)站項目制作,到程序開發(fā),運營維護。成都創(chuàng)新互聯(lián)從2013年開始到現(xiàn)在10年的時間,我們擁有了豐富的建站經(jīng)驗和運維經(jīng)驗,來保證我們的工作的順利進行。專注于網(wǎng)站建設就選成都創(chuàng)新互聯(lián)。
salesforce.com 是世界上第一個提出云計算平臺的公司,同時,它還引入了世界上第一門云計算編程語言Apex。
1. Get Started with Apex
Learning Objectives
完成本單元后,您將能夠:
描述Apex編程語言的主要功能。
保存一個Apex類并使用Anonymous.Apex調用方法。
使用開發(fā)者控制臺檢查調試日志。
Apex的特點:
代碼托管-在服務器Lightning Platform上保存,編譯和執(zhí)行Apex。
Apex是不區(qū)分大小寫的語言
Apex命名規(guī)范(這里我用的是Java的規(guī)范)
類名首字母大寫,如果類名由多個單詞組成,每個單詞的首字母都要大寫。 如:
public class MyFirstClass{}
變量名、方法名首字母小寫, 如果名稱由多個單詞組成,除第一個單詞外,其他每個單詞的首字母都要大寫。 如:
int index=0; public void toString(){}
常量名全部大寫
Apex支持以下數(shù)據(jù)類型:
Primitive (Integer, Double, Long, Date, Datetime, String, ID, or Boolean) 原始數(shù)據(jù)類型(整數(shù),雙精度,長整型,日期,日期時間,字符串,ID或布爾值)
Collections (Lists, Sets and Maps)
sObject
這是Salesforce中的特殊數(shù)據(jù)類型。
它類似于SQL中的表,并且包含與SQL中的列類似的字段。
有兩種類型的sObjects:Standard和Custom。
例如,Account是一個標準的sObject ; 任何其他用戶定義的對象(如我們創(chuàng)建的Customer對象)是Custom sObject。
Enums 枚舉
Classes, Objects and Interfaces 類,對象和接口
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 Listresult = new List (); //Interate through a for loop to populate the array of Strings for(Integer i=0; i
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中的標準和自定義對象記錄映射到Apex中的sObject類型。 以下是Apex中用于標準對象的一些常見sObject類型名稱。
Account
Contact
Lead
Opportunity
創(chuàng)建一個sObject,您需要聲明一個變量,并將其分配給一個sObject實例。變量的數(shù)據(jù)類型是sObject類型。 以下示例創(chuàng)建一個類型為Account的sObject變量,并將其分配給名稱為charles的新帳戶。
Account acct = new Account(Name='charles');sObject和字段名
對于自定義對象和自定義字段,API名稱始終以__c后綴結尾。 對于自定義關系型(Relationship)字段,API名稱以__r后綴結尾。 例如:
標簽為Merchandise的自定義對象的API名稱為Merchandise__c。
標有Description標簽的自定義字段的API名稱為Description__c。
標有Items標簽的自定義關系型字段的API名稱為Items__r。
創(chuàng)建sObjects并添加字段
在插入Salesforce記錄之前,必須首先在內(nèi)存中將其創(chuàng)建為sObject。 與其他任何對象一樣,sObject是使用new運算符創(chuàng)建的:
ccount acct = new Account();API對象名稱為Apex中sObject變量的數(shù)據(jù)類型。 在此示例中,Account是acct變量的數(shù)據(jù)類型。 acct變量引用的Account為空,因為我們尚未填充其任何字段。 有兩種添加字段的方法:通過構造函數(shù)或使用
.
表示法。添加字段的最快方法是在構造函數(shù)中將它們指定為“名稱/值”對。 例如,此語句創(chuàng)建一個新帳戶sObject,并使用字符串值填充其“名稱”字段。
Account acct = new Account(Name='charles');Name字段是帳戶唯一必填的字段,這意味著必須先填充該字段,然后才能插入新記錄。 但是,您也可以為新記錄填充其他字段。 此示例還添加了電話號碼和員工人數(shù)。
Account acct = new Account(Name='charles', Phone='(415)555-1212', NumberOfEmployees=10000);或者,您可以使用點表示法將字段添加到sObject。 以下內(nèi)容與前面的示例等效,盡管它需要花費更多的代碼行。
Account acct = new Account(); acct.Name = 'charles'; acct.Phone = '(415)555-1212'; acct.NumberOfEmployees = 10000;通常,在使用sObjects時,您使用特定的sObject數(shù)據(jù)類型,例如,標準對象使用Account或Book定制對象使用Book__c。 但是,當您不知道方法要處理的sObject類型時,可以使用通用的sObject數(shù)據(jù)類型。
使用通用sObject數(shù)據(jù)類型聲明的變量可以引用任何Salesforce記錄,無論是標準記錄還是自定義對象記錄。
這個例子顯示了如何通用sObject變量可以分配給任何Salesforce對象:一個名為Book__c的帳戶和一個自定義對象。
sObject sobj1 = new Account(Name='Trailhead'); sObject sobj2 = new Book__c(Name='Workbook 1');相反,使用特定sObject數(shù)據(jù)類型聲明的變量只能引用相同類型的Salesforce記錄。
在處理通用sObject時,有時需要將sObject變量轉換為特定的sObject類型。 這樣做的好處之一是能夠使用點符號來訪問字段,而點符號在通用sObject上不可用。 由于sObject是所有特定sObject類型的父類型,因此可以將通用sObject強制轉換為特定sObject。 本示例說明如何將通用sObject強制轉換為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語句。
使用upsert插入或更新記錄。
捕獲DML異常。
使用數(shù)據(jù)庫方法插入帶有部分成功選項的新記錄并處理結果。
知道何時使用DML語句以及何時使用數(shù)據(jù)庫方法。
對相關記錄執(zhí)行DML操作。
使用數(shù)據(jù)處理語言(縮寫為DML)在Salesforce中創(chuàng)建和修改記錄。 DML通過提供簡單的語句來插入,更新,合并,刪除和還原記錄,從而提供了一種直接的記錄管理方法。
因為Apex是一種以數(shù)據(jù)為中心的語言,并且保存在Lightning Platform上,所以它可以直接訪問Salesforce中的數(shù)據(jù)。 與其他需要額外設置才能連接到數(shù)據(jù)源的編程語言不同,使用Apex DML,管理記錄變得非常容易! 通過調用DML語句,您可以快速對Salesforce記錄執(zhí)行操作。
本示例將charles帳戶添加到Salesforce。 首先創(chuàng)建一個帳戶sObject,然后將其作為參數(shù)傳遞給insert語句,該語句將記錄保留在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語句
以下DML語句可用。
insert
update
upsert
delete
undelete
merge
每個DML語句都接受單個sObject或一個sObject列表(或數(shù)組)。 在sObjects列表上進行操作是一種處理記錄的更有效方法。
除了幾個語句外,所有這些語句都是熟悉的數(shù)據(jù)庫操作。 upsert和merge語句特定于Salesforce,并且非常方便。
upsert DML操作使用指定的字段來確定是否存在現(xiàn)有對象,如果沒有指定字段,則使用ID字段在單個語句中創(chuàng)建新記錄并更新sObject記錄。
除了幾個語句外,所有這些語句都是熟悉的數(shù)據(jù)庫操作。 upsert和merge語句特定于Salesforce,并且非常方便。
merge語句將多達三個相同sObject類型的記錄合并到其中一個記錄中,刪除其他記錄,并重新關聯(lián)任何相關記錄。
ID字段自動分配給新記錄
插入記錄時,系統(tǒng)會為每個記錄分配一個ID。 除了將ID值保留在數(shù)據(jù)庫中之外,ID值還將自動填充到在DML調用中用作參數(shù)的sObject變量上。
本示例說明如何獲取與插入帳戶相對應的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
因為示例中的sObject變量在DML調用之后包含ID,所以您可以重用此sObject變量以執(zhí)行進一步的DML操作,例如更新,因為系統(tǒng)將能夠通過匹配ID將sObject變量映射到其對應的記錄。 您可以從數(shù)據(jù)庫中檢索記錄以獲取其字段,包括ID字段,但是DML無法做到這一點。 您需要使用SOQL編寫查詢。 您將在另一個單元中學習SOQL。
批量DML
您可以在單個sObject上執(zhí)行批量DML操作,也可以在sObject列表上批量執(zhí)行DML操作。 建議執(zhí)行批量DML操作,因為這有助于避免達到調控器限制,例如,每個Apex事務的DML限制為150條語句。 設置此限制是為了確保公平訪問Lightning Platform中的共享資源。 在sObject列表上執(zhí)行DML操作被視為一個DML語句,而不是每個sObject的一個語句。
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)問題或有更好的方法歡迎交流討論。
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){ ListaddAccounts = new List (); for(Integer i = 0; i 在匿名窗口中執(zhí)行下面的代碼:
AccountHandler.insertAccount(10);效果演示:
執(zhí)行上述代碼前: 我的Acounts下面只有3條記錄
執(zhí)行上述代碼后: 我的Accounts下面多了10條記錄
結果Trailhead報錯了,我才知道理解錯了,我以為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){ ListaddAccounts = new List (); for(Integer i = 0; i 效果演示: 成功在Accounts上插入了10條數(shù)據(jù)
4. Write SOQL Queries
Learning Objectives
在Apex中編寫SOQL查詢。
通過使用開發(fā)人員控制臺中的查詢編輯器執(zhí)行SOQL查詢。
通過使用匿名Apex執(zhí)行嵌入在Apex中的SOQL查詢。
查詢相關記錄。
Reference
Use sObjects and DML Learning Objectives
編寫SOQL查詢
要從Salesforce中讀取記錄,您必須編寫查詢。Salesforce提供了Salesforce對象查詢語言(簡稱SOQL),可用于讀取保存的記錄。SOQL與標準SQL語言類似,但是為Lightning Platform定制的。
由于Apex可以直接訪問存儲在數(shù)據(jù)庫中的Salesforce記錄,因此您可以將SOQL查詢嵌入到Apex代碼中,并以簡單的方式獲取結果。當SOQL嵌入Apex中時,稱為內(nèi)聯(lián)SOQL。
要將SOQL查詢包含在Apex代碼中,請將SOQL語句包裝在方括號[ ]中,然后將返回值分配給sObjects數(shù)組。例如,以下內(nèi)容檢索具有兩個字段Name和Phone的所有帳戶記錄,并返回一個Account sObjects數(shù)組。
Account [ ] acct = [select Name,Phone from Account];預備知識
本單元中的某些查詢期望組織擁有客戶和聯(lián)系人。 在運行查詢之前,請創(chuàng)建一些示例數(shù)據(jù)。
在開發(fā)人員控制臺
在窗口中插入以下代碼片段,然后單擊執(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; } }
擴展
與其他SQL語言不同,您不能使用
*
來查詢 所有記錄。 您必須指定要顯式獲取的每個字段。 如果您嘗試訪問未在SELECT子句中指定的字段,則會收到錯誤消息,因為尚未檢索到該字段。您無需在查詢中指定Id字段,因為無論在查詢中是否指定,它始終會在Apex查詢中返回。 例如:SELECT Id,Phone FROM Account和SELECT Phone FROM Account是等效的語句。 如果您要檢索的是唯一字段,則可能只有一次需要指定ID字段,因為您必須至少列出一個字段:SELECT Id FROM Account。 在查詢編輯器中運行查詢時,您可能還需要指定ID字段,因為除非指定,否則不會顯示ID字段。
在SOQL查詢中訪問變量
如果Apex中的SOQL語句前面帶有冒號(:),則它們可以引用Apex代碼變量和表達式。在SOQL語句中使用局部變量稱為 bind。
本示例說明了如何使用
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 ListsearchForContacts(String parm1,String parm2){ List contactList = [select ID,Name from Contact where( LastName =: parm1 AND MailingPostalCode =: parm2)]; return contactList; } }
4. Write SOSL Queries
Learning Objectives
描述SOSL和SOQL之間的區(qū)別。
使用SOSL查詢跨多個對象搜索字段。
通過使用開發(fā)人員控制臺中的查詢編輯器執(zhí)行SOSL查詢。
編寫SOSL查詢 Salesforce對象搜索語言(SOSL)是一種Salesforce搜索語言,用于在記錄中執(zhí)行文本搜索。使用SOSL在Salesforce中跨多個標準和自定義對象記錄搜索字段。SOSL與Apache Lucene相似。 將SOSL查詢添加到Apex很簡單-您可以將SOSL查詢直接嵌入到Apex代碼中。當SOSL嵌入Apex中時,稱為內(nèi)聯(lián)SOSL。
6. Salesforce Developer Console Shortcut Key
Ctrl+. :表示代碼自動補全功能
Ctrl+D :表示刪除光標所在的行
Ctrl+Alt+N : 將光標放在一條語句上,然后點擊右上角的’Go To’就會跳到相應的語句中
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ù)類型的項目的有序集合。 每個項目都有一個稱為索引的位置。 這使按編號索引檢索列表中的項目變得容易。 但是Apex的收藏不僅僅是列表。 集合的其他兩種類型是集合和映射。
Set
到目前為止,您已經(jīng)創(chuàng)建了一種Collection類型,列表。
set
集合是相同類型的無序唯一項集合。與List列表類似,Set集合是一組稱為元素的項,所有元素都具有相同的數(shù)據(jù)類型,如字符串、整數(shù)甚至Account。與List列表不同,集合不維護其元素的特定順序。因為元素是無序的,所以Set集合不能有任何重復。如果你試圖添加一個已經(jīng)在Set集合中的元素,你不會得到一個錯誤,但是新值不會添加到Set集合中。
請記住,在循環(huán)遍歷List列表時,總是按照添加到List列表中的項的順序訪問它的項。當循環(huán)遍歷一個Set集合時,因為元素是無序的,所以可以以隨機順序訪問元素。/* * 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(){ SetteaTypes = new Set (); teaTypes.add('Black'); teaTypes.add('White'); teaTypes.add('Herbal'); System.debug(teaTypes); } } 匿名窗口執(zhí)行:
SetPractice.testSet();
Map
與List列表或Set集合相比,Map是更復雜的集合。映射中的每個項目都有兩個部分:鍵和值,稱為鍵值對。鍵和值可以是任何數(shù)據(jù)類型。盡管每個鍵都是唯一的,但是值可以在映射中重復。想象一下電話國家代碼的映射。國家/地區(qū)代碼是鍵,國家/地區(qū)名稱是值。每個國家/地區(qū)代碼都是唯一的,但是國家/地區(qū)可以重復(因為一個國家/地區(qū)可能包含多個國家/地區(qū)代碼)。
put方法
要將鍵值對添加到Map,請使用put方法,如下所示:
put方法需要兩個參數(shù):鍵和值。 讓我們創(chuà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(){ MapteaTypes = new Map (); teaTypes.put('Black','Earthy'); teaTypes.put('White','Sweet'); teaTypes.put('herbal','Sweet'); System.debug(teaTypes); } }
重點
List代表一類的有序數(shù)據(jù)列表。數(shù)據(jù)序號從0開始。與JAVA不同的是:List是一個類,并且不存在ArrayList等子類。即實例化
eg:Listlist1 = new List (); List可以通過自身構造函數(shù)實例化,也可以通過數(shù)組進行實例化。
以下為List主要方法:
注:set()方法在設置插入位置以前應確保長度大于需要插入的位置,否則將拋出異常。Set代表一類數(shù)據(jù)的無序列表。與JAVA不同的是:Set是一個類,不存在HashSet等子類。即實例化
eg:Setset1 = new Set (); 到此,相信大家對“Apex和Database相關知識點有哪些”有了更深的了解,不妨來實際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關內(nèi)容可以進入相關頻道進行查詢,關注我們,繼續(xù)學習!
分享名稱:Apex和Database相關知識點有哪些
網(wǎng)頁鏈接:http://fisionsoft.com.cn/article/ghccie.html