+ Start a Discussion
LoneStar69LoneStar69 

onlcick javascript button in lightning

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

{!REQUIRESCRIPT("/soap/ajax/19.0/connection.js")}
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 == ""){
location.replace('/_ui/core/email/author/EmailAuthor?p3_lkid={!Case.Id}&p2_lkid={!Case.ContactId}&p24={!Case.Cons_Email__c};{!Case.Cons_2_Email__c};{!Case.Cons_3_Email__c}&template_id=00X60000004dTuo&p26=cons@gmail.com');
}
else if(flc == "PC-AF") {
location.replace('/_ui/core/email/author/EmailAuthor?p3_lkid={!Case.Id}&p2_lkid={!Case.ContactId}&p24={!Case.Cons_Email__c};{!Case.Cons_2_Email__c};{!Case.Cons_3_Email__c}&template_id=00Xt0000000LxEP&p26=cons@gmail.com');
}
else if(flc != "PC-AF") {
location.replace('/_ui/core/email/author/EmailAuthor?p3_lkid={!Case.Id}&p2_lkid={!Case.ContactId}&p24={!Case.Cons_Email__c};{!Case.Cons_2_Email__c};{!Case.Cons_3_Email__c}&template_id=00Xt0000000LxEt&p26=cons@gmail.com');
}

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

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

https://trailhead.salesforce.com/modules/lex_javascript_button_migration/units/javascript_button_alternatives
https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/lightning_component_actions.htm
LoneStar69LoneStar69
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}"/>
</aura:component>
Controller -
({
handleClick: function(component, event, helper)
    {
        var mycase = component.get("v.CaseObj");
        var action=component.get("c.FieldCheck");
        action.setParams({
            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);
                   
                    urlEvent.setParams({
                        "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 });
               
                urlEvent.fire();
                   }
                else if(flag == false)
                {
                    alert('Please make sure address is not empty:');  
                }
                    else{
                    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");
                }
                           }
        });
        $A.enqueueAction(action);
    }
})
Apex -
public with sharing class SimpleValidation {
 @AuraEnabled    
    public static boolean FieldCheck(Case obj, String oId)
    {
      try
    {
        if(obj.Con_Email__c == null){

            return false;
        }
        else{
            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.
LoneStar69LoneStar69
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.

switch("{!Opportunity.StageName}"){ 
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}"); 

break; 
default: 
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."); 
break; 
}