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
Dmitry KomarovDmitry Komarov 

Create a modal window and open it when new Activity (Task,Call,etc) is saved

Hello.
Could anyone help me with next:
When a new Activity is added to Account a modal dialog should open with:
Simple header like Update Account
2 radio buttons - Yes (default) \ No 
If selected 'Yes' - Then a picklist from Account object should be shown with it current value selected, so user can change value and update account on save
If No - no updates need and modal just close.

Thanks in advance
Dev-FoxDev-Fox
Hi Dmitry,
You need it in visualforce or lightning?
Dmitry KomarovDmitry Komarov
Hi Dev-Fox.
Thanks for your reply.
I need it in lightning
Dmitry KomarovDmitry Komarov
Hi there.
I'm already created a component and controller. So for now I need some help with next:
How to catch successful result after click on this button User-added image  and fire my component 

My component code:
<aura:component implements="flexipage:availableForRecordHome,force:hasRecordId"
                access="global" controller="accountFlightsCtrl">
    
    <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
    
    <aura:attribute name="recordId" type="Id"/>
    <aura:attribute name="objInfo" type="account" default="{sobjectType : 'Account'}" />
    <aura:attribute name="isOpen" type="boolean" default="true"/>
    <aura:attribute name="isYes" type="boolean" default="true"/>
    <aura:attribute name="options1" type="List" default=""/>
    <aura:attribute name="radioOptions" type="List" default="[
                                                        {'label': 'Yes', 'value': 'true'},
                                                        {'label': 'No', 'value': 'false'}
                                                        ]"/>
    <aura:attribute name="value" type="String" default="true"/>
    <aura:attribute name="sVal" type="String" default=""/>
    <aura:attribute name="flVal" type="String" default=""/>
    
    <!-- Modal Form -->
    <div class="slds-m-around_xx-large">
        <aura:if isTrue="{!v.isOpen}">
            <section role="dialog" tabindex="-1" aria-labelledby="modal-heading-01" aria-modal="true" aria-describedby="modal-content-id-1" class="slds-modal slds-fade-in-open slds-modal_medium">
				<header class="slds-modal__header">
                    <h2 id="modal-heading-01" class="slds-text-heading_medium slds-hyphenate">Update Est. Flights P.A</h2>
                </header>
                <div class="slds-modal__content slds-p-around_medium" id="modal-content-id-1">                    
                    <lightning:radioGroup name="radioGroup"
                                          label="{!'Is “Est. # Flights P.A” still: ' + v.sVal + '?'}"
                                          options="{! v.radioOptions }"
                                          value="{! v.value }"
                                          type="radio"
                                          onchange="{! c.handleChange }"/>
                    <aura:if isTrue="{!v.isYes}">
                        <div class="slds-form-element">
                            <label class="slds-form-element__label" for="select-01">Est. # Flights P.A</label>
                            <div class="slds-select_container">
                                <ui:inputSelect aura:id="accFlights" required="true" class="slds-select" change="{!c.onPicklistChange}"/>
                            </div>
                        </div>
                    </aura:if>
                </div>
                <!--###### MODAL BOX FOOTER Part Start ######-->
                <footer class="slds-modal__footer">
					<aura:if isTrue="{!v.isYes}">
                        <lightning:button variant="brand" 
                                          label="Save"
                                          title="Save"
                                          onclick="{! c.saveModal }"/>
                    	<aura:set attribute="else">
                            <lightning:button variant="neutral" 
                                      label="Close"
                                      title="Close"
                                      onclick="{! c.closeModal }"/>
                        </aura:set>
                    </aura:if>
                </footer>
            </section>
            <div class="slds-backdrop slds-backdrop_open"></div>
            <!--###### MODAL BOX Part END Here ######-->
        </aura:if>
    </div>
</aura:component>

Component js controller: 
 
