How to Call Apex Imperatively in Salesforce LWC

Master imperative Apex calls in Salesforce LWC for dynamic user actions, server-side processing, and real-world Lightning Web Component development.

Neha Panwar
By
Neha Panwar
Salesforce Developer and Technical Writer
Neha Panwar is a Salesforce developer and technical writer who shares practical tutorials, Apex guides, and real-world solutions for developers. She focuses on simplifying Salesforce concepts,...
- Salesforce Developer and Technical Writer

Lightning Web Components provide multiple ways to communicate with Apex. One of the most important approaches is the imperative Apex call, where developers manually control when an Apex method should execute.

Unlike the @wire service that automatically fetches data reactively, imperative Apex gives full control over server calls. This makes it extremely useful for button clicks, form submissions, search operations, record updates, and dynamic user actions.

If you already understand the basics of the Salesforce @wire Decorator in LWC with Real Examples, learning imperative Apex becomes much easier because both approaches are commonly used together in real Salesforce projects.

Imperative Apex is heavily used in enterprise applications where developers need better control over execution timing, error handling, and DML operations.

What Is Imperative Apex in LWC?

Imperative Apex means calling an Apex method manually from JavaScript instead of letting the framework invoke it automatically.

In simple words:

  • Developer decides when the Apex call happens
  • The call usually happens after a user action
  • The method returns a single response
  • It supports DML operations
  • It works well for dynamic interactions

This approach is very common in:

  • Search functionality
  • Save buttons
  • Update operations
  • Delete actions
  • API callouts
  • Dynamic filtering
  • Form submissions

Unlike @wire, imperative Apex does not continuously stream data reactively.

When Should You Use Imperative Apex?

Imperative Apex should be used when:

  • You need to control execution manually
  • The Apex method performs DML operations
  • Data should load only after user interaction
  • You want better async handling
  • You need custom error handling
  • The method is not cacheable

Typical examples include:

  • Save Contact button
  • Search Accounts feature
  • Update Opportunity stage
  • Create Case record
  • Call external API from Apex

Imperative Apex vs @wire

Many beginners get confused between imperative Apex and the wire service.

Here is the biggest difference.

Feature@wireImperative Apex
Automatic executionYesNo
Manual controlLimitedFull
Supports DMLNoYes
Reactive updatesYesNo
Best for read operationsYesYes
Best for user actionsNoYes

If you are still learning wire service concepts, read Salesforce @wire Decorator in LWC with Real Examples before moving deeper into imperative calls.

Wire service vs imperative Apex in Salesforce LWC

How Imperative Apex Works in LWC

The flow usually looks like this:

  1. User clicks button
  2. JavaScript method executes
  3. Apex method is called
  4. Server processes request
  5. Response returns to component
  6. UI updates dynamically

This manual execution pattern provides much more flexibility compared to reactive wire methods.

Importing Apex Methods in LWC

The first step is importing the Apex method.

Apex Controller

public with sharing class AccountController {

@AuraEnabled
public static List<Account> getAccounts() {

return [
SELECT Id, Name
FROM Account
LIMIT 10
];
}
}

LWC JavaScript Import

import getAccounts
from '@salesforce/apex/AccountController.getAccounts';

This import syntax is required for both imperative and wired Apex calls.

Basic Imperative Apex Example

Here is the most basic example.

JavaScript

import { LightningElement } from 'lwc';

import getAccounts
from '@salesforce/apex/AccountController.getAccounts';

export default class AccountList extends LightningElement {

accounts;
error;

async loadAccounts() {

try {

this.accounts = await getAccounts();

this.error = undefined;

} catch(error) {

this.error = error;

this.accounts = undefined;
}
}
}

HTML

<template>

<lightning-button
label="Load Accounts"
onclick={loadAccounts}>
</lightning-button>

<template if:true={accounts}>

<template
for:each={accounts}
for:item="acc">

<p key={acc.Id}>
{acc.Name}
</p>

</template>

</template>

</template>

This is one of the most common Salesforce LWC patterns.

Why async/await Is Preferred

Modern Salesforce projects usually use async/await because it improves readability.

Older promise syntax:

getAccounts()
.then(result => {

})
.catch(error => {

});

Modern approach:

async loadAccounts() {

try {

this.accounts = await getAccounts();

} catch(error) {

}
}

The async approach creates cleaner and more maintainable code.

Imperative Apex with Parameters

Most real-world applications pass parameters dynamically.

Apex Controller

public with sharing class ContactController {

@AuraEnabled
public static List<Contact> searchContacts(String searchKey) {

String keyword = '%' + searchKey + '%';

return [

SELECT Id, Name, Email
FROM Contact
WHERE Name LIKE :keyword
LIMIT 10
];
}
}

JavaScript

import { LightningElement }
from 'lwc';

import searchContacts
from '@salesforce/apex/ContactController.searchContacts';

export default class SearchComponent
extends LightningElement {

searchKey = '';
contacts;

handleChange(event) {

this.searchKey =
event.target.value;
}

async handleSearch() {

try {

this.contacts =
await searchContacts({

searchKey:
this.searchKey
});

} catch(error) {

console.error(error);
}
}
}

