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
Matt FolgerMatt Folger 

N - 1 Apex Class code

I have a value in an object that I'm trying to create a button for the user to de-crement (N - 1).  This value tracks the progress of an record in the program and the N - 1 functionality would allow the existing department to send a record back one phase if they find that it isn't done to their standards (a quality control function).  Found out that this can't be done in VisualForce itself via something simple like a command button that just writes out "object__c.field__c - 1" or something to that effect, so need the controller method of how this would be done and can't seem to find it online here.  

The field in question is called "RPP_Tracker__c.Present_Dept_Number__c".  Any help much appreciated.
Best Answer chosen by Matt Folger
Nayana KNayana K
SUGGESTION 1:
Suppose below is your VF page:
<apex:page standardController="RPP_Tracker__c" extensions="TrackerController">
    <apex:form>
		<apex:commandButton action="{!decrement}" value="Decrement"/>
	</apex:form>
</apex:page>

Controller code:
public class TrackerController {
	public RPP_Tracker__c objTracker {get;set;} 
	
    public TrackerController(ApexPages.StandardController sc) {
        
		objTracker = [SELECT Id, Present_Dept_Number__c FROM  RPP_Tracker__c WHERE Id =: sc.getId()];
    }
	
	public PageReference decrement()
	{
		objTracker.Present_Dept_Number__c = objTracker.Present_Dept_Number__c - 1;
		update objTracker;
		
		PageReference pr = new PageReference('/'+objTracker.Id);
		return pr;
	}
}

SUGGESTION 2:
Instead of going for VF page, you can create a Detail page button with Onclick Javascript behaviour. On click of button decrement value and reload the page on successfull update and show error message if any failure.
JS code: 
{!REQUIRESCRIPT("/soap/ajax/36.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/36.0/apex.js")} 

var trackRec = new sforce.SObject("RPP_Tracker__c"); 
trackRec.Id = '{!RPP_Tracker__c.Id}'; 
var deptNumber = '{!RPP_Tracker__c.Present_Dept_Number__c}';
if(deptNumber != NULL && deptNumber != undefined)
{
	trackRec.Present_Dept_Number__c = deptNumber - 1;
	var result = sforce.connection.update([trackRec]); 
	if(result[0].getBoolean("success")) 
	{ 
		window.location.reload(); 
	} 
	else
	{ 
		var errorMessage = ''; 
		for (var i=0; i<result.length; i++) 
		{ 
			errorMessage += 'Error: ' + result[i].errors.message + ' \n'; 
		} 
		alert(errorMessage); 
	}
}
else
{
	alert('Dept num should have value or >0'); 
}

 

All Answers

Nayana KNayana K
SUGGESTION 1:
Suppose below is your VF page:
<apex:page standardController="RPP_Tracker__c" extensions="TrackerController">
    <apex:form>
		<apex:commandButton action="{!decrement}" value="Decrement"/>
	</apex:form>
</apex:page>

Controller code:
public class TrackerController {
	public RPP_Tracker__c objTracker {get;set;} 
	
    public TrackerController(ApexPages.StandardController sc) {
        
		objTracker = [SELECT Id, Present_Dept_Number__c FROM  RPP_Tracker__c WHERE Id =: sc.getId()];
    }
	
	public PageReference decrement()
	{
		objTracker.Present_Dept_Number__c = objTracker.Present_Dept_Number__c - 1;
		update objTracker;
		
		PageReference pr = new PageReference('/'+objTracker.Id);
		return pr;
	}
}

SUGGESTION 2:
Instead of going for VF page, you can create a Detail page button with Onclick Javascript behaviour. On click of button decrement value and reload the page on successfull update and show error message if any failure.
JS code: 
{!REQUIRESCRIPT("/soap/ajax/36.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/36.0/apex.js")} 

var trackRec = new sforce.SObject("RPP_Tracker__c"); 
trackRec.Id = '{!RPP_Tracker__c.Id}'; 
var deptNumber = '{!RPP_Tracker__c.Present_Dept_Number__c}';
if(deptNumber != NULL && deptNumber != undefined)
{
	trackRec.Present_Dept_Number__c = deptNumber - 1;
	var result = sforce.connection.update([trackRec]); 
	if(result[0].getBoolean("success")) 
	{ 
		window.location.reload(); 
	} 
	else
	{ 
		var errorMessage = ''; 
		for (var i=0; i<result.length; i++) 
		{ 
			errorMessage += 'Error: ' + result[i].errors.message + ' \n'; 
		} 
		alert(errorMessage); 
	}
}
else
{
	alert('Dept num should have value or >0'); 
}

 
This was selected as the best answer
Matt FolgerMatt Folger
I ended up going with a Boolean, that, if TRUE, triggers a workflow field update acommplishing the same thing.  =)  A workaround.