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
Linda 98Linda 98 

Help with Illegal assignment error!!

Hi.

I am having a Map
Map<String,Set<Id>> testmap= new map<String,Set<Id>>();
and values which i am giving to map are as below.
 if(testmap.containsKey(text)){
        
        testmap.get(text).add(u.id);
        }else{
          testmap.put(text,new Set<Id>{u.id})
}
Now i want to collect id value and assign it to a field on account and update it.But When i use values from map method,i am getting below error.

Illegal assignment from List<Set<Id>> to Id

for(account acc:trigger.new){
acc.customtextfield__c =testmap.values();
}
I understand my mistake that i am assigning Map value to list.Could you please guide how can i solve this?
 
RohRoh
Hello Linda,
Can you please add another iterative for loop to break the Set values and add to this field, as below.
 
for(id idSet: testmap.values())
{
for(account acc:trigger.new){
acc.customtextfield__c =idSet;
}
}

Please mark this as the best answer if it has answered your question.

Thanks,
Rohit Alladi
Linda 98Linda 98
Yes.I tried that.It says 
'Loop variable must be of type Set<Id>' for line below

for(id idset:testmap.values()){

}
RohRoh
Small Change Linda, use it this way
 
​for(set<id> idSet: testmap.values())
{
for(account acc:trigger.new){
acc.customtextfield__c =idSet;
}
}

Please mark this as the best answer if it has answered your question.

Thanks,
Rohit Alladi
Linda 98Linda 98
My bad.
its not acc.customtextfield__c
its customlookup field.

so i am having Illegal assignment from Set<Id> to Id error now.
sandeep reddy 37sandeep reddy 37
Hi
linda
so send me your coad and also tell me you are requirment clearly  thats not clear   first of all you have to know that we  can map onlysame  variable type fields
Thanks 
 
Linda 98Linda 98
Sandeep,
Yea.I agree.
All my rest code works fine.Just that i am having a Map<String,set<id>>   i am trying to get map.values() and assign to a lookup field on account then update account in my trigger.
And above is that part of code which i am having issue with.

Hope you could please take a look at above code and guide me .
Thank you.
RohRoh
Hello Linda,
You could save only one id value into the lookup field created, incase if you would want to store all the id values from a set, please create a text field and reference that.

Thanks,
Rohit Alladi
Linda 98Linda 98
But that is lookup field which will reference to other Object.I cant make it as textfield.
So do you mean i have to create a custom text field which will save id.then query that field and assign to my custom lookup field.?
Could you please be little more clear Rohit.???
Linda 98Linda 98
I want only one id to be saved.So one account gets on look up value.
Its like i am updating a object lookup field by fetching it in map.I am using map as i am comparing other fields and doing some data check and then saving in map with id.
 
RohRoh
Hello Linda,
So here's the thing, if you would want to create a lookup field and want the user to navigate to that record on a click , then you will be able to save only one id value into it. 
But, as per code i see you are wanting to save more than one value into that field, i.e. you will be needing a bigger field (customtext field) to incorporate all the values from the map into this . 

All the logic is totally based on the requirement you have. 

1 value into a field vs many values into a field.

Hope that helps.

Thanks,
Rohit Alladi
 
RohRoh
Hello Linda,
If you would want to save only one account value, please use it this way.
 
for(set<id> idSet: testmap.values())
{
  for(account acc:trigger.new){
  acc.customtextfield__c =idSet[0];
}
}

Hope this finds your answer.

Thanks,
Rohit Alladi