function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
RaffusRaffus 

Aura component for updating the Status field of custom object.

I need to update the status(ak2397__Status__c) picklist field of the Custom Orders(ak2397__Orders__c) object using a lightning button. I have created a button 'cancel' and 'reorder'. If I click the cancel button then the status of the object should change from 'Ordered' to 'Cancelled' and if I click the reorder button then the status should change from 'Cancelled to 'Ordered'. I am stuck pls help.

User-added imageHere is my complete code
Component----
<aura:component controller="TestForOMapex" implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId" access="global" >
    <aura:attribute name="accList" type="List"/>  
    <aura:attribute name="activeSections" type="List" />
    <aura:attribute name="searchResult" type="List" description="use for store and display account list return from server"/>
    <aura:attribute name="searchKeyword" type="String" description="use for store user search input"/>
    <aura:handler name="init" value="{!this}" action="{!c.doinit}"/>
    <lightning:card title="Customer Order Details" iconName="standard:Contact"> 
        <div class="slds-border_bottom">
        </div>
        <div class="slds-scrollable_y">
            <lightning:layoutItem size="12">
                <div class="slds-text-longform">
                    <lightning:input value="{!v.searchKeyword}"
                                     placeholder="Search Customers"
                                     aura:id="searchField"
                                     name="searchField"
                                     onchange="{!c.onChange1 }"/>
                    <br/>  
                </div>
            </lightning:layoutItem>
        </div>
        <div class="slds-align_absolute-center">
            <table class="slds-table slds-table_cell-buffer slds-table_bordered">
                <thead>
                    <tr class="slds-line-height_reset">
                     
                         <th class="slds-text-title_caps" scope="col">
                            <div class="slds-truncate" title="Order ID">Order ID</div>
                        </th>
                        <th class="slds-text-title_caps" scope="col">
                            <div class="slds-truncate" title="Orders">Food Item</div>
                        </th>
                        <th class="slds-text-title_caps" scope="col">
                            <div class="slds-truncate" title="Order detail">Order Details</div>
                        </th>
                        <th class="slds-text-title_caps" scope="col">
                            <div class="slds-truncate" title="Order detail">Order Date</div>
                        </th>
                        <th class="slds-text-title_caps" scope="col">
                            <div class="slds-truncate" title="Order detail">Status</div>
                        </th>  
                        <th class="slds-text-title_caps" scope="col">
                            <div class="slds-truncate" title="Edit">Cancel</div>
                        </th>
                        <th class="slds-text-title_caps" scope="col">
                            <div class="slds-truncate" title="Edit">ReOrder</div>
                        </th>
                    </tr>
                </thead>
                <br/>
                <tbody>
                    <aura:iteration items="{!v.accList}" var="acc" indexVar="index">
                        <tr class="slds-line-height_reset">
                        
                            <td class="slds-text-title_caps" scope="col">
                                <div class="slds-truncate" title="FirstName">{!acc.ak2397__Order_Id__c}</div>
                            </td>
                            <td class="slds-text-title_caps" scope="col"> 
                                <div class="slds-truncate" title="Food Item"> {!acc.ak2397__Food_Item__c}</div>
                            </td>
                            <td class="slds-text-title_caps" scope="col">   
                                <div class="slds-truncate" title="Order Detail">{!acc.ak2397__Order_Details__c}</div>
                            </td>
                              <td class="slds-text-title_caps" scope="col">   
                                <div class="slds-truncate" title="Order Date">{!acc.CreatedDate}</div>
                            </td>
                              <td class="slds-text-title_caps" scope="col">   
                                <div class="slds-truncate" title="Status">{!acc.ak2397__Status__c}</div>
                            </td>
                            <td data-label="icon" class="slds-col slds-size_1-of-12" onclick="{!c.cancel}" id="{!acc.Id}">
                                <lightning:icon iconName="utility:delete" size="small" />
                            </td>
                             <td data-label="icon" class="slds-col slds-size_1-of-12" onclick="{!c.reorder}" id="{!acc.Id}">
                                <lightning:icon iconName="utility:change_record_type" size="small" />
                            </td>                            
                        </tr>
                    </aura:iteration>
                </tbody>
            </table>
        </div>
    </lightning:card>
