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
thunksalotthunksalot 

How to increment a variable when a VF component is called, if not in setter method?

I'm trying to increment the Invoice Count on the Opportunity each time a new invoice is issued for the Opportunity.  The Invoice is a Visualforce Component that is called either by an VF Page or an VF Email Template (since the invoices can be downloaded as PDF or emailed as an attachment).

 

I see on this page of the VF documentation that is *not* a good practice to try and increment a variable within a Setter method.  But, the documentation doesn't suggest any alternative.  How can I accomplish this?  Note that since this is a PDF invoice, I can't use any Action methods.

 

Below is what I have now and here's the error I'm getting:

 

common.apex.runtime.impl.DmlExecutionException: Update failed. First exception on row 0 with id 006J00000039ZoeIAE; first error: UNABLE_TO_LOCK_ROW, unable to obtain exclusive access to this record: [] 

Here's my component's opening where the parameter is passed, kicking off the Set function below:

<apex:component access="global" controller="IAR_Controller" allowDML="TRUE" >
<!--  pass the Opportunity to the controller's opportunity object -->
<apex:attribute name="opportunityID" description="" type="ID" assignTo="{!opportunityID}"/>

 

Here's my controller set function:

    public Id opportunityID { 
        get; 
        set{
            //  When the OpportunityID is set by the component parameter tag, 
            //  it is now possible to query all the opportunity, transaction and lineitem info
            opportunityID = value;
            opportunity = [select Id, Invoice_Count__c from Opportunity WHERE Id = :this.opportunityID];
            transactions = [select Id from ChargentSFA__Transaction__c WHERE ChargentSFA__Transaction__C.ChargentSFA__Opportunity__c = :opportunity.ID];
            opportunitylineitems = [select Id from OpportunityLineItem where opportunityId = :opportunity.ID];

            // In case the Invoice Count has not defaulted to 1 for some reason and is therefore null,
            // set it to 1.
            if(opportunity.Invoice_Count__c == null){ opportunity.Invoice_Count__c = 1;}
           
            // Since this is really the initialization, increment the the Invoice Count now so that 
            // this Invoice will have one higher count than the previous one
            if(docType == 'invoice' && opportunity.Invoice_Count__C != null){
                opportunity.Invoice_Count__c = opportunity.Invoice_Count__c + 1;
                update opportunity;
            }
        }
    }