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
AkshiiiiiAkshiiiii 

input checkbox field

I need to create a checkbox input field and link that to a custom field. As in whatever input we give in the checkbox, that value should be stored in a custom checkbox field. How can I do this?
Best Answer chosen by Akshiiiii
ravi soniravi soni
hi,
try below dummy cmp.
public class updateAccountCtrl {
    @AuraEnabled
    public static void updateCheckboxField(string recId, boolean checkboxValue){
        Account Acc = new Account();
        Acc.Id = recId;
        Acc.CustomCheckboxField__c = checkboxValue;
       // Acc.Name = checkboxValue;
        update Acc;
            
        
    }

}
<aura:component implements="force:appHostable,flexipage:availableForAllPageTypes,
                            flexipage:availableForRecordHome,force:hasRecordId,
                            forceCommunity:availableForAllPageTypes,force:lightningQuickAction" 
                access="global" controller="updateAccountCtrl">
    <lightning:input type="checkbox" label="custom Checkbox" name="checkbox" aura:id="idCheckbox"/>
    <lightning:button name="click" label="Update" onclick="{!c.handleUpdate}"/>
	
</aura:component>

-------------------------------------------

({
	handleUpdate : function(component, event, helper) {
        var getValue = component.find('idCheckbox').get('v.value');
        var action = component.get("c.updateCheckboxField");
        action.setParams({ recId : component.get("v.recordId"),checkboxValue :  getValue});
        action.setCallback(this, function(response) {
            var state = response.getState();
            if (state === "SUCCESS") {
                alert("Update Successfully");
     }
            else if (state === "INCOMPLETE") {
                // do something
            }
            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);
    }
})

Add it on RecordDetailPage

don't forget to mark it as best answer.
Thank you
 

All Answers

ravi soniravi soni
hi Akshiiiii,
you can create a checkbox field as formula and value will be stored based on custom field.
1.) create a formula field returnType should be checkbox and in formula field put ref. of custom checkbox field. you only need put api name of that customfield and then save.
don't forget to mark it as best answer.
thank you
AkshiiiiiAkshiiiii
hi veer soni,
Thanks for your reply but this is not what I am looking for. I need to create a checkbox in aura component and whatever value the user gives in that checkbox should be stored in a custom field. How can I achieve this?
 
ravi soniravi soni
you can achieve this by calling apex class.
simply when user put data into checkbox field, that is in aura, at that time you can call apex method and update value.
If your cmp is added on recordPage then simply you can pass 2 params into apex first is recordId and second one is checkbox value.

if it helpful to you then don't forget to mark it as best answer.
Thank you

 
AkshiiiiiAkshiiiii
This sounds helpful. Could you please share a sample code for the same? 
ravi soniravi soni
hi,
try below dummy cmp.
public class updateAccountCtrl {
    @AuraEnabled
    public static void updateCheckboxField(string recId, boolean checkboxValue){
        Account Acc = new Account();
        Acc.Id = recId;
        Acc.CustomCheckboxField__c = checkboxValue;
       // Acc.Name = checkboxValue;
        update Acc;
            
        
    }

}
<aura:component implements="force:appHostable,flexipage:availableForAllPageTypes,
                            flexipage:availableForRecordHome,force:hasRecordId,
                            forceCommunity:availableForAllPageTypes,force:lightningQuickAction" 
                access="global" controller="updateAccountCtrl">
    <lightning:input type="checkbox" label="custom Checkbox" name="checkbox" aura:id="idCheckbox"/>
    <lightning:button name="click" label="Update" onclick="{!c.handleUpdate}"/>
	
</aura:component>

-------------------------------------------

({
	handleUpdate : function(component, event, helper) {
        var getValue = component.find('idCheckbox').get('v.value');
        var action = component.get("c.updateCheckboxField");
        action.setParams({ recId : component.get("v.recordId"),checkboxValue :  getValue});
        action.setCallback(this, function(response) {
            var state = response.getState();
            if (state === "SUCCESS") {
                alert("Update Successfully");
     }
            else if (state === "INCOMPLETE") {
                // do something
            }
            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);
    }
})

Add it on RecordDetailPage

don't forget to mark it as best answer.
Thank you
 
This was selected as the best answer
ravi soniravi soni
hi Akshiiiii,
try above sample example and tell us what is your status. if our solution is helpfull to you then don't forget to mark it as best answer so that it could help to others in future.

Thank you