顯示具有 Apex 標籤的文章。 顯示所有文章
顯示具有 Apex 標籤的文章。 顯示所有文章

2019-01-04

Trailhead commonly asked questions: Expecting '}' but was: 'for'

網頁上的程式碼怎麼原樣貼上去,還沒有過?
Why the code can't be save while it's been directly copied and pasted from the Trailhead page?

通常這個問題產生的原因,是因為你把程式碼直接貼在class內,但這種「把內臟攤出來」的做法是不被允許的。
This usually happened due to 'directly paste code inside class', you need to put the logic inside a method.

仔細看一下 trailhead 提供的程式碼,應該是包在 method 內的。
Look the Trailhead page, carefully, the logic should be inside of a method.

你是不是直接沒有複製到 method 的頭尾XD?
There are probably some code that your missing. :P

p.s. 請善用右上角的copy // Use the copy in the up-right corner


2018-06-19

UPDATE: NOW IT HAS!! A reason why Apex doesn't have while-case

更新:在 18" summer release 後,有類似的語法可以使用。
所以這篇基本上也不太需要存在了QQ

關於 「Switch-while Statements」的連結:https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_switch.htm




先說我最近K資料得到的重點:while-case 在視覺上比起大量的 if-else 更容易理解,但是 Apex 因其可能存在的 「未定義行為」故不採用。
Staring with my resent note for certification-preparing: while-case is more visual pleasing then parallel-ed, if-else. But the possible "undefined, unwanted behavior" is probably the reason why Apex doesn't have it.

定義:while-case 語法是類C語言中,用來處理大量、等階(相同時間點) if-else 語法時,運用「案例」的觀念,進而折疊、縮短程式碼並增加可讀性的功能。
Define: While-case is a method with the understanding of "case", for C-Like language to shorten and increase readability in code.

而 Apex 語法最主要的存在的目的是提供  SFDC 上的服務,所以在雲端開發、多租戶的前提下,程式碼的行為、記憶體的邊界,比什麼都重要。
Apex, on the other hand, as the main Language on SFDC, with the purpose of cloud development/compiling/execution and with multitenant awareness, guards closely against runaway code by enforcing limits, which prevent code from monopolizing shared resources.



2018-05-30

機翻的教學文章真是太誤人子弟了

身為一個新手,想要查點資料,看到免強可以接受的語言張貼了內容,想說那就點進去看看,結果發現是  TrailHead 內容的機翻(機器翻譯)版本。

專有名詞如果沒有用「尋找取代」去替換的話,真的是讓人很一個頭兩個大。

2018-05-10

Execute Apex Class right away/scheduled/delayed in Developer console

如果要測試的 Apex/Bech 是排程執行的,可以不用傻傻等系統幫你執行。

直接在 Developer Console -> Debug -> Open execute anonymous window

輸入以下指令,好直接執行該 Apex:


YourScheduleApexClass s=new YourScheduleApexClass();
s.execute(null) ;


把 YourScheduleApexClass 改成目標 Apex Class 名稱,當然,也要該 Class 是執行的 interface 才可以。

2018-05-07

Result: [OPERATION FAILED]: classes/LeadProcessor.cls: Invalid loop variable type expected SObject was Lead

來,先上error_msg:
Result: [OPERATION FAILED]: classes/LeadProcessor.cls: Invalid loop variable type expected SObject was Lead

然後是code:
    global void execute(Database.BatchableContext BC, List l_scope) {
List l_scope_new = new List();
for(Lead l : l_scope)
{
l.leadsource = 'Dreamforce';
l_scope_new.add(l);
count++;
}
update l_scope_new;
}

原因:compiler期待型態: Lead的變數,但是接到的是型態為sObject的變數的時候,就會跳這個error。