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
learn1.3947400898749973E12learn1.3947400898749973E12 

create a button to update a check box and button should work for both Classic and lightning

I need to create a button that should be update the checkbox and the button should be visible based on a field value. I created the JS button but it didnt work in Lightning. I am new to lightning, Can anyone help me to handle this scenario

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

var myquery = "SELECT Id,Hidden_Checkbox__c FROM SBQQ__Quote__c WHERE Id = '{!SBQQ__Quote__c.Id}' limit 1"; 

result = sforce.connection.query(myquery); 
records = result.getArray("records"); 

var myOpp = records[0]; 
var updateOpp = new Array(); 

var myValRev = myOpp.Hidden_Checkbox__c; 

if(myValRev == 'true'){ 
myOpp.Hidden_Checkbox__c=false; 

else 

myOpp.Hidden_Checkbox__c=true; 


updateOpp.push(myOpp); 
result = sforce.connection.update(updateOpp); 

if(result[0].getBoolean("success")){ 
window.location = "/" + "{!SBQQ__Quote__c.Id}"; 
}else{ 
alert('Could not Export : '+result); 
}
Khan AnasKhan Anas (Salesforce Developers) 
Hi,

Greetings to you!

For lightning, create a custom quick action of update record type and then set the checkbox field value to true in the predefined values section. Add the action to the layout. 
Now once you click on the action button, the checkbox value will be updated and the process will be triggered.
Or you can create a lightning component with a button and place it on a page through lightning app builder.

Component:
<aura:component controller="UpdateAccCheckboxC"
                implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" >
	
    <aura:handler event="force:refreshView" action="{!c.isRefreshed}" />
    
    <lightning:button variant="brand" label="Update A Checkbox" onclick="{!c.updateCheck}"  />
</aura:component>

Controller:
({
    updateCheck : function(component, event, helper) {
        var rid = component.get("v.recordId");
        var action = component.get("c.updateChk");
        action.setParams({key : rid});
        action.setCallback(this, function(response) {
            var state = response.getState();
            if (state === "SUCCESS") {
                $A.get('e.force:refreshView').fire();
            }
            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");
                }
            }
        });
        $A.enqueueAction(action);
    },
    
    isRefreshed: function(component, event, helper) {
        location.reload();
    },
})

Apex:
public class UpdateAccCheckboxC {

    @AuraEnabled
    public static void updateChk(String key){
        Account acc = [SELECT Id, Name, Cb__c FROM Account WHERE Id=:key];
        if(acc.Cb__c == false){
        	acc.Cb__c = true;
        }
        else{
            acc.Cb__c = false;
        }
        UPDATE acc;
    }
}

For classic, JavaScript button will work fine.

Also, please refer to the below links which might help you further with the above requirement.

https://developingflow.com/2015/03/16/update-a-record-through-a-button-and-salesforce-flow/

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
learn1.3947400898749973E12learn1.3947400898749973E12
Hello Khan,

Thanks for your update. The button opens in a new window when I click the button.

 User-added image