({    
    doInit: function(cmp, event, helper) {
        //Retrieve current value of Account.FlightsPA__c
        helper.getCurrentFlightsValue(cmp);
        //Get list of options from picklist
        helper.fetchPickListVal(cmp, 'FlightsPA__c', 'accFlights');
    },
    onPicklistChange: function(cmp, event, helper) {
        // get the value of select option
        let fl = event.getSource().get("v.value");
        // Is input numeric?
        if (fl.length < 1) {
            // Set error
            event.getSource().set("v.errors", [{message:"You must select a value"}]);
        } else {
            // Clear error
            event.getSource().set("v.errors", null);
        }
        cmp.set('v.flVal', fl);
    },
    openModal: function(cmp, event, helper) {
        // for Display Model,set the "isOpen" attribute to "true"
        cmp.set("v.isOpen", true);
        cmp.set("v.isYes", true);
        cmp.find('accFlights').set("v.options", cmp.get('v.options1'));
    },
    closeModal: function(cmp, event, helper) {
        cmp.set("v.isOpen", false);
    },
    saveModal: function(cmp, event, helper) {
        //Update Accout Flights field
        let fl = cmp.get('v.flVal');
        if (fl.length < 1 || fl == null || fl == '' || fl == undefined) {
            // Set error
            cmp.find('accFlights').set("v.errors", [{message:"You must select a value"}]);
        } else {
            // Clear error
            event.getSource().set("v.errors", null);
            helper.updatePicklistVal(cmp, fl);
            cmp.set("v.isOpen", false);
        }
    },
    handleChange: function (cmp, event) {
        //Push options to select 
        cmp.set("v.isYes", event.getParam('value'));
        //Get current param and show\hide
        cmp.find('accFlights').set("v.options", cmp.get('v.options1'));
    },
})

Component js helper:
({
    fetchPickListVal: function(cmp, fieldName, elementId) {
        var action = cmp.get("c.getSelectOptions");
        action.setParams({
            "sObj": cmp.get("v.objInfo"),
            "fld": fieldName
        });
        var opts = [];
        action.setCallback(this, function(response) {
            if (response.getState() == "SUCCESS") {
                var allValues = response.getReturnValue();
                console.log(allValues);
                if (allValues !== undefined && allValues.length > 0) {
                    opts.push({
                        class: "optionClass",
                        label: "--- None ---",
                        value: ""
                    });
                }
                for (var i = 0; i < allValues.length; i++) {
                    opts.push({
                        class: "optionClass",
                        label: allValues[i],
                        value: allValues[i]
                    });
                }
                //cmp.find(elementId).set("v.options", opts);
                cmp.set("v.options1", opts);
            }
        });
        $A.enqueueAction(action);
    },
    
    getCurrentFlightsValue: function (cmp) {
        var action = cmp.get("c.getCurrentFlightsValue");
        action.setParams({
            "currentId": cmp.get("v.recordId"),
        });
        action.setCallback(this, function(response) {
            if (response.getState() == "SUCCESS") {
                var value = response.getReturnValue();
                cmp.set("v.sVal", value);
            } else {
                console.log(response);
            }
        });
        $A.enqueueAction(action);
    },
    
    updatePicklistVal: function (cmp, value) {
        var action = cmp.get('c.updateAccountFlights');
        action.setParams({
            "currentId": cmp.get("v.recordId"),
            "fl": value
        });
        action.setCallback(this, function(response) {
            if (response.getState() == "SUCCESS") {
                this.showToast();
            } else {
                console.log(response);
            }
        });
        $A.enqueueAction(action);
    },
    
    showToast : function(cmp, event, helper) {
        var toastEvent = $A.get("e.force:showToast");
        toastEvent.setParams({
            "title": "Success!",
            "message": "The record has been updated successfully."
        });
        toastEvent.fire();
    },
})

Apex class: 
public class accountFlightsCtrl {
    @AuraEnabled
    public static List < String > getSelectOptions(sObject sObj, string fld) {
        List < String > allOpts = new list < String > ();
        // Get the object type of the SObject.
        Schema.sObjectType objType = sObj.getSObjectType();
        
        // Describe the SObject using its object type.
        Schema.DescribeSObjectResult objDescribe = objType.getDescribe();
        
        // Get a map of fields for the SObject
        map < String, Schema.SObjectField > fieldMap = objDescribe.fields.getMap();
        
        // Get the list of picklist values for this field.
        list < Schema.PicklistEntry > values = fieldMap.get(fld).getDescribe().getPickListValues();
        // Add these values to the selectoption list.
        for (Schema.PicklistEntry a: values) {
            allOpts.add(a.getValue());
        }
        //allOpts.sort();
        return allOpts;
    }
    
    @AuraEnabled
    public static void updateAccountFlights(Id currentId, String fl)
    {
        Account acc = [SELECT Id,FlightsPA__c FROM Account WHERE Id = :currentId];
        acc.FlightsPA__c = fl;
        update(acc);
    }
    
    @AuraEnabled
    public static String getCurrentFlightsValue(Id currentId) {
        Account acc_fl = [SELECT FlightsPA__c FROM Account WHERE Id = :currentId];
        return acc_fl.FlightsPA__c;
    }
}