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
SKTSKT 

Apex Error: Illegal assignment from List<String> to String

I have a requirement to fetch Contact Region from Contact and update the same on User.

Here is my logic:
 
list<user> user = [SELECT name,Region__c from User where isactive = true];
  Set<string> str = new Set<string>();
        for(user u1 : user){
            str.add(u1.name);
        }

        List <Contact> Contact = [
 select id,Salesforce_User__c,Contact_Region__r.name  from contact where Contact_Region__c != null];
                                      
                                      
        Map<id, list<String>> usertcMap = new Map<id, list<String>> ();
        for (Contact Contacts : Contact) {
            if(!usertcMap.containsKey(Contacts.Salesforce_User__c))
                usertcMap.put(Contacts.Salesforce_User__c, new list<string>{Contacts.Contact_Region__r.name});
        else
            usertcMap.get(Contacts.Salesforce_User__c).add(Contacts.Contact_Region__r.name);
        }
        list<User> userupdates = new list<user>();
        for(User us : user){ 
            if(usertcMap.containsKey(us.id))
            {
                 
                list<string> allProsList =usertcMap.get(us.id);
                us.Region__c = allProsList;
                userupdates.add(us);
            }
        }

        if(!userupdates.IsEmpty())
            update userupdates;


now when i am trying to run the above logic, i am seeing this error:
Illegal assignment from List<String> to String

Can anyone please let me know how to overcome this error?
Thanks!
Best Answer chosen by SKT
Suraj Tripathi 47Suraj Tripathi 47

Hi SKT,

Please find the solution.

us.Region__c = string.valueOf(allProsList);

Please mark it as the Best If it helps you.

Thank You

All Answers

Maharajan CMaharajan C
Hi SKT,

Try the below code :
 
list<user> users = [SELECT Id,name,Region__c from User where isactive = true];
Set<Id> userIds = new Set<Id>();
for(user u1 : user){
	userIds.add(u1.Id);
}

List <Contact> Contacts = [select id,Salesforce_User__c,Contact_Region__r.name  from contact where Contact_Region__c != null and Salesforce_User__c IN: userIds];
							  
							  
Map<id, String> usertcMap = new Map<id, String> ();
for (Contact Con : Contacts) {
	usertcMap.put(Con.Salesforce_User__c , Con.Contact_Region__r.name);
}

list<User> userupdates = new list<user>();
for(User us : users){ 
	if(usertcMap.containsKey(us.id))
	{
		us.Region__c = usertcMap.get(us.id);
		userupdates.add(us);
	}
}

if(!userupdates.IsEmpty())
	update userupdates;

Thanks,
Maharajan.C
​​​​​​​
Suraj Tripathi 47Suraj Tripathi 47

Hi SKT,

Please find the solution.

us.Region__c = string.valueOf(allProsList);

Please mark it as the Best If it helps you.

Thank You

This was selected as the best answer
Maharajan CMaharajan C
Sorry i thoght you will have only one region so ihave changed the code. To remove the braces use the  String.join method with comma separator..

us.Region__c = String.join(allProsList,',');

Thanks,
Maharajan.C