0

I have the below javascript button, since the fields populate based on conditional picklist from parent object i can't create Quick action to pre-populate the field values, how can i use this in lightning

{!REQUIRESCRIPT("/soap/ajax/30.0/connection.js")}

var Status = '{!ELA__c.Status__c}';

if(Status == "Active"){
window.location = "/a02/e?retURL={!ELA__c.Id}&RecordType=01240000000M83B&
00N40000002Hvox=ELA&
CF00N40000003BTpq={!URLENCODE(ELA__c.Name)}&
00N40000002HvWZ=RTB";
}
else if(Status == "Return"){
window.location = "/a02/e?retURL={!ELA__c.Id}&
RecordType=01240000000M83B&00N40000002Hvox=IOT&
CF00N40000003BTpq={!URLENCODE(ELA__c.Name)}&
00N40000002oqS9={!URLENCODE(ELA__c.Returning_Items__c)}&
00N40000002HvWZ=RTB";
}
else if((Status == "New")|| (Status == "Completed") || (Status == "Cancelled") || (Status == "Pending - Finance")){
alert("You cannot create OM file with current ELA Status");
}
  • You should be able to do this w visual workflow and replace the button w a flow action. Flow can handle the logic easily. – gorav Sep 27 '19 at 22:36
  • If you did want to go the quick action route though, you could use a lightning component quick action and use a createRecord event in it and pass in the attributes that you want to prepopulate as parameters and then fire the event. – Roti Sep 27 '19 at 23:19
1

The strategy to replace the javascript button with a lightning alternative is pretty simple.

  1. Identify what your javaScript button is doing. In your case, it is a 2 step process. Checking the status of the record and based on status you are navigating user using window.location or showing an alert.
  2. There are many lightning alternatives available to cover the javascript functionality but you need to find the best-suited one. The best place to start is trailhead by doing this trail:- Lightning Alternatives to JavaScript Buttons
  3. In your case, you can use visualforce page, Aura Component, Lightning Web Component or flows.
  4. You can't use quick actions of type create a record or update a record as they will not allow you to show a conditional alert. Also, If you are thinking of using URL button, the URL hack will not work in Lightning.
  5. If you are going for Lightning Component, Your component will be as simple as below:-

init event is fired when when an app or component is initialized, prior to rendering. Handle this event using the aura:handler tag.

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

In your controller, you need to call an apex method to get the status of the current record and then use createRecord event by passing the default value of the field based on status value. You can show an alert using toast message if status is New or completed or cancelled or Pending - Finance.

var createAcountContactEvent = $A.get("e.force:createRecord");
createAcountContactEvent.setParams({
    "entityApiName": "Contact",
    "defaultFieldValues": {
        'Phone' : '415-240-6590',
        'AccountId' : '001xxxxxxxxxxxxxxx'
    }
});
createAcountContactEvent.fire();

Showing an error message using toast in the controller:-

    showToast : function(component, event, helper) {
        var toastEvent = $A.get("e.force:showToast");
        toastEvent.setParams({
            "type" : "error"
            "title": "Error!",
            "message": "You cannot create OM file with current ELA Status"
        });
        toastEvent.fire();

}
| improve this answer | |

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.