</aura:component>
Js Controller
({
    onChange1: function (component, evt, helper) {
        var selectedLimit = component.find('searchField').get('v.value');
        component.set('v.searchKeyword', selectedLimit);
        helper.SearchHelper(component, event);
    },
        doinit: function(component) {
        var action = component.get('c.getContactData');
        var self = this;
        action.setCallback(this, function(actionResult) {
        component.set('v.acclist', actionResult.getReturnValue());
	});
		$A.enqueueAction(action);
	}, 
    cancel : function(component, event) {
        var action = component.get("c.cancelContact");
        action.setParams({contactId:event.target.id});
        action.setCallback(this, function(response) {
            component.set("v.accList",response.getReturnValue());
        });
        $A.enqueueAction(action);
    },
        reorder : function(component, event) {
            var action = component.get("c.updateContact");
            action.setParams({contactId:event.target.id});
            action.setCallback(this, function(response) {
                component.set("v.accList",response.getReturnValue());    
            });
            $A.enqueueAction(action);
        }, 
})

Helper
({
    SearchHelper : function(component, event, helper) {
        var action = component.get('c.fetchAcc');
         action.setParams({ searchKey : component.get("v.searchKeyword"),
                          });
        action.setCallback(this, function(response){
            var state = response.getState();
            if(state === "SUCCESS"){
                var allValues = response.getReturnValue();
                console.log("allValues--->>> " + JSON.stringify(allValues));
                //component.set('v.activeSections', allValues.Name);
                component.set('v.accList', allValues);
            }
            else if(state === "ERROR") {
                var errors = response.getError();
                if(errors){
                    if(errors[0] && errors[0].message){
                        console.log("Error Message: " + errors[0].message);
                    }
                }
                else{
                    console.log("Unknown Error");
                }
            }
        });
        $A.enqueueAction(action);
    }
});

Apex Controller
public class TestForOMapex {
  @AuraEnabled(cacheable=true)
    public static List<ak2397__Orders__c> fetchAcc (String searchKey){
        String query= 'select ak2397__Order_Id__c, ak2397__Food_Item__c, ak2397__Order_Details__c, CreatedDate, ak2397__Status__c from ak2397__Orders__c';
         if ( searchKey != Null ) {
            String key = '%' + searchKey + '%';
            query += ' WHERE Name LIKE :key';
        }system.debug(Database.query( query ));
         return Database.query( query );
    }
    @AuraEnabled
    public static List<ak2397__Orders__c> getContactData(){
        return [select Id from ak2397__Orders__c Limit 10];
    }
    @AuraEnabled
    public static List<ak2397__Orders__c> cancelContact(String contactId)
    {
        List<ak2397__Orders__c> upContact = [SELECT Id, ak2397__Status__c FROM ak2397__Orders__c];
        if(upContact.size()>0){
            for(ak2397__Orders__c om: upContact){
                om.ak2397__Status__c = 'Cancelled';
                upContact.add(om);
            }
            upsert upContact;
        }
        return getContactData();
    public static List<ak2397__Orders__c> updateContact(String contactId)
    {
        List<ak2397__Orders__c> upContact = [SELECT Id, ak2397__Status__c FROM ak2397__Orders__c];
        if(upContact.size()>0){
            for(ak2397__Orders__c om: upContact){
                om.ak2397__Status__c = 'Ordered';
                upContact.add(om);
            }
            upsert upContact;
        }
        return getContactData();
    }
}
}

 
PriyaPriya (Salesforce Developers) 

Hey Raffus,

What is the error you are getting ?

Thanks & Regards,

Priya Ranjan

RaffusRaffus
I am getting nothing, that is the problem