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
Naofumi KunNaofumi Kun 

Lightning component: Make a checkbox True upon clicking the button

I created a Lightning Component that will update the value of the field "Submit Go-No Go Question?" from the Opportunity Object.

I created the following:
Apex Class - updateCheckBoxPlan
=================================
public class updateCheckBoxPlan {
@AuraEnabled
public static void updateCheck(){
list Pl_list = new list();
pl_list = [select id,Opportunity.Submit_Go_No_Go_Question__c from Opportunity Limit 1];
Opportunity p =new Opportunity();
p.id=pl_list[0].id;
p.Opportunity.Submit_Go_No_Go_Question__c=true;
update p;
}
}
=================================
Component - Submit
=================================
<aura:component implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,force:lightningQuickAction" access="global" controller="updateCheckBoxPlan">
    <aura:attribute name="updateCheckBox" type="Opportunity" default="{'sobjectType':'Opportunity'}"/>
    <lightning:button variant="brand" label="Confirm" onclick="{!c.updateCheck11}"  />
</aura:component>
=================================
Controller - SubmitGNGController
=================================
({
updateCheck11 : function(c, e, h) {
h.updateCheck11_helper(c,e,h);
},
})
=================================
Helper - SubmitGNGHelper
=================================
({
updateCheck11_helper : function(c,e,h) {
alert('Success!');
var save_action = c.get("c.updateCheck");
save_action.setParams({
});
$A.enqueueAction(save_action);
}
})
=================================

Any help is much appreciated!
Best Answer chosen by Naofumi Kun
Khan AnasKhan Anas (Salesforce Developers) 
Hi Anel,

Greetings to you!

Please try the below code, I have tested in my org and it is working fine. Kindly modify the code as per your requirement.

Apex:
public class updateCheckBoxPlan {

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

Component:
<aura:component controller="updateCheckBoxPlan"
                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();
    },
})

Place this component on record detail page using Lightning App Builder.

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​​​​​​​

All Answers

Khan AnasKhan Anas (Salesforce Developers) 
Hi Anel,

Greetings to you!

Please try the below code, I have tested in my org and it is working fine. Kindly modify the code as per your requirement.

Apex:
public class updateCheckBoxPlan {

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

Component:
<aura:component controller="updateCheckBoxPlan"
                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();
    },
})

Place this component on record detail page using Lightning App Builder.

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​​​​​​​
This was selected as the best answer
Madhavi B TMadhavi B T
Simply Superb really from so many days i was checking the code and many of the codes i have checked which have not worked, Thanks Khan Anas for sharing ,it was really helpful.