6

In classic we could use a custom button with some JS to create a simple toggle functionality.

Cf. Javascript Detail Page Toggle Button

What would be a valid lightning experience alternative (programmatic / declarative)

I have tried the Record Update Quick Action which works - yet it is 300% more clicks than a simple toggle button.

Basically I want the behaviour of the quick record update action (toast message, field value gets updated on lightning page) without the hassle of doing more than 1 click

2

It takes a fair amount of work to do almost anything in Lightning, so here's a basic framework to get you started. Note that this is meant to be a "same-as-previous-question-but-in-Lightning" answer, so interested readers might also want to check out this question. Advanced error handling is left as an exercise to the reader.

LightningMethods.cls

This class provides the query and update mechanisms that we need. Other methods would be ideal for a solid, reusable framework, but this should get you started.

public class LightningMethods {
    @AuraEnabled public static void updateRecords(SObject[] records) {
        update records;
    }
    @AuraEnabled public static SObject[] queryRecords(String query) {
        return Database.query(query);
    }
}

UpdateRecordComponent.cmp

Change the name of the component, as appropriate, plus all related data types.

<aura:component controller="LightningMethods" implements="force:hasRecordId,flexipage:availableForRecordHome">
    <aura:attribute name="record" type="SObject" />
    <aura:handler name="init" value="{!this}" action="{!c.doInit}" />
    <ui:button label="Update" press="{!c.updateRecord}" />
</aura:component>

UpdateRecordComponentController.js

This only includes the minimum error handling, but demonstrates how we can query and update records. Again, modify data types as needed for your purpose.

({
    doInit: function(component) {
        var action = component.get("c.queryRecords");
        action.setParams({
            query: "SELECT Id, No_Parent_Account__c FROM Account WHERE Id = '"+recordId+"'"
        });
        action.setCallback(this, function(result) {
            // Add error handling here
            if(component.isValid() && event.getState() === "SUCCESS") {
                component.set("v.record", result.getReturnValue()[0]);
            }
        });
        $A.enqueueAction(action);
    },
    updateRecord: function(component, event, helper) {
        var record = component.get("v.record")
            action = component.get("c.updateRecords");
        record.No_Parent_Account__c = !record.No_Parent_Account__c;
        action.setParams({
            records: [record]
        });
        action.setCallback(this, function(result) {
            // Add error handling here
            if(component.isValid() && event.getState() === "SUCCESS") {
                var toast = $A.get("e.force:showToast"),
                    refresh = $A.get("e.force:refreshView");
                toast.setParams({
                    title: "Record Updated",
                    message: "The record was updated successfully."
                });
                toast.fire();
                refresh.fire();
            }
        });
        $A.enqueueAction(action);
    }
})
  • Thats the way I did it, pls see my comment to crmprogdev's answer – Christian Szandor Knapp Feb 8 '17 at 15:00
  • 1
    @ChristianSzandorKnapp I forgot to add "force:refreshView", which may have been the part you were missing to begin with. – sfdcfox Feb 8 '17 at 15:03
  • Awesome! I tried force:recordUpdate but thats with Lightning Data Service only. My solution will be perfect if the body of the custom lightning action is not visible to the end user anymore. I fire $A.get("e.force:closeQuickAction").fire(); but the popup is still visible ever so shortly – Christian Szandor Knapp Feb 8 '17 at 15:06
  • @ChristianSzandorKnapp You could add the component somewhere on the page, and there would just be a button. No Quick Action necessary. That was sort of the design I was thinking of when I wrote this up. – sfdcfox Feb 8 '17 at 15:09
  • I am replacing standard buttons (edit, etc) with actions - so it seems if I don't want to rebuild the highlightspanel, im stuck with quick actions for now – Christian Szandor Knapp Feb 8 '17 at 15:16
2

As one option, you can create a Custom Quick Action which could include a Lightning component to run the equivalent JS as your original button would. Quick Actions can also include Invocable Apex as another option if that would serve your purpose.

  • I am using a custom quick action with lightning component currently. Things that are difficult: The modal of the quick action shows up even if height is set to 0px. That's ok but the record page does not get updated. So my button works, the toggle is executed but the result does not reflect on the record's home page – Christian Szandor Knapp Feb 8 '17 at 14:58
  • 2
    If you open a new question with your code, I'll be happy to look at it tomorrow. Will be out of the office the rest of the day. It sounds like one of the things you're not doing is setting the new value when you've finished updating the record. That tells other components/pages to refresh their values. If your page scrolls, there's a solution to the modal issue that was recently posted. – crmprogdev Feb 8 '17 at 15:16
  • I will post my solution later on so we can optimize it tomorrow, thx Thanks crmpprogdev and @sfdcfox – Christian Szandor Knapp Feb 8 '17 at 15:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.