2

I have a class that gets an object that has a Currency field called Amount__c.

I am updating all my queries to work for a multi-currency org. So my query now becomes:

SELECT 
    Id, 
    Amount__c, 
    convertCurrency(Amount__c) Converted___Amount__c 
FROM Revenue__c 
WHERE xyz

In my test class, I need to check if that object is getting the converted currency alias, but I can't use .get cos it tells me that field doesn't exist on the Revenue__c object. Can anyone tell me how I check if the alias is on the object returned?

0

2 Answers 2

1

Remove the "__c" from your alias:

SELECT 
    Id, 
    Amount__c, 
    convertCurrency(Amount__c) ConvertedAmount 
FROM Revenue__c 
WHERE xyz

then you can use .get('ConvertedAmount')

3
  • Doesnt work. USed a different field than above (Annual_Revenue__c). I get an error: Invalid field ConvertedAnnualRevenue for Revenue__c Sep 23, 2019 at 19:33
  • Not sure what you're doing differently, this is working for me: list<Opportunity> lo = [SELECT Id, Amount, convertCurrency(Amount) cA FROM Opportunity]; system.debug(lo[0].get('cA'));
    – Ideasforce
    Sep 23, 2019 at 22:28
  • 1
    Yep, it appears the problem was caused by an issue earlier in my method. Thanks lads. Sep 24, 2019 at 15:33
2

You should probably check the field API name correctly. For your reference, all below work correctly:

SOQL 1:

for(Account acc: [SELECT Id, 
                  Name,
                  convertCurrency(AnnualRevenue)
                  FROM Account limit 10
                 ]) 
{
    System.debug('AnnualRevenue => '+acc.get('AnnualRevenue'));    
}

SOQL 2:

for(Account acc: [SELECT Id, 
                  Name, AnnualRevenue,
                  convertCurrency(AnnualRevenue) an__Rev
                  FROM Account limit 10
                 ]) 
{
    System.debug('an__Rev => '+acc.get('an__Rev'));    
}

SOQL 3:

for(Account acc: [SELECT Id, 
                  Name, AnnualRevenue,
                  convertCurrency(AnnualRevenue) an__Rev__c
                  FROM Account limit 10
                 ]) 
{
    System.debug('an__Rev__c => '+acc.get('an__Rev__c'));    
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .