You need to sign in to do that
Don't have an account?
anup-prakash
scenario For usage of Maps and sets
Hi I have been working on salesforce for quite a while now. Despite that I consider myself as a beginner.. Can you Please give me scenarios as to where I will have to use sets and Maps.. I have used Lists and I am comfortable with It but I have never faced a scenario as to when I have to use a Map and Set in differnt scenerio.. so that I could help me to understand the usage of these two collection elements..
Hey Anup,
I took Avidev9 code and optimized it further to use sets and maps appropriately. You can use a set or map, if you want to capture unique values. In this trigger / example, I elimated the several lines of iteration code by using sets and maps effectively.
If this post solves your problem kindly mark it as solution. if this post is helpful please throw Kudos.
All Answers
This will give you slight Idea why and when to use collections.
here we minimized the SOQL calls by taking the SOQL out of the loop.
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
trigger CheckDuplicate on Fiscal_Year__c (before insert, before update) {
for(fiscal_year__c fy: Trigger.new){
String fiscalYearName = fy.name;
String divisionName = fy.Division__c;
List<String> fiscalYearNameList = new list<String>();
for(fiscal_year__c fy1 : [select Name from fiscal_year__c where division__r.ID =: divisionName]){
fiscalYearNameList.add(fy1.Name);
}
for(String s : fiscalYearNameList){
if(fiscalYearName.equals(s)){
fy.Name.addError('Same Fiscal Year value already Present for this Division');
}
}
}
}
So is this wrong practice or is it okay?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
trigger CheckDuplicateAttachment on Attachment (before insert, before update) {
/* String fiscalYearID;
String divisionID; */
String attachName;
String attachParentID;
List<String> attachListNames = new List<String>();
for(Attachment A : Trigger.new){
attachParentID = A.ParentId;
attachName = A.Name;
for(Attachment Att: [Select Name from Attachment where parentID =: attachParentID ]){
attachListNames.add(Att.Name);
}
for(String s : attachListNames ){
if(attachName.equals(s)){
A.addError('Attachment by this Name already exists in this fiscal Year');
}
}
}
}
You have SOQL inside nested for Loop.
So if there are are say 200 records the inner loop will be called 200 times and hence 200 SOQL and you will get an exception
The link contains both bad and fixed/bulkified trigger
String attachName;
String attachParentID;
List<String> attachListNames = new List<String>();
for(Attachment A : Trigger.new){
attachParentID = A.ParentId;
attachName = A.Name;
}
for(Attachment Att: [Select Name from Attachment where parentID =: attachParentID ]){
attachListNames.add(Att.Name);
}
for(Attachment A : Trigger.new){
for(String s : attachListNames ){
if(attachName.equals(s)){
A.addError('Attachment by this Name already exists in this fiscal Year');
}
}
}
}
Is this code Okay now? If you could tell without being troubled..
I just wrote this for you in a notepad may not work. But contains all the logic you need it to work
PS : Am not troubled, I want you to understand the basics here
Hey Anup,
I took Avidev9 code and optimized it further to use sets and maps appropriately. You can use a set or map, if you want to capture unique values. In this trigger / example, I elimated the several lines of iteration code by using sets and maps effectively.
If this post solves your problem kindly mark it as solution. if this post is helpful please throw Kudos.