+ Start a Discussion
LiquidLiquid 

I have hit a wall, trying to update entire PriceBook Currency + Unit price accordingly

Hi Im trying to do something that sounds pretty simple to my clients...

Every quarter a new price book is generated, and sometimes they want to clone an existing PriceBook only with a different Currency + update the UnitPrice according to the exchange rate in the company's Currency table.

 

So this is what the client does, goes to a price book -> Creates a new price book from an existing one.

Goes to my SF page with the new pricebook ID in the QS, chooses the new currency and I would ideally want to change all the PriceBookEntry currency + unitprice.

 

In short, the error thrown at me is :

 

System.SObjectException: Field is not writeable: PricebookEntry.CurrencyIsoCode
Class.vfChangePriceBookEntriesCurrController.CommitChanges: line 43, column 9 External entry point

 

Here is my VF page:

 

 

 

 

<apex:page sidebar="false" controller="vfChangePriceBookEntriesCurrController">
    <apex:pageBlock title="{!pbName} change currency">
        <apex:form >
            <table style="width:100%">
                <tr>
                    <td>
                        Change currency to :
                    <apex:selectList id="CurrencyTo" style="width: 50px;" size="1" value="{!currencyTo}">
                        <apex:selectOptions value="{!allCurrencyISOOptions}"/>
                    </apex:selectList>
                    </td>
                </tr>
                <tr>
                    <td>
                        <apex:commandButton value="Commit" Action="{!CommitChanges}" />
                    </td>
                </tr>
            </table>        
        </apex:form>
    </apex:pageBlock>
</apex:page>

 

 

 

 

 

And the controller: 

 

public with sharing class vfChangePriceBookEntriesCurrController {
    public String pbName                                                    {get;set;}
    private String pbID                                                     {get;set;}
    public Pricebook2 pb                                                    {get;set;}
    public List<SelectOption> allCurrencyISOOptions                         {get;set;}
    public List<CurrencyType> allCurrencysDB                                {get;set;}
	public String currencyTo 												{get;set;}
    public vfChangePriceBookEntriesCurrController()
    {          
        //get id from qs
        if(ApexPages.currentPage().getParameters().get('id') != '' && ApexPages.currentPage().getParameters().get('id') != Null)
                pbID = ApexPages.currentPage().getParameters().get('id') ; 
        pb = [Select p.Name, (Select UseStandardPrice, UnitPrice, SystemModstamp,
        						 ProductCode, Product2Id, Pricebook2Id, Name, LastModifiedDate,
        						 LastModifiedById, IsDeleted, IsActive, Id, CurrencyIsoCode, CreatedDate,
        						 CreatedById From PricebookEntries) From Pricebook2 p where Id =: pbID ]; 
        if (pb!=null)
        pbName = pb.Name;   
        allCurrencyISOOptions = new List<SelectOption>();
        allCurrencysDB = new List<CurrencyType>();
        allCurrencysDB = [Select c.IsoCode, c.ConversionRate From CurrencyType c where IsActive=true and IsCorporate=false];
        if (allCurrencysDB!=null && allCurrencysDB.size()>0)
        	{
        		for(CurrencyType c : allCurrencysDB)
                	allCurrencyISOOptions.add(new SelectOption(c.IsoCode,c.IsoCode));
        	}
        
    }
    public PageReference CommitChanges()
    {
    	System.debug('In commit!');
	    if (currencyTo!=null && currencyTo!='')
	    {
	    	system.debug('Currency: ' + currencyTo);
	    	double rate = 0.0;
	    	for (CurrencyType ct : allCurrencysDB)
	    		if (ct.IsoCode == currencyTo)
	    			rate = ct.ConversionRate;
	    	if (rate!=0.0 && pb.PricebookEntries.size()>0)
	    	{
		    	for (PricebookEntry e : pb.PricebookEntries)
		    	{
		    		e.CurrencyIsoCode = currencyTo;
		    		e.UnitPrice = e.UnitPrice * rate;
		    	}
		    	update pb.PricebookEntries;
	    	}
	    }
	    return null;
    }
}

 

Any help is welcome! :)

Thanks in advance..!

 

 

LiquidLiquid

any ideas on how to solve this ? :smileysad: