Create new opportunity and display in the list.

    component    

<aura:component controller="saveRecordClass">

     <aura:attribute name="homePage"  type="String"/>

    <aura:attribute name="accountsData" type="Opportunity[]"/>

    <aura:attribute name="newRec" type="boolean" default="false"/> 

    <aura:attribute name="showList" type="boolean" default="true"/>

    <aura:attribute name="data" type="Object"/>

    <aura:attribute name="columns" type="List"/>

    <aura:attribute name="recordId" type="String"/>

    <aura:attribute name="updatedRecord" type="Object[]" />

     <aura:handler name="init" value="{!this }" action="{!c.doInit }"/>

    <aura:if isTrue="{!v.showList}">

        <lightning:card>

            <aura:set attribute="actions">

                <lightning:button label="Create Account" onclick="{!c.handleCreate}" variant="Brand"/>

            </aura:set>

            <div align ="center" class="slds-box slds-box_small slds-theme_shade slds-theme_alert-texture" Style ="font-size:15px">ACCOUNT LIST</div>

            <lightning:datatable

                                 aura:id="accountDataTable"

                                 columns="{!v.columns}"

                                 data="{!v.data}"

                                 keyField="Id"

                                 onsave ="{!c.onSave}"

                                 hideCheckboxColumn="true"

                                 onrowaction="{!c.handleRowAction}" />

        </lightning:card>

    </aura:if>

    <aura:if isTrue="{!v.newRec}">

        <lightning:card title="Create New Opportunity" class="slds-var-m-around_xx-large slds-var-p-around_xx-large">

            <lightning:recordEditForm onsuccess="{!c.handleShowList}"  objectApiName="Opportunity">

                <lightning:inputField fieldName="Name" editable="true"/>

                <lightning:inputField fieldName="StageName" />

                <lightning:inputField fieldName="CloseDate" />

                <lightning:inputField fieldName="AccountId" />

                <div class="slds-m-top_medium">

                    <lightning:button variant="brand" type="submit" name="save" label="Save"/>

                </div>

            </lightning:recordEditForm>

        </lightning:card>

    </aura:if>

</aura:component>


    Controller.js    

({

    doInit : function(component, event, helper) {        

        component.set('v.columns', [

            {label: 'Name', fieldName: 'Name', editable:'true', type: 'text'},

            {label: 'StageName', fieldName: 'StageName', editable:'true', type: 'Picklist'},

            {label: 'CloseDate', fieldName: 'CloseDate', editable:'true', type: 'Date'},

            {label: 'AccountId', fieldName: 'AccountId', type: 'Lookup'}

        ]);        

        helper.fetchAccounts(component, helper);

    },

     onSave : function (component, event, helper) {

        helper.saveDataTable(component, event, helper);

    },

    handleCreate : function(component, event, helper)

    {

        component.set( 'v.newRec', true);

        component.set( 'v.showList', false);

    },

    

    handleShowList : function(component, event, helper)

    {

        component.set( 'v.newRec', false);

        component.set( 'v.showList', true);

        helper.fetchAccounts(component);

    },

})


    Helper.js    

({

    fetchAccounts : function(component, event, helper) 

    {

        var action = component.get("c.getAccounts");

        action.setCallback(this,function(response) {

            var state = response.getState();

            if (state === "SUCCESS")

            {

                component.set("v.data", response.getReturnValue());

            }

        });

        $A.enqueueAction(action);

    },

    

    saveDataTable : function(component, event, helper) 

    {

        var editedRecords =  component.find("accountDataTable").get("v.draftValues");

        var totalRecordEdited = editedRecords.length;

        var action = component.get("c.updateRecords");

        action.setParams({

            'editedOpportunityList' : editedRecords

        });

        action.setCallback(this,function(response) {

            var state = response.getState();

            if (state === "SUCCESS") {

                //if update is successful

                if(response.getReturnValue() === true){

                    helper.showToast({

                        "title": "Record Update",

                        "type": "success",

                        "message": totalRecordEdited+" Account Records Updated"

                    });

                } 

                else{

                this.showToast("ERROR","error",JSON.stringify(response.getError())); 

            }

            }

        });

        $A.enqueueAction(action);

    }

 })


    apex    

public class saveRecordClass {

    @AuraEnabled    

    public static List<Opportunity> getAccounts(string accName) {    

        return [SELECT Id, Name, StageName, CloseDate, Account.name, CreatedDate FROM Opportunity order by CreatedDate desc LIMIT 8];    

    }   

     @AuraEnabled

    public static boolean updateRecords(List<Opportunity> editedOpportunityList){

        try{

            update editedOpportunityList;

            return true;

        } 

        catch(Exception e){

            return false;

        }

    }

}

Comments

Popular posts from this blog

Write a trigger and it's Test class on Account when Account is update check all opportunity inside the account.

Populate Description field with the user first name who creates or updates the record by using userInfo standard keyword , and also does not allow user to delete the record. Write it's Test class also.

Dependent picklist in Aura