+ Start a Discussion
Eden_ZivEden_Ziv 

Apex Code > Transaction Wizard > Insert correct currency when creating a new record

Hi,

I have an Apex Code that I believe relates to creating a new record from a custom object in a transaction wizard.

This is part of the code:
 
// Create order
        Order__c newOrder = new Order__c(
            Status__c = 'Draft',
            Primary_Account__c = primary.get(0).record.Account.Id,
            Primary_Contact__c = primary.get(0).record.Id,
            Advisor__c = (primaryRole == 'Advisor' || primaryRole == 'Calling On Behalf Of Investor' ? investor.get(0).record.Id : null)
        );
        insert newOrder;

        // Create purchases
        List<Transaction__c> newTransactions = new List<Transaction__c>();
        List<Current_Holding__c> modifiedHolders = new List<Current_Holding__c>();
        List<String> newTransactionTypes = new List<String>();
        for (Purchase t : purchases) {
            for (PurchaseRecord p : t.purchases) {
                
                if (p.isValid()) {
                    
                    Transaction__c purchase = new Transaction__c(
                        RecordTypeId = t.record.RecordTypeId,
                        Order__c = newOrder.Id,                      
                        Transaction_Reason__c = t.record.Transaction_Reason__c,
                        Campaign_Source__c = t.record.Campaign_Source__c,
                        Gift_Card_Required__c = t.record.Gift_Card_Required__c,
                        Gift_Card_To__c = t.record.Gift_Card_To__c,
                        Gift_Card_From__c = t.record.Gift_Card_From__c,
                        Occasion__c = t.record.Occasion__c,
                        Registration_Type__c = t.record.Registration_Type__c,
                        Entry_Type__c = (t.record.Registration_Type__c == 'Registered Account' ? t.record.Entry_Type__c : 'Book Entry'),
                        Payment_Method__c = t.record.Payment_Method__c,
                        Issue__c = p.record.Issue__c,
                        Rate__c = p.record.Rate__c,
                        Units__c = p.record.Units__c,
                        Increment__c = 0,
                        Amount__c = p.bondDetails.Minimum_Subscription__c * p.record.Units__c,
                        CurrencyIsoCode = issueCurrencyMap.get(p.record.Issue__c)
                    );

In the part of order creation, I would like to add the update of the currency from the wizard, just like it is on the second part of the code.
However, I get an error when trying to copy and paste it from one part to another.
Note: the currency is currently shown as the default currency.

Any suggestions on how to make sure that the currency chosen in the wizard will be inserted in the 'order' record?

Thank you for your help!
Ryan GreeneRyan Greene
Add the currency and update newOrder outside of the for loop. So it would look like:
for(Purchase t:purchases){
for(PurchaseRecord p:t.purchases){
ALL YOUR OTHER CODE
}
}
newOrder.CurrencyType__c = CurrencyIsoCode;
update newOrder;Not sure what your currency field name is on the Order object so I put CurrencyType__c