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 trigger!!!

I am having a text field on a user object. which holds values like(Not always) salels,services,marketing,HR,threat and sometimes just sales.
I am having a picklist field on account object which holds above values.....and only one is selected each time.
Based on both the fields i am trying to get id of object and use it on my account object.I am using that field in a trigger.

For example,my values in text field are services,marketing.when user selects marketing on account picklist,i need to check that on user's  and get id of user and use it for my further logic.

Below is my trigger.It works fine in above condition.

But when user selects HR from picklist and two users are having value HR and Threat in custom field.It is considering both the userids.
trigger updateuserdataonaccount on account(before insert,before update){
    Set<String> accrecords= new Set<string>();
    for(account pr : Trigger.new)
    {   
      if(string.isnotblank(pr.picklistfield))
     accrecords.add('%' + pr.piclistfield+'%');
    }
    
     Map<String,Id> mapTOSToUserId = new map<String,Id>();
    
    for(User u : [Select id,customtextfield__c from user
                  where customtextfield__c Like : accrecords])
    {
        for(String strService : accrecords)
        {
        strService = strService .substring(1,strService .length()-1);
    
            if(u.customtextfield__c .containsIgnoreCase(strService))
            {
            mapTOSToUserId.put(strService, u.Id);
            break;
            }
        }
    }
    for(account acc : Trigger.new)
        {   
   //Rest of trigger logic
        }
}



 
Best Answer chosen by Linda 98
Suraj PSuraj P
<pre>
trigger updateuserdataonaccount on account(before insert,before update){
 Set<String> accrecords= new Set<string>();
 for(account pr : Trigger.new)
 {
 if(string.isnotblank(pr.picklistfield)){
  accrecords.add('%' + pr.picklistfield+'%');
 }

 }

 Map<String,Set<Id>> mapTOSToUserId = new map<String,Set<Id>>();

 for(User u : [Select id,customtextfield__c from user
    where customtextfield__c Like :accrecords])
 {
  for(String strService : accrecords)
  {
    strService=strService.replaceAll('%',''));
    for(String text: u.customtextfield__c.split(',')){
        if(text==strService){
            if(mapTOSToUserId.containsKey(text)){
                mapTOSToUserId.get(text).add(u.id);
            }else{
                mapTOSToUserId.put(text,new Set<Id>{u.id});
            }
        }
    }
  }
 }
 for(account acc : Trigger.new)
  {
//Rest of trigger logic
  }
}
</pre>

All Answers

Suraj PSuraj P
The string 'hr' is matched in both 'HR' and 'Threat', since you have the wildcard match defined as '%hr%'. Assuming the values in the custom text field on the user object are comma separated, modify the code as:

<pre>
trigger updateuserdataonaccount on account(before insert,before update){
    Set<String> accrecords= new Set<string>();
    for(account pr : Trigger.new)
    {   
      if(string.isnotblank(pr.picklistfield)){
         accrecords.add('%,' + pr.picklistfield+',%');
         accrecords.add(pr.picklistfield+',%');
         accrecords.add('%,' + pr.picklistfield);
         accrecords.add(pr.picklistfield);
     }
     
    }
    
     Map<String,Id> mapTOSToUserId = new map<String,Id>();
    
    for(User u : [Select id,customtextfield__c from user
                  where customtextfield__c Like : accrecords])
    {
        for(String strService : accrecords)
        {
        strService = strService .substring(1,strService .length()-1);
    
            if(u.customtextfield__c .containsIgnoreCase(strService))
            {
            mapTOSToUserId.put(strService, u.Id);
            break;
            }
        }
    }
    for(account acc : Trigger.new)
        {   
   //Rest of trigger logic
        }
}
</pre>
Linda 98Linda 98
But picklistfield values are not comma separated.
Custom text field on User object are comma separated.
accrecords holds account data..Not user data.

Do you think i have to iterate over all users and save them in list/set as i am doing with account??
Suraj PSuraj P
<pre>
trigger updateuserdataonaccount on account(before insert,before update){
 Set<String> accrecords= new Set<string>();
 for(account pr : Trigger.new)
 {
 if(string.isnotblank(pr.picklistfield)){
  accrecords.add('%' + pr.picklistfield+'%');
 }

 }

 Map<String,Set<Id>> mapTOSToUserId = new map<String,Set<Id>>();

 for(User u : [Select id,customtextfield__c from user
    where customtextfield__c Like :accrecords])
 {
  for(String strService : accrecords)
  {
    strService=strService.replaceAll('%',''));
    for(String text: u.customtextfield__c.split(',')){
        if(text==strService){
            if(mapTOSToUserId.containsKey(text)){
                mapTOSToUserId.get(text).add(u.id);
            }else{
                mapTOSToUserId.put(text,new Set<Id>{u.id});
            }
        }
    }
  }
 }
 for(account acc : Trigger.new)
  {
//Rest of trigger logic
  }
}
</pre>
This was selected as the best answer
Linda 98Linda 98
I didnt get what is this method  

mapTOSToUserId.get(text).add(u.id);

 
Suraj PSuraj P
Adding a user id to the set of users that match a particular picklist value on the account records.For eg: if the value 'HR' is present on 3 user records, and 'Sales' is present on 2 of them, you're creating a map that looks like this:
(hr=>{userid1,userid2,userid3},sales=>{userid1,userid2})
Linda 98Linda 98
OK..got you! But I am having error saying method doesn't exist
Suraj PSuraj P
Which method are you getting the error on? Also confirm if you indeed declared the map as indicated: 
Map<String,Set<Id>> mapTOSToUserId = new map<String,Set<Id>>();

Thanks,
Suraj
Linda 98Linda 98
Thanks a lot.It works .
Linda 98Linda 98
When i use values from map method,i am getting below error.
Illegal assignment from List<Set<Id>> to Id

I am actually updating acc field with id .

for(account acc:trigger.new){
acc.customtextfield__c =mapTOSToUserId.values();
}
I understand my mistake that i am assigning Map value to list.Could you please guide how can i solve this?