By using this site, you agree to the Privacy Policy and Terms of Use.
Accept
horizontal-light horizontal-dark
  • Home
  • Tutorials
    • Salesforce AI
    • Salesforce DevOps
    • Career
    • Errors
    • Interview Questions
    • Salesforce Integration
    • Salesforce Flow
  • Salesforce Tools
  • Apex Development
  • Lightning Web Components
  • Salesforce Admin
  • About
    • Privacy Policy
    • Disclaimer
    • Terms & Conditions
    • Contact
SalesforceCornerSalesforceCorner
Search
  • Home
  • Tutorials
    • Salesforce AI
    • Salesforce DevOps
    • Career
    • Errors
    • Interview Questions
    • Salesforce Integration
    • Salesforce Flow
  • Salesforce Tools
  • Apex Development
  • Lightning Web Components
  • Salesforce Admin
  • About
    • Privacy Policy
    • Disclaimer
    • Terms & Conditions
    • Contact
Follow US
Salesforce Corner » Lightning Web Components » Imperative Apex Call in LWC with Real Project Examples
Lightning Web Components

Imperative Apex Call in LWC with Real Project Examples

Learn how to use Imperative Apex Calls in Salesforce LWC with real project examples, async/await, parameter passing, and best practices.

Neha Panwar
By
Neha Panwar
ByNeha Panwar
Salesforce Developer and Technical Writer
Neha Panwar is a Salesforce developer and technical writer who creates practical learning resources for Salesforce administrators and developers. She specializes in Salesforce Administration, Apex, Lightning...
Follow:
- Salesforce Developer and Technical Writer
Last updated: 2026/06/21
Share
Imperative apex call in LWC tutorial
SHARE

When building Lightning Web Components in Salesforce, developers often need to call Apex methods manually based on user actions like button clicks, form submissions, searches, or record updates. This approach is called an Imperative Apex Call in LWC.

Contents
What Is Imperative Apex Call in LWC?Why Developers Use Imperative Apex CallsImperative Apex vs Wire ServiceWhen Should You Use Imperative Apex?1. When User Action Triggers the Call2. When Performing DML Operations3. When You Need Full Control4. When Apex Method Is Not CacheableBasic Imperative Apex ExampleApex ControllerLWC JavaScriptLWC HTMLReal Project Example: Search ComponentApex ControllerLWC JavaScriptLWC HTMLPassing Parameters to Imperative ApexUsing async/await in Imperative ApexPromise-Based Imperative Apex ExampleError Handling in Imperative ApexImperative Apex for Record CreationApexLWC JavaScriptWhy Wire Service Cannot Handle DMLImperative Apex with Lifecycle HooksCommon Mistakes Developers MakeCalling Apex RepeatedlyNot Handling ErrorsPassing Wrong ParametersForgetting Async HandlingUsing Imperative Instead of Wire EverywhereBest Practices for Imperative ApexUse async/awaitMinimize Server CallsHandle Errors ProperlyUse Cacheable Methods When PossibleKeep Apex BulkifiedImperative Apex in Real Enterprise ProjectsWire vs Imperative: Which One Should You Learn First?Final ThoughtsFAQsWhat is imperative Apex call in LWC?When should developers use imperative Apex in LWC?Can imperative Apex perform insert and update operations?What is the difference between imperative Apex and @wire?Is async/await recommended for imperative Apex?

Unlike the wire service, imperative Apex gives developers full control over when the server call should happen.

If you already read Salesforce @wire Decorator in LWC with Real Examples, you probably noticed that @wire automatically fetches data reactively. But many real-world business scenarios require manual execution instead of automatic loading.

That’s where imperative Apex becomes extremely important.

In this guide, you’ll learn:

  • What imperative Apex calls are
  • When to use imperative Apex instead of @wire
  • Real project examples
  • Passing parameters to Apex
  • Error handling
  • Async/await usage
  • Best practices
  • Common developer mistakes
Imperative Apex Call in Salesforce LWC

What Is Imperative Apex Call in LWC?

An imperative Apex call means calling an Apex method manually using JavaScript.

Instead of Salesforce automatically invoking the method like @wire, developers explicitly decide:

  • when to call Apex
  • what parameters to pass
  • how to handle responses
  • how to handle errors

In simple words:

@wire = automatic reactive call
Imperative Apex = manual controlled call

This is one of the most important Lightning Web Components concepts for Salesforce developers.

If you are new to LWC basics, first read Salesforce LWC Lifecycle Hooks Explained with Real Examples because lifecycle methods are often used together with imperative Apex calls.

Why Developers Use Imperative Apex Calls

Imperative Apex calls are mainly used when developers need full control over execution.

Common use cases include:

  • Button click actions
  • Form submissions
  • Search functionality
  • Record creation
  • Record updates
  • Delete operations
  • Dynamic filters
  • Calling non-cacheable Apex methods
  • Loading data conditionally

For example:

A user clicks:

“Load Contacts”

Only then should Salesforce fetch records from Apex.

This behavior cannot always be handled properly using @wire.

Imperative Apex vs Wire Service

