This question runs in a pre-configured Salesforce environment - no personal connection required.

A partner portal has a "register a new customer" form. The Lightning web component behind it posts the form as a JSON array of Account objects to an Apex method, and the first version of that method was two lines long: deserialize the body into a List<Account>, insert it. It passed every test the team wrote, because the team only ever sent the fields the form shows.

Anyone with a browser developer console can send more than the form shows. JSON.deserialize into an SObject type populates any field named in the body, including fields no form would ever expose. Apex runs in system mode by default, so an ordinary insert writes every field the platform allows, without checking the running user's field-level security. A portal user whose profile hides AnnualRevenue, or a custom credit limit field, can set it anyway just by adding it to the JSON. This is called mass assignment, and the fix is to let the platform decide which fields the running user may write, not the caller and not a list you keep by hand.

Apex gives you two tools for this, and they work well together:

  • Security.stripInaccessible(AccessType.CREATABLE, records) returns an SObjectAccessDecision. Its getRecords() gives you copies of the records with every field the running user cannot create removed, and getRemovedFields() tells you what was removed, as a Map<String, Set<String>> keyed by object name. The copies are new records; the list you passed in is left as it was.
  • Database.insert(records, AccessLevel.USER_MODE) runs the DML with the running user's object permissions and field-level security enforced, so a field the user's profile hides makes the insert fail with a DmlException instead of being written. On its own it is a blunt instrument: the whole request fails, it does not tell the caller which field was the problem, and some system fields such as CreatedDate are simply ignored rather than refused.

Stripping first and inserting in user mode means the request succeeds with the fields it was allowed to set, the caller learns exactly what was refused, and user mode is the backstop if anything slips through.

Here you run as an administrator, so the fields that get stripped are the ones nobody can create: audit fields such as CreatedDate and fields the platform calculates for itself. For a portal user whose profile hides AnnualRevenue, the same call strips that field too. The code is identical; only the running user changes.

One field needs your own attention. stripInaccessible never removes Id, so a caller can send the Id of an existing record. A plain insert then fails the whole request, and an implementation that "fixes" that by switching to upsert lets the caller overwrite any record they can name. A create endpoint must never take a caller-chosen Id.

Given jsonBody (a JSON array of Account objects as a String), return an IntakeResult:

  • A null or blank body, or an empty array, creates nothing and returns an IntakeResult whose two lists are empty (not null). No DML runs.
  • Every field the running user cannot create is removed before anything is saved, on every row it appears in. Rows are not rejected for it: each row is still created from the fields that remain.
  • A row that carries an Id has the Id removed and is created as a new Account. No existing record is ever changed.
  • createdIds holds the Id of each Account created, in the same order as the rows in the payload.
  • strippedFields lists every field that was removed from any row, each name once, using the field API name as the platform reports it (including Id when a row carried one), sorted ascending.

You may assume every row has a Name and every value has the right JSON type for its field.

Examples:

Input: jsonBody = '[{"Name":"Willow Creek Dental","Phone":"555-0142"}]' Output: createdIds = [ the new Account Id ], strippedFields = [] Explanation: every field in the row may be created, so nothing is refused Input: jsonBody = '[{"Name":"Ridgeline Outfitters","LastModifiedDate":"2018-06-30T12:00:00.000Z"}, {"Name":"Summit Rentals","SystemModstamp":"2018-06-30T12:00:00.000Z"}]' Output: createdIds = [ two new Account Ids, Ridgeline first ], strippedFields = [ 'LastModifiedDate', 'SystemModstamp' ] Explanation: both audit fields are refused, each row is still created from the fields that remain Input: jsonBody = '[{"Id":"<the Id of an existing Account>","Name":"Taken Over Inc"}]' Output: createdIds = [ a brand new Account Id ], strippedFields = [ 'Id' ] Explanation: the caller does not get to choose the Id, so the existing Account is left exactly as it was Input: jsonBody = '[]' Output: createdIds = [], strippedFields = [] Explanation: nothing to create, and nothing is saved
Apex Code Editor

Welcome to Lightning Challenge!

How It Works

  • • Write your solution in the code editor
  • • This challenge runs without connecting your own org
  • • Submit to check if your solution passes
  • • Use hints if you get stuck

Contest Alert

🏆 #CodeEveryDay November 2026 (Nov 16–30)Sponsored by FLXBL

Contest runs November 16 - 30. Complete challenges to climb the leaderboard!

Prizes 1 & 2: Salesforce certification exam voucher — won by random drawing, where every point is a ticket. see all prizes

Only the 15 daily challenges shown during this contest count toward points. Earlier dailies don't carry over.

Wally can't hear you

Please sign in to access the AI Assistant

Sign In