function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
DHO_604DHO_604 

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.

Best Answer chosen by Admin (Salesforce Developers) 
ashiiashii

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

ashiiashii

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

DHO_604DHO_604

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

ashiiashii

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 

This was selected as the best answer
DHO_604DHO_604

That's a smart way of approaching this.  Thanks for you help!