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
Gireesh A 7Gireesh A 7 

HI Basically i am changing the owner ship from one user to other user using trigger,i am getting error Error: Compile Error: Method does not exist or incorrect signature: void contains(Id) from the type User at line 21 column 11

trigger UpdateOwnerID on Case (after insert) {

 

List<Case> cases = new List<Case>();

map<Id,String> userIdbyAlias = new map<Id,String>();  //Keep in mind this will only store one user id per alias
    
    for(User u : [Select id,Name from user where IsActive = true AND Profile.Name = 'AH JP – Customer Request Agent'])
    {
            userIdbyAlias.put(u.Id,u.Name);
            system.debug('*** User names'+userIdbyAlias);
     }
  system.debug('**List of user from profile **'+userIdbyAlias);
   for (Case c : [Select Id, OwnerId, CreatedById From Case Where Id IN : Trigger.newMap.keySet()]) {

     for(User u :userIdbyAlias.keySet()){

     if(u.contains(c.OwnerId)) {

            c.OwnerId = userIdByAlias.get(u).Id;
            cases.add(c);
       }
  
     }
  }
   if(cases.size() > 0)
            update cases;
            system.debug('*** Final Output ***'+cases);
}
goabhigogoabhigo
u is not a list, hence you are getting that error. You don't need 2nd for loop.
for (Case c : [Select Id, OwnerId, CreatedById From Case Where Id IN : Trigger.newMap.keySet()]) {

     if(userIdByAlias.contains(c.OwnerId)) {
            c.OwnerId = userIdByAlias.get(c.OwnerId).Id;
            cases.add(c);
       }
     }

Does this work? Let me know.

--
Abhi
Gireesh A 7Gireesh A 7

i have tried before as you suggested but stil same  error i am getting 

Error: Compile Error: Method does not exist or incorrect signature: void contains(Id) from the type Map<Id,String> at line 19 column 23
 

v varaprasadv varaprasad
Hi Gireesh,

Again you are assigning same user id in the trigger.I think case owner id not contained in the map you need assign map key.

please check once below code : 

 
for (Case c : [Select Id, OwnerId, CreatedById From Case Where Id IN : Trigger.newMap.keySet()]) {

     if(userIdByAlias.containskey(c.OwnerId)) {
            c.OwnerId = userIdByAlias.get(c.OwnerId).Id;
            cases.add(c);
       }
     }


Hope this helps you!

Thanks
Varaprasad
@For Support: varaprasad4sfdc@gmail.com

 
RKSalesforceRKSalesforce
Hi Gireesh,

Please use below code. Either lne 13 or 14 will work.
trigger UpdateOwnerID on Case (after insert) {

	List<Case> cases = new List<Case>();
	map<Id,String> userIdbyAlias = new map<Id,String>();  //Keep in mind this will only store one user id per alias
	for(User u : [Select id,Name from user where IsActive = true AND Profile.Name = 'AH JP – Customer Request Agent'])
	{
		userIdbyAlias.put(u.Id,u.Name);
		system.debug('*** User names'+userIdbyAlias);
	}
	system.debug('**List of user from profile **'+userIdbyAlias);
	for (Case c : [Select Id, OwnerId, CreatedById From Case Where Id IN : Trigger.newMap.keySet()]) {
		if(userIdbyAlias.containsKey(c.OwnerId)) {
			c.OwnerId = userIdByAlias.get(u).Id;
			c.OwnerId = userIdByAlias.get(u.Id);
			cases.add(c);
		}
	}
	if(cases.size() > 0)
		update cases;
	system.debug('*** Final Output ***'+cases);
}
Please let me know if you need any help. Mark as best answer if helped.

Regrads,
Ramakant
 
Gireesh A 7Gireesh A 7

Finally i have ended up with below trigger ,can you please tell me how do i get the new user from map ,please finnd the below my comment where i getting error 

trigger UpdateOwnerID on Case (before insert,before update) {

map<Id,User> userIdbyAlias = new map<Id,User>();  // this will only store one user id per alias
    
    for(User u : [Select id,Name from user where IsActive = true AND Profile.Name = 'AH JP – Customer Request Agent'])
    {
            userIdbyAlias.put(u.Id,u);
            system.debug('*** User names'+userIdbyAlias);
     }
  system.debug('**List of user from profile **'+userIdbyAlias);
   for (Case c : [Select Id, OwnerId, CreatedById From Case

           Where Id IN : Trigger.new]) {

     if(userIdByAlias.containskey(c.OwnerId)) {
        if (c.OwnerId <> userIdByAlias.get(u).Id )
            c.OwnerId = userIdByAlias.get(u).Id; // getting error here 

Error: Compile Error: Variable does not exist: u at line 16 column 44
            system.debug('*** Chnaged user **'+c.OwnerId);
            system.debug('*** Assigning user ***'+userIdByAlias.get(u).Id);
           
       }
  
  }
  
}

