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
ManojKumar MuthuManojKumar Muthu 

Formula to display data in a field by passing Input from another field.

Hi All,

I have a requirement where I have to display "Contact Name" or "User Name" based on the Id given a particular field.

For example: Here my Input field is "CreatedBy__c" and my output field is "Contact_Name__c",
Contact Id is 0030l00000IcLXE (Draco) and user Id is 0050l000000Muxz(Ajay).
If CreatedBy__c = 0030l00000IcLXE then the Contact_Name__c = Draco like wise if CreatedBy__c = 0050l000000Muxzthen the Contact_Name__c = Ajay.

Can someone help me on this
  
Best Answer chosen by ManojKumar Muthu
Steven NsubugaSteven Nsubuga
It is not possible to do this in a formula. I suggest a trigger instead.
Trigger getNames on object__c (before insert, before update) {    

    Set<Id> userAndcontactIds = new Set<Id>();
	Map<Id, String> idobjectName = new Map<String, String>();
    for (object__c obj : Trigger.new) {
		
		// add all the ids to the same set, the queries will handle them
		userAndcontactIds.add(obj.IdField__c);
    }
	Map<Id, Contact> contacts = new Map<Id, Contact> ([SELECT Name FROM Contact WHERE Id IN:userAndcontactIds]);
	Map<Id, User> users = new Map<Id, User> ([SELECT Name FROM User WHERE Id IN:userAndcontactIds]);
	
	for (object__c obj : Trigger.new) {
		String objectName =  obj.IdField__c.getSObjectType().getDescribe().getName();
		
		if (objectName == 'User') {
			obj.NameField__c = users.get(obj.IdField__c).Name;
		}
		if (objectName == 'Contact') {
			obj.NameField__c = contacts.get(obj.IdField__c).Name;
		}
	}
}

All Answers

Steven NsubugaSteven Nsubuga
It is not possible to do this in a formula. I suggest a trigger instead.
Trigger getNames on object__c (before insert, before update) {    

    Set<Id> userAndcontactIds = new Set<Id>();
	Map<Id, String> idobjectName = new Map<String, String>();
    for (object__c obj : Trigger.new) {
		
		// add all the ids to the same set, the queries will handle them
		userAndcontactIds.add(obj.IdField__c);
    }
	Map<Id, Contact> contacts = new Map<Id, Contact> ([SELECT Name FROM Contact WHERE Id IN:userAndcontactIds]);
	Map<Id, User> users = new Map<Id, User> ([SELECT Name FROM User WHERE Id IN:userAndcontactIds]);
	
	for (object__c obj : Trigger.new) {
		String objectName =  obj.IdField__c.getSObjectType().getDescribe().getName();
		
		if (objectName == 'User') {
			obj.NameField__c = users.get(obj.IdField__c).Name;
		}
		if (objectName == 'Contact') {
			obj.NameField__c = contacts.get(obj.IdField__c).Name;
		}
	}
}
This was selected as the best answer
ManojKumar MuthuManojKumar Muthu
Hi Steven Nsubuga,

Thanks 

Which field you are referring here as "IdField__c"??
 
Steven NsubugaSteven Nsubuga
The "IdField__c" is the field in which the User Id or the Contact Id is stored. 
The "NameField__c" is the one to be populated with the User name or Contact name.
ManojKumar MuthuManojKumar Muthu
Get the below Error: 
Error: Compile Error: Variable does not exist: CreatedByType__c at line 27 column 13

Line:  if (CaseComments == 'user')
ManojKumar MuthuManojKumar Muthu
and for this too,

Line: String objectName =  obj.CreatedBy__c.getSObjectType().getDescribe().getName();
Below the error I am getting,

Error: Compile Error: Method does not exist or incorrect signature: void getSObjectType() from the type String at line 14 column 47
Steven NsubugaSteven Nsubuga
Use the CreatedByid field.