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
María Jesús CarmonaMaría Jesús Carmona 

How can I update a field on Case based on the profileId of the user who created it?

Hi, I wanted to update a field on Case object when that Case's createdByID field refers a user with a profileID "x" or "y" with a static text.
I think that this can be achieved by a trigger that is fired after the Case has been inserted or updated, storing the createdById value on a variable and using it on a query, I tried that but it gives me this error "Illegal assignment from list to set ".
I let my code below:
trigger myTrigger on Case (after insert, after update) {

    Set<Id> IdUser =new Set<Id>();
    Set<Id> IdProfile =new Set<Id>();

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

    for(Case obj: Trigger.New){
                IdUser.add(obj.createdById);
                IdProfile=[SELECT Id, ProfileID FROM User WHERE User.Id=:IdUser];

        IF(IdProfile=='x' || IdProfile=='y'){
                //obj.field='created by profile x and y';
        }

        caseUpdateList.add(obj);
    }

    IF(caseUpdateList.size() > 0){
        update caseUpdateList;
    }
}
It will surelly have some code bad written, Im still pretty new to Salesforce and Apex triggers.

Maria J.
 
Best Answer chosen by María Jesús Carmona
Raj VakatiRaj Vakati
Why do you need for the trigger ? you can do it as a formual field .. 
Return type of formual is text
IF( CreatedBy.Profile.Name =='A' ,'A','B')

 

All Answers

v varaprasadv varaprasad
Hi Maria,

Please check once below the sample code.
 
trigger myTrigger on Case (after insert, after update) {

    Set<Id> userIds =new Set<Id>();
   
	//After insert
	if(trigger.isAfter && trigger.isUpdate){
    for(Case obj: Trigger.New){	
                userIds.add(obj.createdById);
    }
    
	list<case> lstCases= [select id,prfoileName_c from case where id in : trigger.newmap.keyset()];
	
	Map<id,user> mapUserdetails = [select id,name,profile.name from user where id in: userIds];
	
	for(case cs : lstCases){
	   if(mapUserdetails.containskey(cs.createdById)){
	      cs.prfoileName_c = mapUserdetails.get(cs.createdById).profile.name;
	   }	
	}
    
	  update lstCases;
	}
	
	//After insert
    if(trigger.isAfter && trigger.isUpdate){
    for(Case obj: Trigger.New){
	if((obj.createdById != trigger.oldmap.get(obj.id).createdById) )
                userIds.add(obj.createdById);
    }
    
	list<case> lstCases= [select id,prfoileName_c from case where id in : trigger.newmap.keyset()];
	
	Map<id,user> mapUserdetails = [select id,name,profile.name from user where id in: userIds];
	
	for(case cs : lstCases){
	   if(mapUserdetails.containskey(cs.createdById)){
	      cs.prfoileName_c = mapUserdetails.get(cs.createdById).profile.name;
	   }	
	}
    
	  update lstCases;
	}
}

note: I have not tested the above code may you will get some syntactical errors.

 
Hope this helps you!
If my answer helps resolve your query, please mark it as the 'Best Answer' & upvote it to benefit others.

Thanks
Varaprasad
@For Support: varaprasad4sfdc@gmail.com
Blog: http://salesforceprasad.blogspot.com/

Salesforce latest interview questions  :
https://www.youtube.com/channel/UCOcam_Hb4KjeBdYJlJWV_ZA?sub_confirmation=1

 
v varaprasadv varaprasad
trigger myTrigger on Case (after insert, after update) {

    Set<Id> userIds =new Set<Id>();
   
	//After insert
	if(trigger.isAfter && trigger.isInsert){
    for(Case obj: Trigger.New){	
                userIds.add(obj.createdById);
    }
    
	list<case> lstCases= [select id,prfoileName_c from case where id in : trigger.newmap.keyset()];
	
	Map<id,user> mapUserdetails = [select id,name,profile.name from user where id in: userIds];
	
	for(case cs : lstCases){
	   if(mapUserdetails.containskey(cs.createdById)){
	      cs.prfoileName_c = mapUserdetails.get(cs.createdById).profile.name;
	   }	
	}
    
	  update lstCases;
	}
	
	//After update
    if(trigger.isAfter && trigger.isUpdate){
    for(Case obj: Trigger.New){
	if((obj.createdById != trigger.oldmap.get(obj.id).createdById) )
                userIds.add(obj.createdById);
    }
    
	list<case> lstCases= [select id,prfoileName_c from case where id in : trigger.newmap.keyset()];
	
	Map<id,user> mapUserdetails = [select id,name,profile.name from user where id in: userIds];
	
	for(case cs : lstCases){
	   if(mapUserdetails.containskey(cs.createdById)){
	      cs.prfoileName_c = mapUserdetails.get(cs.createdById).profile.name;
	   }	
	}
    
	  update lstCases;
	}
}

 
Raj VakatiRaj Vakati
Why do you need for the trigger ? you can do it as a formual field .. 
Return type of formual is text
IF( CreatedBy.Profile.Name =='A' ,'A','B')

 
This was selected as the best answer