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?

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')

| improve this answer | |
  • Doesnt work. USed a different field than above (Annual_Revenue__c). I get an error: Invalid field ConvertedAnnualRevenue for Revenue__c – discodowney Sep 23 '19 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 '19 at 22:28
  • 1
    Yep, it appears the problem was caused by an issue earlier in my method. Thanks lads. – discodowney Sep 24 '19 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'));    
}
| improve this answer | |

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.