2

I am only able to set one-sided conversion: either Bitcoin to USD or USD to Bitcoin. But I want both-sided conversion to work which is creating deadlock in Apex. enter image description here

Following is the wrapper class:

public class DecoratedBitCoin{
    public BitCoin bitcoinInstance{get; private set;}
    private Decimal oneBitCoinToUsd;

    public DecoratedBitCoin(BitCoin bitcoinInstance, Decimal oneBitCoinToUsd){
        this.bitcoinInstance = bitcoinInstance;
        this.oneBitCoinToUsd = oneBitCoinToUsd;
    }

    public Decimal bitCoinRate{
        get{
            if (bitcoinInstance != null && bitCoinRate == null )
                bitCoinRate = bitcoinInstance.bitCoinRate;

            return bitCoinRate;
        }

        set{
            if(bitcoinInstance!=null && value!=null){
                usdRate = new RateConverter().newUsd(value, oneBitCoinToUsd);
            }
            bitCoinRate = value;
         }
    }


    public Decimal usdRate{
        get{
            if (bitcoinInstance != null && usdRate == null )
                usdRate = bitcoinInstance.usdRate;

            return usdRate;
        }

        set{
            if(bitcoinInstance!=null && value!=null){
                bitCoinRate = new RateConverter().newBit(value, oneBitCoinToUsd);
            }
            usdRate = value;
         }
    }

}

if I make usdRate = new RateConverter().newUsd(value, oneBitCoinToUsd); to bicoinInstance.usdRate = new RateConverter().newUsd(value, oneBitCoinToUsd);

then conversion works well - ie changing USD will show the corresponding change in Bit Coin on VF Page.

Following is the VF page-block code I am using:

Bit Coin: <apex:actionRegion ><apex:input type="number" id="bitcoin" value="{!instance.bitCoinRate}"  > <apex:actionSupport event="onkeyup" reRender="pageBlock"/></apex:input></apex:actionRegion>
USD: <apex:actionRegion ><apex:input type="number" id="usdcheck" value="{!instance.usdRate}" ><apex:actionSupport event="onkeyup" reRender="pageBlock"/></apex:input></apex:actionRegion>

Error: Maximum stack depth reached: 1001

Solution as suggested by Renato:

Define 4 variables: oldBitCoinValue, newBitCoinValue, oldUsdValue, newUsdValue "if the current value is different from the old value, then calculate the other one"

Updated code below:

public class DecoratedBitCoin{
        private BitCoin bitcoinInstance{get; private set;}
        private Decimal oneBitCoinToUsd;
        private Decimal oldBitCoinValue, newBitCoinValue, oldUsdValue, newUsdValue;

        public DecoratedBitCoin(BitCoin bitcoinInstance, Decimal oneBitCoinToUsd){
            this.bitcoinInstance = bitcoinInstance;
            this.oneBitCoinToUsd = oneBitCoinToUsd;
        }

        public Decimal bitCoinRate{
            get{
                if (bitcoinInstance != null && bitCoinRate == null )
                    bitCoinRate = bitcoinInstance.bitCoinRate;
                    oldBitCoinValue = bitCoinRate;
                return bitCoinRate;
            }

            set{
                if(bitcoinInstance!=null && value!=null){
                    newBitCoinValue = value;
                    if(oldBitCoinValue<>newBitCoinValue){
                        oldBitCoinValue = newBitCoinValue;
                        usdRate = new RateConverter().newUsd(value, oneBitCoinToUsd);
                    }
                }
                bitCoinRate = value;
             }
        }


        public Decimal usdRate{
            get{
                if (bitcoinInstance != null && usdRate == null )
                    usdRate = bitcoinInstance.usdRate;
                    oldUsdValue = usdRate;
                return usdRate;
            }

            set{
                if(bitcoinInstance!=null && value!=null){
                    newUsdValue = value;
                    if(oldUsdValue<>newUsdValue){
                        oldUsdValue = newUsdValue;
                        bitCoinRate = new RateConverter().newBit(value, oneBitCoinToUsd);
                    }
                }
                usdRate = value;
             }
        }

    }
3

I had to make a page that allowed users to apply a fixed or percent-based discount on an item, and it turned out similar to your issue.

What I did: created a wrapper for the item (because there were multiple items on the page, but that is not your case, so you don't need to do this), and created 4 variables: fixed_discount, old_fixed_discount, percent_discount and old_percent_discount.

On the Visualforce I used the tag <apex:actionSupport> with a onchange event so that when the user changes one value, the page would calculate the other.

The code was like "if the current value is different from the old value, then calculate the other one". I think this would apply to your code as well.

4
  • Thanks @Renato: I am trying to follow your solution. I created two variables for both of the setter implementations. I am still not able to solve it. I am updating the code in the post. Please suggest.
    – sanchit
    Mar 11 '17 at 21:04
  • I am always getting the null value in debug statements for bitCoinValue and usdValue.
    – sanchit
    Mar 11 '17 at 21:06
  • 1
    I am able to resolve the issue :) Thanks for your suggestions. I am posting the problem as well as solution in the post.
    – sanchit
    Mar 11 '17 at 21:27
  • Could you please upvote my question
    – sanchit
    Mar 11 '17 at 21:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.