Langsung ke konten
Skillpedia
API & server (backend)40k Β· #7412

salesforce-apex-quality

Fungsinya

Alat ini membantu memastikan kode yang ditulis untuk platform Salesforce aman dan efisien. Alat ini memeriksa kesalahan umum yang dapat menyebabkan masalah performa atau keamanan, seperti melakukan terlalu banyak operasi database sekaligus atau tidak memeriksa apakah pengguna memiliki izin untuk mengakses data tertentu.

Kapan dipakai

  • βœ“Saat menulis atau meninjau kode Apex untuk Salesforce.
  • βœ“Saat Anda ingin menghindari error governor limit di Salesforce.
  • βœ“Untuk memastikan kode menghormati izin pengguna (keamanan).

Link skill

https://github.com/github/awesome-copilot/blob/main/skills/salesforce-apex-quality/SKILL.md

Salin lalu tempelkan ke AI Anda (Claude, ChatGPT, atau Cursor) dan minta AI mengikuti instruksinya.

Cara pakai

Cara paling mudah: salin pesan ini dan tempelkan ke AI Anda. AI akan membaca skill dan mengikuti instruksinya.

Pesan siap tempel

Gunakan skill ini untuk membantu saya: https://github.com/github/awesome-copilot/blob/main/skills/salesforce-apex-quality/SKILL.md Baca file SKILL.md di link tersebut dan ikuti instruksinya untuk tugas saya.
Mau AI Anda menemukan skill seperti ini sendiri?Pasang MCP Skillpedia gratis: cukup satu baris, dan AI mencari di antara 15.000+ skill sebelum tiap tugas.Pasang dalam 1 menit β†’

Contoh permintaan ke AI

β€œTinjau kode Apex ini untuk memastikan sudah mengikuti praktik terbaik Salesforce terkait keamanan dan limit.”

5 skill baru setiap minggu. 1 menit baca.

Cara kerjanya

Alat ini menganalisis kode Anda untuk mencari pola yang diketahui menyebabkan masalah. Misalnya, alat ini memeriksa apakah Anda melakukan query database atau memperbarui informasi di dalam loop, yang tidak efisien dan dapat membuat sistem crash. Alat ini juga memeriksa apakah kode sudah dirancang untuk menghormati izin pengguna yang menjalankannya.

Konten teknis (SKILL.md asli)

Teks asli dari pembuat, dalam bahasa Inggris, langsung dari GitHub.

Salesforce Apex Quality Guardrails

Apply these checks to every Apex class, trigger, and test file you write or review.

Step 1 β€” Governor Limit Safety Check

Scan for these patterns before declaring any Apex file acceptable:

SOQL and DML in Loops β€” Automatic Fail

// ❌ NEVER β€” causes LimitException at scale
for (Account a : accounts) {
    List<Contact> contacts = [SELECT Id FROM Contact WHERE AccountId = :a.Id]; // SOQL in loop
    update a; // DML in loop
}

// βœ… ALWAYS β€” collect, then query/update once
Set<Id> accountIds = new Map<Id, Account>(accounts).keySet();
Map<Id, List<Contact>> contactsByAccount = new Map<Id, List<Contact>>();
for (Contact c : [SELECT Id, AccountId FROM Contact WHERE AccountId IN :accountIds]) {
    if (!contactsByAccount.containsKey(c.AccountId)) {
        contactsByAccount.put(c.AccountId, new List<Contact>());
    }
    contactsByAccount.get(c.AccountId).add(c);
}
update accounts; // DML once, outside the loop

