+ Start a Discussion

onlcick javascript button in lightning

Guys, i have a onclick javascript button that validates fields, uses different templates based on field values.

var ca = new sforce.SObject("Case");
var p = "{!Case.Cons__c}";
var email = "{!Case.Cons_Email__c}";
var email2 = "{!Case.Cons_2_Email__c}";
var email3 = "{!Case.Cons_3_Email__c}";
var flc = "{!Case.Topic__c}";

if((email =="")&&(email2 =="")&&(email3 ==""))
alert("Please select cons and make sure cons has email address ");
else if(flc == ""){
else if(flc == "PC-AF") {
else if(flc != "PC-AF") {

How to make this button work on lightning, can someone please share the code, thank you.
Raj VakatiRaj Vakati

You can implement this button with Quick Actions in Case object. Refer this links.

I started with a simple validation first, and it's not working.
Here is the code i have written so far -

Component -
<aura:component controller="SimpleValidation" implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:lightningQuickAction,force:hasRecordId" access="global" >
    <aura:attribute name="CaseObj" type="Case" default="{ 'sobjectType' : 'case'}"/>  
    <lightning:button label="Redirect" onclick="{! c.handleClick}"/>
Controller -
handleClick: function(component, event, helper)
        var mycase = component.get("v.CaseObj");
        var action=component.get("c.FieldCheck");
            obj: mycase,
            oId : component.get("v.recordId")
            action.setCallback(this, function(response) {
            var state = response.getState();
            if(state === "SUCCESS")
                var flag= response.getReturnValue();
                 console.log('return value of flag ' + flag);
                 if(flag == true)
                var urlEvent = $A.get("e.force:navigateToURL");
                 console.log('my case id is '+ mycase.Id);
                        "url": "/_ui/core/email/author/EmailAuthor?p3_lkid={!mycase.Id}&p2_lkid={!mycase.ContactId}&p24={!mycase.Con_Email__c};{!mycase.Con_2_Email__c};{!mycase.Con_3_Email__c}&template_id=00X60000004dTuo&p26=test@gmail.com"});
                        urlEvent.setParams({ "isredirect": true });
                else if(flag == false)
                    alert('Please make sure address is not empty:');  
                    alert('Check the code:');
            else if (state === "INCOMPLETE") {
            else if (state === "ERROR") {
                var errors = response.getError();
                 if (errors) {
                    if (errors[0] && errors[0].message) {
                        console.log("Error custom message: " + errors[0].message);
                } else {
                    console.log("Unknown error");
Apex -
public with sharing class SimpleValidation {
    public static boolean FieldCheck(Case obj, String oId)
        if(obj.Con_Email__c == null){

            return false;
            return true;
    catch(Exception e)
        throw new AuraHandledException('Darn it! Something went wrong: ' + e.getMessage());   
The issues that i am having right now -
1) In the logs i see my case id is 'undefined'
2) The field Con_Email__c has an email address, but the flag value is returning 'false' instead of 'true'
3) If i fire the URL on false flag value , it gives me the below error.
Unable to Access Page
The value of the "p3_lkid" parameter contains a character that is not allowed or the value exceeds the maximum allowed length.
Remove the character from the parameter value or reduce the value length and resubmit.
If the error still persists, report it to our Customer Support team. Provide the URL of the page you were requesting as well as any other related information.

Can you help me on this, thank you.
Friends, is it possible to make this work in lightning? can somebody please help me with this, thank you.
Rosendo RodriguezRosendo Rodriguez
Hola tengo un boton JavaScript, como puedo ponerlo en cun componente para que funcione en lightnign?
 codigo del boton.

case "7. Apartado": 
window.open("{!URLFOR("../apex/Aletc_Formato_UnidadCondominal", "")}?id={!Opportunity.Id}"); 
case "9. Elaboración Contrato": 
window.open("{!URLFOR("../apex/Aletc_Formato_UnidadCondominal", "")}?id={!Opportunity.Id}"); 

alert("No se puede generar contrato para esta Oportunidad con desarrollo {!Opportunity.Desarrollo__c} y etapa {!Opportunity.StageName}, Solo está disponible para la etapa 9. Elaboración Contrato.");