You need to sign in to do that
Don't have an account?
Raghu_dev
loop through the map object
Hi, Can some one help me how to loop through the map object?
For eg., I have the following map variable declared and populated the values.. I want to query the map variable at the later stages in the code and loop through the results. Please advice
Let me explain the requirement a little more..
I want to use the map as a resultset (or a recordset). Because of the governing rules in salesforce, I am trying to replace my repetitive soql statements align to maps and sets. Actual requirement is as follows.
There will be a huge load of leads from several vendors into our sfdc org. These leads will have DUNS numbers. The trigger on lead will have to look for the duns number on account (which was already populated from a different process) object and attach the lead to that account.
my earlier code was working perfect but because of the soql governing limits on trigger , I have to change that code to work.
Here is the actual code :
Here is the idea how to replace that code align to maps and sets .. pls advice if the approach is wrong
1. Get all the lead duns # and the Ids associated to those leads into a map
2. Query the account with the keys (duns) from the above map
3. The above query can result in bulk records as there are duplicate duns numbers in accounts.
4. Now, I have to loop through the leadmap to identify the account associated to that lead (as the duns on lead and duns on account matches)
---- How to query or search the map object ?
---- How to loop through the map object ?
5. Then populate the lead account id with the account id of the matching account record.
Please help
Thanks
Raghu
Message Edited by Raghu_dev on 09-08-2008 09:05 AM
Message Edited by Raghu_dev on 09-08-2008 09:46 AM
For eg., I have the following map variable declared and populated the values.. I want to query the map variable at the later stages in the code and loop through the results. Please advice
Code:
Map<String, Lead> leadMap = new Map<String, Lead>(); for (Lead lead : Trigger.new) { leadMap.put(lead.DUNS_NEW__c, lead); }
Let me explain the requirement a little more..
I want to use the map as a resultset (or a recordset). Because of the governing rules in salesforce, I am trying to replace my repetitive soql statements align to maps and sets. Actual requirement is as follows.
There will be a huge load of leads from several vendors into our sfdc org. These leads will have DUNS numbers. The trigger on lead will have to look for the duns number on account (which was already populated from a different process) object and attach the lead to that account.
my earlier code was working perfect but because of the soql governing limits on trigger , I have to change that code to work.
Here is the actual code :
Code:
trigger dunsOnLeads on Lead (before insert, before update) { for (Lead ld : Trigger.new) { String str = ld.DUNS_NEW__c; String mstrSfdcId = ''; Id sfdcId; // for customer identification String custmstrSfdcId = ''; Id custsfdcId; // for prospect identification String prosmstrSfdcId = ''; Id prossfdcId; System.debug('Duns on Lead ' + str); Integer custi=0; Integer prosi=0; Integer i=0; if (str != null) //find for null (or space) { // look for active customers for (Account tmpAccnt : [select Id, Master_SFDC_ID__c, MODS_Account_Type__c, customer_number__c from Account where IsDeleted = false and Duns__c = :ld.duns_new__c]) { if ((tmpAccnt.MODS_Account_Type__c == 'Customer') && (tmpAccnt.customer_number__c != '')) { custmstrSfdcId = tmpAccnt.Master_SFDC_ID__c; custsfdcId = tmpAccnt.Id; custi++; } else { prosmstrSfdcId = tmpAccnt.Master_SFDC_ID__c; prossfdcId = tmpAccnt.Id; prosi++; } } if (custi > 0) // there are no customers found with the same duns number { mstrSfdcId = custmstrSfdcId; sfdcId = custsfdcId; i = custi; } else if (prosi > 0) { mstrSfdcId = prosmstrSfdcId; sfdcId = prossfdcId; i = prosi; } // if no customers found with the same match, look for prospects /*if (i==0) { for (Account tmpAccnt : [select Id, Master_SFDC_ID__c from Account where IsDeleted = false and MODS_Account_Type__c = 'Prospect' and Duns__c = :ld.duns_new__c]) { mstrSfdcId = tmpAccnt.Master_SFDC_ID__c; sfdcId = tmpAccnt.Id; i++; } }*/ // if the account is identified as slave, master sfdc id is the unique id of the master. // in that case, lead will be attached to that master account. //System.debug('Master SFDC: ' + mstrSfdcId); //System.debug('Integer i: ' + i); //System.debug('SFDC Id: ' + sfdcId); if (i > 1 && mstrSfdcId == null) { ld.account__c = sfdcId; // Attach this lead to the last account from search and also, show this lead on the view ld.Dup_DUNS__c = True; } else if (i > 1 && mstrSfdcId != null) { ld.account__c = mstrSfdcId; } else if (i == 1 && mstrSfdcId != null) { ld.account__c = mstrSfdcId; } else if (i == 1 && mstrSfdcId == null && sfdcId != null) { ld.account__c = sfdcId; } else if (i==0) { ld.account__c = null; ld.Dup_DUNS__c = False; } } else { ld.account__c = null; ld.Dup_DUNS__c = False; } } }
Here is the idea how to replace that code align to maps and sets .. pls advice if the approach is wrong
1. Get all the lead duns # and the Ids associated to those leads into a map
Code:
Map<String, Lead> leadMap = new Map<String, Lead>(); for (Lead lead : Trigger.new) { leadMap.put(lead.DUNS_NEW__c, lead.Id); }
2. Query the account with the keys (duns) from the above map
Code:
for (Account newAccnt = new [select Id, Master_SFDC_ID__c, MODS_Account_Type__c, customer_number__c from Account where IsDeleted = false and Duns__c IN : leadMap.keyset()])
3. The above query can result in bulk records as there are duplicate duns numbers in accounts.
4. Now, I have to loop through the leadmap to identify the account associated to that lead (as the duns on lead and duns on account matches)
---- How to query or search the map object ?
---- How to loop through the map object ?
5. Then populate the lead account id with the account id of the matching account record.
Please help
Thanks
Raghu
Message Edited by Raghu_dev on 09-08-2008 09:05 AM
Message Edited by Raghu_dev on 09-08-2008 09:46 AM
Thanks for the response. Infact, I was just changing the code which was almost like the code you have published. I think I am stuck at the 3rd step.
This is what I want to do in the 3rd step. Since the step 2 might result in duplicate accounts (as duns could repeat on more than one account - duplicate accounts), I want to loop through the output (actually your suggestion would work if there is only one account with that duns). Here is the loop I am trying to put it together..
Please let me know if I am not clear.
For example if you query the account object for duns # 1234 and 2 accounts come back.
The map will store only the second account as the value pair. You never know of the other account when you are getting ready to use the map.
This is because the keys 1234 matched.
There is no need to do your other for loop because there will always be only one key value pair in a map.
Can I use Lists to store the duplicate accounts as well ?
Thank you so much for your response.
There is no such think as a duplicate account in salesforce, because every account, or any SObject, has it's own ID.
Making is not a dup.
So a list of accounts with the same duns number will work, and you can use list methods to get back all accounts with that duns number.
I cant query a list right ? (pls correct me if I am wrong). All I see in the documentation is List.get(index) .. is there a way I can query the list ?
http://wiki.apexdevnet.com/index.php/LeadDuplicatePreventer.apex
there are more to look thru on this page
http://wiki.apexdevnet.com/index.php/Apex_and_Visualforce