+ 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
piyush_sonipiyush_soni
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

piyush_sonipiyush_soni
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!!