Many beginners get confused between these two approaches.

Here is the main difference.

FeatureImperative Apex@wire Service
ExecutionManualAutomatic
Best ForUser actionsReactive data
Supports DMLYesNo
Full ControlYesLimited
Async HandlingManualFramework-managed
Cacheable RequiredOptionalRequired

If you haven’t read it yet, also check:

Salesforce @wire Decorator in LWC with Real Examples

Understanding both approaches together is extremely important for interviews and real projects.

When Should You Use Imperative Apex?

Salesforce officially recommends imperative Apex in these scenarios:

1. When User Action Triggers the Call

Example:

  • Button click
  • Search click
  • Save action

2. When Performing DML Operations

Examples:

  • insert
  • update
  • delete
  • upsert

3. When You Need Full Control

You decide:

  • when to call
  • how often to call
  • what conditions to apply

4. When Apex Method Is Not Cacheable

@wire requires:

@AuraEnabled(cacheable=true)

But imperative Apex can call both:

  • cacheable methods
  • non-cacheable methods

Basic Imperative Apex Example

Let’s start with a simple real-world example.

Apex Controller

public with sharing class ContactController {

@AuraEnabled(cacheable=true)
public static List<Contact> getContacts() {

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

LWC JavaScript

import { LightningElement } from 'lwc';

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

export default class ContactList extends LightningElement {

contacts;
error;

async loadContacts() {

try {

this.contacts = await getContacts();

this.error = undefined;

} catch(error) {

this.error = error;

this.contacts = undefined;
}
}
}

LWC HTML

<template>

<lightning-button
label="Load Contacts"
onclick={loadContacts}>
</lightning-button>

<template if:true={contacts}>

<template for:each={contacts} for:item="con">

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

</template>

</template>

</template>

This is one of the most commonly used LWC patterns in Salesforce projects.

Real Project Example: Search Component

One of the best real-world use cases for imperative Apex is dynamic searching.

Apex Controller

public with sharing class AccountController {

@AuraEnabled(cacheable=true)
public static List<Account> searchAccounts(String searchKey) {

String key = '%' + searchKey + '%';

return [

SELECT Id, Name
FROM Account
WHERE Name LIKE :key
LIMIT 10
];
}
}

LWC JavaScript

import { LightningElement } from 'lwc';

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

export default class AccountSearch extends LightningElement {

searchKey = '';

accounts;

handleChange(event) {

this.searchKey = event.target.value;
}

async handleSearch() {

this.accounts =
await searchAccounts({

searchKey : this.searchKey
});
}
}

LWC HTML

<template>

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

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

</template>

This type of search functionality is heavily used in enterprise Salesforce applications.

Passing Parameters to Imperative Apex

One important rule:

Always pass parameters as objects.

Correct:

getAccounts({
searchKey : this.searchKey
});

Wrong:

getAccounts(this.searchKey);

Salesforce expects parameters in object format.

Using async/await in Imperative Apex

Modern Salesforce projects usually use:

async/await

instead of traditional promise chaining.

Benefits:

  • Cleaner code
  • Better readability
  • Easier error handling

Example:

async loadData() {

try {

this.records =
await getAccounts();

} catch(error) {

console.error(error);
}
}

Promise-Based Imperative Apex Example

Older projects may still use .then() and .catch().

Example:

getAccounts()

.then(result => {

this.accounts = result;

})

.catch(error => {

console.error(error);
});

Both approaches are valid.

However, async/await is cleaner and easier to maintain.

Error Handling in Imperative Apex

Proper error handling is extremely important.

Best practice:

Always use:

try/catch

Example:

try {

this.contacts =
await getContacts();

} catch(error) {

this.error = error.body.message;
}

Good error handling improves user experience significantly.

You should also understand Mixed DML Operation Error in Salesforce with Fixes because many imperative Apex operations involve DML statements.

Imperative Apex for Record Creation

Imperative Apex is commonly used for inserts and updates.

Apex

@AuraEnabled
public static void createAccount(String accName){

Account acc = new Account(
Name = accName
);

insert acc;
}

LWC JavaScript

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

async saveAccount() {

await createAccount({

accName : 'Test Account'
});
}

This type of pattern is used heavily in custom Salesforce forms.

Why Wire Service Cannot Handle DML

The @wire service only supports read operations.

Imperative Apex is required for:

  • insert
  • update
  • delete

because these operations modify Salesforce data.

That is why imperative Apex is extremely important in real-world development.

Imperative Apex with Lifecycle Hooks

Many developers combine imperative Apex with lifecycle hooks like:

connectedCallback()

Example:

connectedCallback() {

this.loadAccounts();
}

This pattern is very common.

Related tutorial:

Salesforce LWC Lifecycle Hooks Explained with Real Examples

Common Mistakes Developers Make

Calling Apex Repeatedly

Too many unnecessary server calls hurt performance.

Not Handling Errors

Unhandled exceptions create bad user experience.

Passing Wrong Parameters

Always pass parameters as objects.

Forgetting Async Handling

Imperative Apex is asynchronous.

Using Imperative Instead of Wire Everywhere

Sometimes @wire is the better choice.

Understanding both approaches is important.

Best Practices for Imperative Apex

Use async/await

Cleaner and modern syntax.

Minimize Server Calls

Avoid unnecessary Apex execution.

Handle Errors Properly

Always use try/catch.

Use Cacheable Methods When Possible

Improves performance.

Keep Apex Bulkified

Server-side logic should follow governor limit best practices.

You should also read:

Salesforce Governor Limits with Real Examples and Best Practices

because poorly designed Apex methods can easily hit limits.

Imperative Apex in Real Enterprise Projects

Large Salesforce applications commonly use imperative Apex for:

  • Search systems
  • Custom forms
  • Dynamic dashboards
  • Record updates
  • Integration triggers
  • File upload flows
  • Multi-step wizards
  • External API execution

This is why mastering imperative Apex is essential for every Salesforce LWC developer.

Wire vs Imperative: Which One Should You Learn First?

Learn both.

However:

  • @wire is easier for beginners
  • Imperative Apex gives more flexibility

Most enterprise Salesforce applications use a combination of both.

Related tutorials:

Salesforce @wire Decorator in LWC with Real Examples
Salesforce REST API Tutorial for Beginners with Real Integration Examples
Apex Trigger Tutorial for Beginners in Salesforce
Queueable Apex in Salesforce for Beginners: Complete Async Processing Guide
Batch Apex in Salesforce: Complete Guide with Real Examples

Final Thoughts

Imperative Apex calls are one of the most important concepts in Salesforce Lightning Web Components because they give developers full control over server communication.

Unlike the reactive wire service, imperative Apex allows developers to trigger Apex manually based on user actions, perform DML operations, pass dynamic parameters, and handle asynchronous processing more flexibly.

In real-world Salesforce projects, imperative Apex is widely used for:

  • forms
  • search functionality
  • record creation
  • updates
  • integrations
  • complex UI workflows

Once you understand imperative Apex properly, building scalable and interactive Lightning Web Components becomes much easier.

FAQs

What is imperative Apex call in LWC?

Imperative Apex means calling an Apex method manually using JavaScript instead of using the automatic @wire service.

When should developers use imperative Apex in LWC?

Developers should use imperative Apex when:

  • handling button clicks
  • performing DML operations
  • controlling execution manually
  • passing dynamic parameters

Can imperative Apex perform insert and update operations?

Yes. Imperative Apex supports:

  • insert
  • update
  • delete
  • upsert

operations.

What is the difference between imperative Apex and @wire?

@wire automatically fetches data reactively, while imperative Apex gives developers manual control over execution.

Is async/await recommended for imperative Apex?

Yes. Modern Salesforce projects commonly use async/await because it creates cleaner and more readable code.

TAGGED:Apex in LWCAsync Await LWCImperative Apexlightning web componentsLWC Apex Callsalesforce apexsalesforce developersalesforce lwcWire vs Imperative
Share This Article
Facebook Email Print
ByNeha Panwar
Salesforce Developer and Technical Writer
Follow:
Neha Panwar is a Salesforce developer and technical writer who creates practical learning resources for Salesforce administrators and developers. She specializes in Salesforce Administration, Apex, Lightning Web Components (LWC), Flow, integrations, and automation. Through Salesforce Corner, she publishes step-by-step tutorials, coding guides, and real-world solutions designed to help readers understand Salesforce concepts and apply them in projects with confidence.
Leave a Comment Leave a Comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Latest Post

Salesforce approval process diagram
Salesforce Approval Process: A Practical Guide with Real Business Examples
Salesforce Admin
Salesforce relationship comparison infographic
Master-Detail or Lookup? Choosing the Right Relationship in Salesforce
Salesforce Admin
Salesforce dynamic forms interface illustration
Salesforce Dynamic Forms: A Better Way to Show and Hide Fields
Salesforce Flow
Salesforce flow debugging guide
How to Debug and Fix Salesforce Flow Errors ?
Salesforce Flow
Salesforce Flow Loops tutorial diagram
Salesforce Flow Loops: Collections, Iteration, and Best Practices
Salesforce Flow

Stay Updated with Salesforce Tutorials

Get the latest Salesforce guides, tutorials, and developer tips delivered to your inbox.
slaesforce corner mascot

Explore More Topics

  • salesforce admin
  • salesforce developer
  • Salesforce Admin
  • salesforce tutorial
  • Salesforce Development
  • salesforce automation
  • salesforce apex
  • salesforce security
  • Apex Development
  • lightning web components
  • Lightning Web Components
  • Salesforce Tutorials
  • salesforce lwc
  • Salesforce Tools
  • Salesforce Beginner Guide
horizontal-dark-transparent

Learn Salesforce development with practical tutorials, Apex guides, integration examples, and real-world solutions for developers.

  • Quick Links:
  • About
  • Contact
  • Privacy Policy
  • Disclaimer
  • Terms & Conditions
Facebook Twitter Youtube Linkedin-in

Salesforce Corner © 2026

Welcome Back!

Sign in to your account

Username or Email Address
Password

Lost your password?