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
Irvine DelacroixIrvine Delacroix 

User ID convert to Username in Trigger

Hello,

Can you guys help on my simple trigger experiment please. What I am trying to do is to find the name of the UserID.
I have object (TestObject__c) and Fields (txtField__c, WhoUser__c).

My goal is to return the name of the UserID that is in the txtField under my TestObject. Say for example, my user is Bob and his user ID is 0053000000AtjAhAAJ.

So, if the txtField value is 0053000000AtjAhAAJ, the WhoUser field will return Bob when I save the Record.

I've tried experimenting on this but it seems my codes does not make any sense.. I'm always getting that "Unexpected Token" error when trying to query.. (I'm still learning the syntax though) :D

Thanks a lot!
Best Answer chosen by Irvine Delacroix
Arthur LockremArthur Lockrem
trigger TextObject_BeforeTrigger on TestObject__c (before insert, before update) {

	Set<Id> userIds = new Set<Id>();
	
	for (TestObject__c testObject : trigger.new) {
		userIds.add(testObject.txtField__c);	
	}
	
	List<User> users =
		[SELECT Id, Name
		 FROM User
		 WHERE Id IN : userIds];
	Map<Id, User> mUsers = new Map<Id, User>(users);
	
	for (TestObject__c testObject : trigger.new) {
		User user = mUsers.get(testObject.txtField__c);
		if (user != null) testObject.WhoUser__c = user.Name;
	}
}
I didn't test this, but the concepts should be there.
- Get a set of user ids
- Get a map of user ids to user records
- Lookup users for each test object
- If a user match is found, update the who user field

Good luck
 

All Answers

Arthur LockremArthur Lockrem
trigger TextObject_BeforeTrigger on TestObject__c (before insert, before update) {

	Set<Id> userIds = new Set<Id>();
	
	for (TestObject__c testObject : trigger.new) {
		userIds.add(testObject.txtField__c);	
	}
	
	List<User> users =
		[SELECT Id, Name
		 FROM User
		 WHERE Id IN : userIds];
	Map<Id, User> mUsers = new Map<Id, User>(users);
	
	for (TestObject__c testObject : trigger.new) {
		User user = mUsers.get(testObject.txtField__c);
		if (user != null) testObject.WhoUser__c = user.Name;
	}
}
I didn't test this, but the concepts should be there.
- Get a set of user ids
- Get a map of user ids to user records
- Lookup users for each test object
- If a user match is found, update the who user field

Good luck
 
This was selected as the best answer
Irvine DelacroixIrvine Delacroix
Awesome! Thank you so much @Arthur! I can start a new Idea from here. :)
Arthur LockremArthur Lockrem
You're welcome.  If this resolved your issue please select the answer and mark the question as solved.
Joana MatosJoana Matos
Hi, I have a process to change the record type of a contact, whenever a tick box is ticked, and to then change the Name field to the Contact ID. 
​​​​​​I then need to reinstate the original Name of the Contact, when the box is untucked, and it will revert to the original Record Type. The process is not working in the last part, when reinstating the Name. 
​​​​​​Does anyone have any suggestion? 
Thanks
Greg Adams 58Greg Adams 58

Advertising has always been the essential instrument for raising the profile of your goods, and this aspect of business has not altered for a very long time. In order to adapt to a new market with the Internet and become what is now known as the Advertising Technology, advertising had to change with the arrival of the digital era in the 2000s (AdTech). The appropriate person at the right time may be reached with your products using a certain set of strategies and procedures (more info here https://mobilunity.com/blog/hire-adtech-developers/). Advertising had to adapt quickly when a brand-new digital market emerged in order to capture consumers' attention both offline and online.