function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Stephanie Boggs 17Stephanie Boggs 17 

Lightning Component to Replace Javascript Button

Hello! Thank you in advance for your assistance! I have been working on this for over 24 total changing the Controller and Component in small ways here and there to test and re-test, and I think I am going in circles at this point.

We have 8 custom Javascript buttons for our Cases - one per department. What these buttons do is allow the user to own the case with one click. It enters their name into a custom user lookup "owner" field for that department, updates their name to the standard Owner field, and updates the Status to Assigned. 

The current, working, custom JS button is:
{!REQUIRESCRIPT("/soap/ajax/13.0/connection.js")} 
var caseObj = new sforce.SObject("Case"); 
caseObj.Id = '{!Case.Id}'; 

caseObj.CST_Owner__c = '{!$User.Id}'; 
caseObj.OwnerId = '{!$User.Id}'; 
caseObj.Status = "Assigned"; 

var result = sforce.connection.update([caseObj]); 
window.location.href=window.location.href;

My Case Buttons Component is currently - this shows on the page and allows for button clicks and will sometimes throw an error and sometimes not but makes no changes:
<aura:component implements="flexipage:availableForAllPageTypes" access="global">
    <aura:attribute name="CaseObj" type="Case" />
        <lightning:button label="Own CST" onclick="{! c.owncst}"/>
</aura:component>

My Controller is currently:
({
owncst : function(component, event, helper) {
    var btnClicked = event.getSource();
    var CaseObj = component.get('v.CaseObj',true)
    component.set('v.CaseObj.Description', "true");
}})
Best Answer chosen by Stephanie Boggs 17
sfdcMonkey.comsfdcMonkey.com
hi Stephanie, use below lightning component code :

apex controller :
public class caseStatisChangeCtrl {
 @AuraEnabled 
    public static void updateStatus(case obj,String oId){
        system.debug('obj' + obj);
        case cc = obj;
        cc.Id = oId;
        update cc;
    }
}
component
<aura:component controller="caseStatisChangeCtrl" implements="flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,flexipage:availableForRecordHome,force:hasRecordId" access="global" >
	  <aura:attribute name="CaseObj" type="Case" default="{ 'sobjectType' : 'case'}"/>
      <lightning:button label="Own CST" onclick="{! c.owncst}"/>
</aura:component>
javaScript controller
({
  owncst : function(component, event, helper) {
      
      var caseObject = component.get("v.CaseObj");
        caseObject.OwnerId = $A.get("$SObjectType.CurrentUser.Id");
        caseObject.Status = 'Assigned';
       // update more fields here
       
      var action = component.get("c.updateStatus");
          action.setParams({
            obj: caseObject,
            oId : component.get("v.recordId")  
        });
      // set call back 
        action.setCallback(this, function(response) {
            
            var state = response.getState();
            if (state === "SUCCESS") {
                alert('update successful');
                $A.get('e.force:refreshView').fire();
            }
             else if (state === "INCOMPLETE") {
                alert("From server: " + response.getReturnValue());
            } else if (state === "ERROR") {
                var errors = response.getError();
                if (errors) {
                    if (errors[0] && errors[0].message) {
                        console.log("Error message: " + errors[0].message);
                    }
                } else {
                    console.log("Unknown error");
                }
            }
        });
        // enqueue the action
        $A.enqueueAction(action);
   }
})
output :
User-added image

case object not support the quick action button with lightning component

Let us know if it helps you, and kindly mark it best answer it this answer helps you so it make proper solution for others in future
thanks





 

All Answers

sfdcMonkey.comsfdcMonkey.com
hi Stephanie, use below lightning component code :

apex controller :
public class caseStatisChangeCtrl {
 @AuraEnabled 
    public static void updateStatus(case obj,String oId){
        system.debug('obj' + obj);
        case cc = obj;
        cc.Id = oId;
        update cc;
    }
}
component
<aura:component controller="caseStatisChangeCtrl" implements="flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,flexipage:availableForRecordHome,force:hasRecordId" access="global" >
	  <aura:attribute name="CaseObj" type="Case" default="{ 'sobjectType' : 'case'}"/>
      <lightning:button label="Own CST" onclick="{! c.owncst}"/>
</aura:component>
javaScript controller
({
  owncst : function(component, event, helper) {
      
      var caseObject = component.get("v.CaseObj");
        caseObject.OwnerId = $A.get("$SObjectType.CurrentUser.Id");
        caseObject.Status = 'Assigned';
       // update more fields here
       
      var action = component.get("c.updateStatus");
          action.setParams({
            obj: caseObject,
            oId : component.get("v.recordId")  
        });
      // set call back 
        action.setCallback(this, function(response) {
            
            var state = response.getState();
            if (state === "SUCCESS") {
                alert('update successful');
                $A.get('e.force:refreshView').fire();
            }
             else if (state === "INCOMPLETE") {
                alert("From server: " + response.getReturnValue());
            } else if (state === "ERROR") {
                var errors = response.getError();
                if (errors) {
                    if (errors[0] && errors[0].message) {
                        console.log("Error message: " + errors[0].message);
                    }
                } else {
                    console.log("Unknown error");
                }
            }
        });
        // enqueue the action
        $A.enqueueAction(action);
   }
})
output :
User-added image

case object not support the quick action button with lightning component

Let us know if it helps you, and kindly mark it best answer it this answer helps you so it make proper solution for others in future
thanks





 
This was selected as the best answer
Stephanie Boggs 17Stephanie Boggs 17
I just had a chance to put this in my sandbox and it works perfectly! Thank you, piyush_soni!!
LoneStar69LoneStar69
Hi Piyush, i have a smiliar requirement here https://developer.salesforce.com/forums/ForumsMain?id=9060G0000005QjNQAU
but couldn't get started on the controller part esp. on the validation in the controller, can you help me on 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; 
}