Example of an Aura component in Salesforce

Here's an example to display a list of contacts related to an account and add new contacts and update existing ones.


<!-- ContactList.cmp -->
<aura:component controller="ContactController">
    <aura:attribute name="accountId" type="String" />
    <aura:attribute name="contacts" type="List" />
    <aura:handler name="init" value="{!this}" action="{!c.init}" />

    <ul>
        <aura:iteration items="{!v.contacts}" var="contact">
            <li>{!contact.Name}</li>
        </aura:iteration>
    </ul>

</aura:component>

==========================================================================

// ContactController.cls
public with sharing class ContactController {
    @AuraEnabled
    public static List<Contact> getContacts(String accountId) {
        return [SELECT Id, Name FROM Contact WHERE AccountId = :accountId];
    }
}

==========================================================================


// ContactListController.js
({
    init: function(component, event, helper) {
        var accountId = component.get("v.accountId");
        var action = component.get("c.getContacts");
        action.setParams({ accountId: accountId });
        action.setCallback(this, function(response) {
            var state = response.getState();
            if (state === "SUCCESS") {
                component.set("v.contacts", response.getReturnValue());
            } else {
                console.log("Failed to fetch contacts");
            }
        });
        $A.enqueueAction(action);
    }
})

==========================================================================

Add New Contact Functionality:

<!-- ContactList.cmp -->
<aura:component controller="ContactController">
    <!-- Existing code -->
    <!-- ... -->

    <!-- Add New Contact Form -->
    <div>
        <lightning:input aura:id="contactName" label="Name" />
        <lightning:input aura:id="contactEmail" label="Email" />
        <lightning:button label="Add Contact" onclick="{!c.addContact}" />
    </div>
</aura:component>

// ContactListController.js
({
    // Existing code
    // ...

    addContact: function(component, event, helper) {
        var accountId = component.get("v.accountId");
        var contactName = component.find("contactName").get("v.value");
        var contactEmail = component.find("contactEmail").get("v.value");

        var action = component.get("c.createContact");
        action.setParams({
            accountId: accountId,
            name: contactName,
            email: contactEmail
        });
        action.setCallback(this, function(response) {
            var state = response.getState();
            if (state === "SUCCESS") {
                // Refresh the contact list after adding a new contact
                helper.refreshContactList(component);
            } else {
                console.log("Failed to add contact");
            }
        });
        $A.enqueueAction(action);
    }
})

// ContactController.cls
public with sharing class ContactController {
    // Existing code
    // ...

    @AuraEnabled
    public static void createContact(String accountId, String name, String email) {
        Contact newContact = new Contact();
        newContact.AccountId = accountId;
        newContact.Name = name;
        newContact.Email = email;
        insert newContact;
    }
}

<!-- ContactList.cmp -->
<aura:component controller="ContactController">
    <!-- Existing code -->
    <!-- ... -->

==========================================================================


Update Contact Functionality:

    <!-- Update Contact Form -->
    <div>
        <lightning:input aura:id="updateContactName" label="Name" />
        <lightning:input aura:id="updateContactEmail" label="Email" />
        <lightning:button label="Update Contact" onclick="{!c.updateContact}" />
    </div>
</aura:component>

// ContactListController.js
({
    // Existing code
    // ...

    updateContact: function(component, event, helper) {
        var accountId = component.get("v.accountId");
        var contactId = component.get("v.selectedContactId");
        var contactName = component.find("updateContactName").get("v.value");
        var contactEmail = component.find("updateContactEmail").get("v.value");

        var action = component.get("c.updateContact");
        action.setParams({
            contactId: contactId,
            name: contactName,
            email: contactEmail
        });
        action.setCallback(this, function(response) {
            var state = response.getState();
            if (state === "SUCCESS") {
                // Refresh the contact list after updating a

Comments

Popular posts from this blog

Introduction to Salesforce Agent Script: Build Predictable AI Agents

Anti-Patterns in Salesforce Agentforce: What to Avoid When Building AI Agents

MCP vs. Traditional APIs: Choosing the Right Integration Approach