You need to sign in to do that
Don't have an account?
Linda 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.
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 } }
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
<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>
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??
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>
mapTOSToUserId.get(text).add(u.id);
(hr=>{userid1,userid2,userid3},sales=>{userid1,userid2})
Map<String,Set<Id>> mapTOSToUserId = new map<String,Set<Id>>();
Thanks,
Suraj
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?