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
Arnas BaronasArnas Baronas 

Custom button to increment field value on click

Hello,
I am new to salesforce and I would like to know how to create custom button to update record's field value on click.

Should I use lightning component + apex code or is it possible achieve it without code?

Example - what I am trying to achieve:
FieldName:  Times clicked
FieldValue = 0 + 1 time clicked // sObject_c.TimesClicked++

Best Answer chosen by Arnas Baronas
Ajay K DubediAjay K Dubedi
Hi Arnas,

Use bellow code for set default look up value in the custom lightning component

Aura Comonent:
 
<aura:component implements="flexipage:availableForAllPageTypes,force:appHostable" access="global"> 
<aura:attribute name="TimesClicked" type="number" default=0/> 
<lightning:outputField fieldName="Phone" value="{!c.TimesClicked}"/>
<lightning:button variant="brand" label="Click for increment" onclick="{!c.increaseTimesClicked}"/>

</aura:component>

Controller:
({ 
    increaseTimesClicked: function(component, event, helper){ 
        var count = component.get('v.TimesClicked');
        count = count+1;
        component.set('v.TimesClicked', count);
    } 
})
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks,
Ajay Dubedi