goabhigogoabhigo
Hi Gireesh,

I don't know why the name of your map says alias but you are not storing any alias in it. However, please try the below code.
trigger UpdateOwnerID on Case (before insert,before update) {
    
    map<Id,User> userIdbyAlias = new map<Id,User>();  // this will only store one user id per alias
    
    for(User u : [Select id,Name from user where IsActive = true AND Profile.Name = 'AH JP – Customer Request Agent'])
    {
        userIdbyAlias.put(u.Id,u);
        system.debug('*** User names'+userIdbyAlias);
    }
    system.debug('**List of user from profile **'+userIdbyAlias);
    for (Case c : [Select Id, OwnerId, CreatedById From Case
                   Where Id IN : Trigger.new]) {
    	if(userIdByAlias.containskey(c.OwnerId)) {
        	if (c.OwnerId <> userIdByAlias.get(c.OwnerId).Id )
            	c.OwnerId = userIdByAlias.get(c.OwnerId).Id; // getting error here 
                system.debug('*** Changed user **'+c.OwnerId);
                system.debug('*** Assigning user ***'+userIdByAlias.get(c.OwnerId).Id);          
            }
        }
}

You were trying to access 'u' out of its scope, hence you got the error.

--
Abhi
Gireesh A 7Gireesh A 7
Hello Abhi ,

Can you confirm one thing here ,i have implememnted sharing rule ,does it impact for the above  role of profile users ,does it impact still we wright a trigger?
goabhigogoabhigo
If the new owner profile do not have permission (will need at least Read), then you will have issues. Otherwise I don't see any problem.

I am not going deeper into the business requirement. I hope you are well aware of it and know what you are doing with this trigger.

--
Abhi
Gireesh A 7Gireesh A 7
Yes i do agree ,here i am using only one profile and in that profile have 6 users ,we rae chnaging the ownershp from the case object among those users of same profile 
Gireesh A 7Gireesh A 7
for same profile there  are two role say A and B ,i have implemented sharing A role can share to role B ,when i come to the case record i am trying to chnage the ownership of X user of role A to Y user of role A( same role ) ,is that feasible from trigger? ,but currently i can  able to change the ownership from role A user to role B  user.
goabhigogoabhigo
Yeahbsolutely, its possible. There is almost everything possible with apex :)
You will have to query the users in that role, assign when the user ids dont match (between the queried users from the role vs the case owner user).
Gireesh A 7Gireesh A 7
okay,thanks.how do i querry and make it work of above sceanrio,as i new to salesforce
goabhigogoabhigo
Can you show what you have tried? Search for querying users based on role. Once you show the snippet here I will help you. If I post the code where's your learning?
Gireesh A 7Gireesh A 7

As you suggested i have tried with querry the user list based on role.look at the first for loop in the trigger

trigger UpdateOwnerID on Case (before insert,before update) {
    
    map<Id,User> userIdbyAlias = new map<Id,User>();  // this will only store one user id per alias
    
    for(User u : [Select id,Name,UserRole.Name from user where IsActive = true AND Profile.Name = 'AH JP – Customer Request Agent' AND UserRole.Name ='AH JP– Tech Advisor'])
    {
        userIdbyAlias.put(u.Id,u);
        system.debug('*** User names'+userIdbyAlias);
    }
    system.debug('**List of user from profile **'+userIdbyAlias);
    for (Case c : [Select Id, OwnerId, CreatedById From Case
                   Where Id IN : Trigger.new]) {
        if(userIdByAlias.containskey(c.OwnerId)) {
            if (c.OwnerId <> userIdByAlias.get(c.OwnerId).Id )
                c.OwnerId = userIdByAlias.get(c.OwnerId).Id; // getting error here 
                system.debug('*** Changed user **'+c.OwnerId);
                system.debug('*** Assigning user ***'+userIdByAlias.get(c.OwnerId).Id);          
            }
        }
}