HTML

<template>

<lightning-input
label="Search Contact"
onchange={handleChange}>
</lightning-input>

<lightning-button
label="Search"
onclick={handleSearch}>
</lightning-button>

</template>

This pattern is heavily used in enterprise search components.

Imperative Apex execution flow in Salesforce LWC

Why Imperative Apex Is Required for DML Operations

The wire service cannot perform DML operations safely.

That means operations like:

  • insert
  • update
  • delete
  • upsert

should use imperative Apex.

Example:

@AuraEnabled
public static void updateAccount(Id accId) {

Account acc = [
SELECT Id, Name
FROM Account
WHERE Id = :accId
];

acc.Name = 'Updated';

update acc;
}

This is one reason imperative Apex is extremely important in real applications.

Error Handling in Imperative Apex

Good error handling improves user experience significantly.

Example

async handleSave() {

try {

await saveRecord();

} catch(error) {

console.error(error.body.message);
}
}

In production applications, developers often combine this with toast notifications.

You should also understand Apex Test Classes for Beginners: Step-by-Step Guide to Salesforce Testing because proper testing is essential for Apex controllers used in LWC.

Using Toast Messages with Imperative Apex

Toast messages are commonly used after successful operations.

import { ShowToastEvent }
from 'lightning/platformShowToastEvent';

Example:

this.dispatchEvent(

new ShowToastEvent({

title: 'Success',

message: 'Account Updated',

variant: 'success'
})
);

This creates a better UI experience for users.

Real Project Example

Suppose a sales team clicks:

“Generate Invoice”

The component may:

  • Call Apex imperatively
  • Validate records
  • Create invoice records
  • Generate PDF
  • Send API callout
  • Return success message

This kind of workflow cannot be handled properly using only @wire.

Enterprise Salesforce applications heavily depend on imperative Apex for these scenarios.

Imperative Apex and Governor Limits

Badly designed Apex calls can still hit governor limits.

Common issues include:

  • SOQL inside loops
  • Too many DML operations
  • Large data processing
  • Unoptimized queries

That is why understanding Salesforce governor limits overview is extremely important when building LWC applications.

Common Beginner Mistakes

Calling Apex Repeatedly

Avoid unnecessary server calls.

Not Handling Errors

Always use try/catch blocks.

Large Data Fetching

Avoid querying massive datasets unnecessarily.

Mixing Wire and Imperative Incorrectly

Use each approach for the correct use case.

Best Practices for Imperative Apex

Use async/await

Cleaner and easier to maintain.

Handle Errors Properly

Never ignore server exceptions.

Minimize Server Calls

Reduce unnecessary Apex requests.

Use Loading Indicators

Show spinners during processing.

Keep Apex Lightweight

Avoid heavy logic inside controllers.

Bulkify Apex Logic

Always design Apex for scalability.

Imperative Apex vs REST API Calls

Some developers confuse imperative Apex with direct API integration.

Imperative Apex:

  • Calls Salesforce Apex methods internally

REST API:

  • Connects Salesforce with external systems

If you want to learn integrations, also read Salesforce REST API Tutorial for Beginners with Real Integration Examples

Real Enterprise Use Cases

Imperative Apex is commonly used for:

  • Dynamic search
  • Save buttons
  • File upload processing
  • Payment integrations
  • PDF generation
  • AI response generation
  • External API integrations
  • Multi-step forms
  • Data validation workflows

This makes it one of the most important concepts in Lightning Web Components.

Salesforce imperative Apex enterprise workflow

Final Thoughts

Imperative Apex is one of the most important techniques in Salesforce Lightning Web Components because it gives developers full control over server-side execution.

While the @wire service is excellent for reactive read-only operations, imperative Apex becomes essential when handling:

  • user-triggered actions
  • DML operations
  • dynamic searches
  • custom workflows
  • complex business logic

Most enterprise Salesforce applications use a combination of both wire service and imperative Apex depending on the use case.

As you continue learning Lightning Web Components, mastering imperative Apex will help you build more scalable, interactive, and production-ready Salesforce applications.

FAQs

What is imperative Apex in LWC?

Imperative Apex is a manual way of calling Apex methods from Lightning Web Components using JavaScript.

When should I use imperative Apex?

Use imperative Apex when:

  • user interaction controls execution
  • DML operations are required
  • custom error handling is needed

Can imperative Apex perform DML operations?

Yes. Imperative Apex supports insert, update, delete, and upsert operations.

What is the difference between wire and imperative Apex?

@wire is reactive and automatic, while imperative Apex is manually controlled.

Does imperative Apex support async/await?

Yes. Modern LWC development commonly uses async/await with imperative Apex calls.

Share This Article
Salesforce Developer and Technical Writer
Follow:
Neha Panwar is a Salesforce developer and technical writer who shares practical tutorials, Apex guides, and real-world solutions for developers. She focuses on simplifying Salesforce concepts, integrations, and backend development to help beginners and professionals learn faster.
Leave a Comment