You need to sign in to do that
Don't have an account?
Referencing Unrelated Tables in a trigger
I'm getting stuck on referencing unrelated tables using a SOQL query.
For example I have a Leads table that has a country field, and I need to reference to a Mapping Table that contains the currency code for the country, and update the lead's currency code with the value from the Mapping table. How would I approach this?
I started with a map that looks like this: LeadMap<id, string> where the id is the lead's id and the string contains the country code. I'm not sure how to approach the rest of the problem as I really need to end up with a map of <id,id> that contains the lead Id and the currency id.
Any help will be greatly appreciated.
Key cannot be duplicated.
To handle this, here is one tricky way. I dont know whether this will work or not.
LeadMap<String,ID>
Keyvalue = LeadId + Country (Now it will be unique).
In your for loop, split the "keyvalue" to seperate leadId and Country (I think you can split after 18 character, since Salesforce ID will be always 18 character. Then the remaining part will be Country). Now you can get the currency for each country and put the LeadId and CurrencyId into the Map.
Hope this logic will help you.
Thanks
ashii
All Answers
hi,
Is there any relation between country (from Lead table) and currency code? How do you identify the currency code for country? For ex: If USA then its should be "$". How do you identify this?
Could you pls elaborate on this?
Find my understanding below:-
LeadID Country
01 USA
02 UK
03 IND
04 AUS
CurrID Country Currency
01 USA $
02 IND Rs
First table represents >> Leads table
Second table represents >> Currency table
If its like this then you can create a LeadMap<String,ID> where string is Country and Id is LeadID.
Next collection is CurrencyMap<String,Id> where string is Country and Id is CurrId.
//**** Get all the country from Leads
Set<String> S = LeadMap.keySet();
//**** Initialize Map to store the realtion
Map<Id,Id> MapRelationID = new Map<Id,Id>();
for(String sc : S)
{
if(CurrencyMap.containskey(sc))
{
MapRelationID.put(LeadMap.get(sc),CurrencyMap.get(sc));
}
}
Please check for the correct syntax, if I missed anything. Hope this will help you.
Thanks
ashii
Hi,
What you described below is almost what I'm looking for. However, will the lead map work if there are more than 1 key with the same value? ie USA appears on both LeadID 01 and 05:
LeadID Country
01 USA
02 UK
03 IND
04 AUS
05 USA
Key cannot be duplicated.
To handle this, here is one tricky way. I dont know whether this will work or not.
LeadMap<String,ID>
Keyvalue = LeadId + Country (Now it will be unique).
In your for loop, split the "keyvalue" to seperate leadId and Country (I think you can split after 18 character, since Salesforce ID will be always 18 character. Then the remaining part will be Country). Now you can get the currency for each country and put the LeadId and CurrencyId into the Map.
Hope this logic will help you.
Thanks
ashii
That's a smart way of approaching this. Thanks for you help!