5

My org just recently switched from Classic to Lightning, and there's one limitation I'm having a lot of trouble working around. Before the transition, we had a lot of URL hack buttons linking to the standard page to create a new record. These, while not technically supported, worked just fine in classic, but break in LEX.

For the buttons at the top of the page, I've used Lightning Component quick actions that fire $A.get("event.force:createRecord") using defaultFieldValues in the params, which has worked like a charm. However, we have many of these buttons at the related list level, and in this case, custom Lightning Component buttons are not supported. (See this question. Also, on the roadmap, there is no planned release date yet as of May 2019 when I'm writing this.)

My first attempt was to create a URL button to link to a Visualforce page and use Lightning Out to embed the component that initiated the link, similar to what was described in this question. However, I'm back to square one, because I need to redefine the link in the javascript. If I navigate to a URL, I can't default the fields for the same reason I couldn't use the original button to begin with. Additionally, all navigation methods (as far as I know, anyway) in vanilla JS do not support defaulting.

Next, I tried to do the same thing with a direct link to a Lightning App from the URL button - like /c/MyLightningApp. However, I still needed to add the same <aura:dependency resource="markup://force:*" type="EVENT"/> to avoid an error, and when I did, the events seemed to be undefined in the way they would be in a VF page. Am I missing something?

Link, on URL list button on the related list of a custom object on the Opportunity page layout: /c/CreateRecord_App.app?accId={!Opportunity.AccountId}&oppId={!Opportunity.Id}

Lightning App:

<aura:application>
    <aura:attribute name="accId" type="String"/>
    <aura:attribute name="oppId" type="String"/>
    <c:CreateRecord accountId="{!v.accId}" opportunityId="{!v.oppId}"/>
    <aura:dependency resource="markup://force:*" type="EVENT"/> <!--When I didn't include this, it errored-->
</aura:application>

Lightning Component

<aura:component>
    <aura:attribute name="opportunityId" type="String"/>
    <aura:attribute name="accountId" type="String"/>
    <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
</aura:component>

Lightning Controller

({
    doInit : function(component, event, helper){
        var createRecordEvent = $A.get("event.force:createRecord");
        createRecordEvent.setParams({
            entityApiName: "Custom_Object__c"
            , defaultFieldValues: {
                Opportunity__c: component.get("v.opportunityId")
                , Account__c: component.get("v.accountId")
            }
        });
        //These are being populated correctly
        console.log(component.get("v.opportunityId"));
        console.log(component.get("v.accountId"));
        createRecordEvent.fire();
    }
})

At this point, I'm stuck. Is there something I'm missing in the approach above? Why would I need to define the dependency on an entirely Lightning-based approach, and why would I be unable to use the event functionality?

Also, is there a completely different approach to solving this dilemma that's eluding me?

  • so your question is you want to pre-default values for a list view button lets say opportunity list and create a new custom object record with pre-filled opportunity and account information? – RedDevil May 17 at 19:28
  • @RedDevil I'm looking to use a list button to link to the standard new screen with pre-filled values. – SFDC Neuf May 17 at 21:51
  • Do you need list view button or related list view button? – Pranay Jaiswal May 21 at 20:30
  • @PranayJaiswal related list view. – SFDC Neuf May 21 at 20:49
  • @SFDCNeuf Take a look at this question and answers there if that helps. – Jayant Das May 22 at 17:56
2
+100

For list buttons I would suggest using URL buttons in combination with lightning:isUrlAddressable interface. Using a component you are not moving outside of standard Lightning Application so there is no need to add any dependencies. So your url would look something like that:

/lightning/cmp/c__CreateRecord?c__accId={!Opportunity.AccountId}&c__oppId={!Opportunity.Id}

Remember to add your namespace (probably c__) before component's name and it's attributes. After this critical update was activated attributes without namespace are removed from pageReference's state.

  • This did end up working for me! A few things: it would be helpful to add that the parameters would be accessed via component.get("v.pageReference").state.<param>. Also, the c__ is not necessary on the parameters. – SFDC Neuf May 28 at 17:36
  • @SFDCNeuf it may work for you without c__ because you haven't activated critical update yet. Consider adding the prefix or you may have to do a re-work soon. Regarding accessing the parameter: I also tend to use something like that: <aura:attribute name="paramName" type="String" default="{!v.pageReference.state.c__paramName"/> but it's not always a valid way. – Bartosz Śliwiński May 28 at 19:02

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.