+ Start a Discussion
Jim Parker 7Jim Parker 7 

Update ID Values

So I am having an issue updating a variable.  In essence, I want to choose a "User" based upon a condition.  If the case has an employee email address on it, then use the User that has that email address.  Otherwise, use the CreatedById.  I have a User record called finalUser.  I created finalUser early and then am trying to update the finalUser.Id based upon those two scenarios.  I verified that I have an valid ID before setting finalUser equal to that but when I check to see if finalUser was updated after with a debug, the variable is still set to null.  I am new to SOQL so I am clearly doing an assignment incorrectly... Help!  I commented where it is broken. 
trigger CaseTeamTrigger on Case (after insert) {
/**
   * Comments:
   * To give visiblity into the Case records that were created by a User, we 
   * will add them to the CaseTeamMember list when a particular record is 
   * updated. The user should only be added to the CaseTeamMember table once.
   * 
   * Additionally, add case teams based on user role. 
   */
   
    Map<Id, CaseTeamMember> membersToAdd = new Map<Id, CaseTeamMember>();
    Map<Id, CaseTeamTemplate> teamsToAdd = new Map<Id, CaseTeamTemplate>();
    List<User> employeeEmailSubmitter;
    Id employeeEmailID;
    User finalUser = new User();
    UserRole userRoleID = new UserRole();
    
    //Finds the case teams
	list <CaseTeamTemplate> bethesdaTeam = [Select Id, Name from CaseTeamTemplate where Name ='Bethesda Team' limit 1];
    list <CaseTeamTemplate> busDevTeam = [Select Id, Name from CaseTeamTemplate where Name ='Business Development Team'  limit 1];
    list <CaseTeamTemplate> commDepTeam = [Select Id, Name from CaseTeamTemplate where Name ='Commerical Deposits Team'  limit 1];
    list <CaseTeamTemplate> dcTeam = [Select Id, Name from CaseTeamTemplate where Name ='DC Team'  limit 1];
    list <CaseTeamTemplate> hcDepTeam = [Select Id, Name from CaseTeamTemplate where Name ='Healthcare Deposits Team' limit 1];
    list <CaseTeamTemplate> herndonTeam = [Select Id, Name from CaseTeamTemplate where Name ='Herndon Team' limit 1];
    list <CaseTeamTemplate> itTeam = [Select Id, Name from CaseTeamTemplate where Name ='IT Help Desk' limit 1];
    list <CaseTeamTemplate> potomacTeam = [Select Id, Name from CaseTeamTemplate where Name ='Potomac Team' limit 1];
    list <CaseTeamTemplate> rockvilleTeam = [Select Id, Name from CaseTeamTemplate where Name ='Rockville Team' limit 1];
    list <CaseTeamTemplate> corpLenders = [Select Id, Name from CaseTeamTemplate where Name ='Corporate Lending Team' limit 1];
    list <CaseTeamTemplate> finalTeam;
    
    //Finds the case triggered
    List<Case> cases = [Select Id, OwnerId, CreatedById, Case_Creator_Employee_Email__c 
                  from Case where id IN :Trigger.newMap.keySet()];
    
    //Determine case creater
    for (Case c : cases) {
		employeeEmailSubmitter = [Select ID, Email from User where Email =: c.Case_Creator_Employee_Email__c limit 1];
        //if no results returned with an employee email match, then use CreatorID
        if (employeeEmailSubmitter.isEmpty() && c.CreatedbyId != null)  {
            System.debug('*******************************************'); 
            System.debug('Made it to the no employee email case - ' + c.CreatedById);
//THIS DOESN"T WORK
        	finalUser.Id =c.CreatedById ;
            update finalUser;
            System.debug('FinalUser ID - ' + finalUser.Id);
        }
        //If Employee Email match found, use that employee
        Else if (!employeeEmailSubmitter.isEmpty()){
            for (User user : employeeEmailSubmitter){
                if (user.Id != null){
                    System.debug('*******************************************'); 
            		System.debug('Made it to the employee email match -' + user.Id);
//THIS DOESN"T WORK
            		finalUser.Id = user.Id;
                    update finalUser;
                    System.debug('FinalUser ID - ' + finalUser.Id);
                }
            }
        }
   }       
    
   //Determine User Role of case submitter 
   System.debug('*******************************************'); 
   System.debug('Final User Role ID Name ' + finalUser.UserRoleId);
   List <UserRole> userRoles = ([Select Id, Name from UserRole where Id =: finalUser.UserRoleId limit 1]);
    if (!userRoles.isEmpty()){
    	for (UserRole role: userRoles){
       		userRoleID.Id = role.Id;
        }
   } 		
   
    
    //map role to case team
    System.debug('*******************************************'); 
    System.debug('Role ID Name ' + UserRoleID.Id);
    If (UserRoleID.Name == 'Bethesda Branch Manager') {for (CaseTeamTemplate team : bethesdaTeam) finalTeam.add(team); }
    else if (UserRoleID.Name == 'Bethesda Staff') {for (CaseTeamTemplate team : bethesdaTeam) finalTeam.add(team); }
    else if (UserRoleID.Name == 'Business Development Officers') {for (CaseTeamTemplate team : busDevTeam) finalTeam.add(team); }
    else if (UserRoleID.Name == 'Business Development Staff'){for (CaseTeamTemplate team : busDevTeam) finalTeam.add(team); }
    else if (UserRoleID.Name == 'DC Branch Manager'){for (CaseTeamTemplate team : dcTeam) finalTeam.add(team); }
    else if (UserRoleID.Name == 'DC Staff'){for (CaseTeamTemplate team : dcTeam) finalTeam.add(team); }
    else if (UserRoleID.Name == 'Herndon Branch Manager'){for (CaseTeamTemplate team : herndonTeam) finalTeam.add(team); }
    else if (UserRoleID.Name == 'Herndon Staff'){for (CaseTeamTemplate team : herndonTeam) finalTeam.add(team); }
    else if (UserRoleID.Name == 'Lending Branch Deposit Staff'){for (CaseTeamTemplate team : hcDepTeam) finalTeam.add(team); }
    else if (UserRoleID.Name == 'Potomac Branch Manager'){for (CaseTeamTemplate team : potomacTeam) finalTeam.add(team); }
    else if (UserRoleID.Name == 'Potomac Staff'){for (CaseTeamTemplate team : potomacTeam) finalTeam.add(team); }
    else if (UserRoleID.Name == 'Rockville Branch Manager'){for (CaseTeamTemplate team : rockvilleTeam) finalTeam.add(team); }
    else if (UserRoleID.Name == 'Rockville Staff'){for (CaseTeamTemplate team : rockvilleTeam) finalTeam.add(team); }
    else if (UserRoleID.Name == 'Commercial Deposits Manager'){for (CaseTeamTemplate team : commDepTeam) finalTeam.add(team); }
    else if (UserRoleID.Name == 'Commercial Deposits Staff'){for (CaseTeamTemplate team : commDepTeam) finalTeam.add(team); }
    else if (UserRoleID.Name == 'IT Employees'){for (CaseTeamTemplate team : itTeam) finalTeam.add(team); }
    else if (UserRoleID.Name == 'Corporate Lenders'){for (CaseTeamTemplate team : corpLenders) finalTeam.add(team); }    
      
    
    //create list of new members to add
    for (Case c : cases) {
        if (c.Id != null && finalUser.Id!= null){
        	System.debug('*******************************************'); 
        	System.debug('Made it to the new member list add');
    		membersToAdd.put(c.Id, new CaseTeamMember(             
            	ParentId = c.Id,                           
            	MemberId = finalUser.Id ) );

        }
        
      
    }

    List <CaseTeamRole> caseTeamRl = [SELECT Id, Name FROM CaseTeamRole WHERE Name = 'Case Creator' LIMIT 1];    
    //add case creator to list
    //
    	        System.debug('*******************************************'); 
          		System.debug('Made it before IF - ' + membersToAdd.isEmpty() + ' ' + caseTeamRl.isEmpty() );
    if (!membersToAdd.isEmpty() && !caseTeamRl.isEmpty()) {
      try {
    	        System.debug('*******************************************'); 
          		System.debug('Made it in Role If');
        		for (CaseTeamMember ctm : membersToAdd.values()) {
                    for (CaseTeamRole role : caseTeamRl) {
          			System.debug('*******************************************'); 
          			System.debug('Made it to the Case Creator List addition');
          			ctm.TeamRoleId = role.Id;
                	}
                }
        
    
    	for (CaseTeamMember ctm : [SELECT Id, MemberId, ParentId
                       FROM CaseTeamMember
                       WHERE ParentId IN :membersToAdd.keySet()
                       AND MemberId =: finalUser.Id
                       ORDER BY ParentId]) {
                       
       
        System.debug('*******************************************'); 
        System.debug('Made it to the individual user delete');
          if (membersToAdd.containsKey(ctm.ParentId)) {
            membersToAdd.remove(ctm.ParentId);
          }
        }
         
    if (!membersToAdd.isEmpty()) {
          System.debug('*******************************************'); 
          System.debug('Made it to the individual user insert');
          insert membersToAdd.values();
        }
        
        } catch (System.QueryException qe) {}
    }
    
    //add case teams
    System.debug('*******************************************'); 
    System.debug('Made it to top of teams Add if - ' + teamsToAdd.isEmpty());
      if (!teamsToAdd.isEmpty()) {
      try {
        CaseTeamTemplateRecord caseTeamLink;          
        for(Case caseEval: cases){
            for (CaseTeamTemplate team : finalTeam){
				caseTeamLink= new CaseTeamTemplateRecord(ParentId =caseEval.Id, TeamTemplateId = team.Id );
            	}
            }
          if (caseTeamLink!=null) {
            System.debug('*******************************************'); 
            System.debug('Made it to the case team insert');
        	insert caseTeamLink;
          }
 
        } catch (System.QueryException qe) {}
    }
    
    
    
}


 
Tarun Khatri (TK)Tarun Khatri (TK)
Hello Jim,

You code is simply doing a blank update on user record.
finalUser.Id =c.CreatedById ;
update finalUser;

The above lines of code will just do a blank update on CreatedbyId user record. Same is the case with employeeEmailSubmitter record. You can check last modified date of user record to varify the blank update.

What you want to update on user record? If it a field, you need to pull that field before update. For e.g.:
finalUser.Id =c.CreatedById ;
finalUser.Name = 'Updated Name';
update finalUser;


-Thanks,
TK

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.
Jim Parker 7Jim Parker 7
I am basically just trying to use finalUser as the pointer to the User that I want to use later.  So basically, finalUser becomes either the CreatedBy User or the User that has the email address on file.  Then I user finalUser to lookup the role of that User.  So I don't need to update the field, I just want the finalUser to be equal to the correct User. 
Tarun Khatri (TK)Tarun Khatri (TK)
You are using finalUser.Id at multiple places to assign MemberId. Instead you can use String variable to store User Id (CreatedBy or employeeEmailSubmitter). 

Also, if you need more information on that user, like UserRoleId  (Line #65), you need to fire a query on User Object to get details of finalUser.
 
SELECT Id, UserRoleId FROM User WHERE id: <string variable with UserId>