Rule: if you see [SELECT or Database.query, insert, update, delete, upsert, merge inside a for loop body β€” stop and refactor before proceeding.

Step 2 β€” Sharing Model Verification

Every class must declare its sharing intent explicitly. Undeclared sharing inherits from the caller β€” unpredictable behaviour.

Declaration When to use
public with sharing class Foo Default for all service, handler, selector, and controller classes
public without sharing class Foo Only when the class must run elevated (e.g. system-level logging, trigger bypass). Requires a code comment explaining why.
public inherited sharing class Foo Framework entry points that should respect the caller's sharing context

If a class does not have one of these three declarations, add it before writing anything else.

Step 3 β€” CRUD / FLS Enforcement

Apex code that reads or writes records on behalf of a user must verify object and field access. The platform does not enforce FLS or CRUD automatically in Apex.

// Check before querying a field
if (!Schema.sObjectType.Contact.fields.Email.isAccessible()) {
    throw new System.NoAccessException();
}

// Or use WITH USER_MODE in SOQL (API 56.0+)
List<Contact> contacts = [SELECT Id, Email FROM Contact WHERE AccountId = :accId WITH USER_MODE];

// Or use Database.query with AccessLevel
List<Contact> contacts = Database.query('SELECT Id, Email FROM Contact', AccessLevel.USER_MODE);

Rule: any Apex method callable from a UI component, REST endpoint, or @InvocableMethod must enforce CRUD/FLS. Internal service methods called only from trusted contexts may use with sharing instead.

Step 4 β€” SOQL Injection Prevention

// ❌ NEVER β€” concatenates user input into SOQL string
String soql = 'SELECT Id FROM Account WHERE Name = \'' + userInput + '\'';

// βœ… ALWAYS β€” bind variable
String soql = [SELECT Id FROM Account WHERE Name = :userInput];

// βœ… For dynamic SOQL with user-controlled field names β€” validate against a whitelist
Set<String> allowedFields = new Set<String>{'Name', 'Industry', 'AnnualRevenue'};
if (!allowedFields.contains(userInput)) {
    throw new IllegalArgumentException('Field not permitted: ' + userInput);
}

Step 5 β€” Modern Apex Idioms

Prefer current language features (API 62.0 / Winter '25+):

Old pattern Modern replacement
if (obj != null) { x = obj.Field__c; } x = obj?.Field__c;
x = (y != null) ? y : defaultVal; x = y ?? defaultVal;
System.assertEquals(expected, actual) Assert.areEqual(expected, actual)
System.assert(condition) Assert.isTrue(condition)
[SELECT ... WHERE ...] with no sharing context [SELECT ... WHERE ... WITH USER_MODE]

Step 6 β€” PNB Test Coverage Checklist

Every feature must be tested across all three paths. Missing any one of these is a quality failure:

Positive Path

  • Expected input β†’ expected output.
  • Assert the exact field values, record counts, or return values β€” not just that no exception was thrown.

Negative Path

  • Invalid input, null values, empty collections, and error conditions.
  • Assert that exceptions are thrown with the correct type and message.
  • Assert that no records were mutated when the operation should have failed cleanly.

Bulk Path

  • Insert/update/delete 200–251 records in a single test transaction.
  • Assert that all records processed correctly β€” no partial failures from governor limits.

…

Pertanyaan umum

Apa itu 'governor limits'?

Ini adalah aturan Salesforce yang membatasi jumlah pemrosesan yang dapat dilakukan kode, untuk memastikan platform berjalan lancar bagi semua pengguna.

Apa itu 'SOQL/DML di dalam loop'?

Ini terjadi ketika Anda melakukan query database (SOQL) atau operasi penulisan (DML seperti insert, update, delete) secara berulang di dalam loop. Hal ini menghabiskan banyak sumber daya dan umumnya dilarang.

Apa arti 'CRUD/FLS security'?

CRUD (Create, Read, Update, Delete) berkaitan dengan izin untuk membuat, membaca, memperbarui, atau menghapus record. FLS (Field-Level Security) berkaitan dengan izin untuk mengakses field tertentu dalam sebuah record. Kode harus memeriksa apakah pengguna memiliki izin-izin tersebut.

Detail

Deskripsi asli dari pembuat

Apex code quality guardrails for Salesforce development. Enforces bulk-safety rules (no SOQL/DML in loops), sharing model requirements, CRUD/FLS security, SOQL injection prevention, PNB test coverage (Positive / Negative / Bulk), and modern Apex idioms. Use this skill when reviewing or generating Apex classes, trigger handlers, batch jobs, or test classes to catch governor limit risks, security ga

repo: github/awesome-copilot

Popularitas

Bintang GitHub
40k
Peringkat
#7412
Lisensi
MIT
Diperbarui
15/10/2026

Apakah skill ini berguna?

0

Masuk untuk memberi suara.

Komentar

  • …

Skill lain dari repositori ini

Skill serupa