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
BA_AdminBA_Admin 

How to display Contact Name In Trigger

Hello All,

         I have field in contact object called Manager which is lookup to contact, i have trigger on contact object so basically this trigger creates case when a new contact is created but when iam selecting manager from contact this manager name is not displaying in the case instead it's displaying Id, any help?

Best Answer chosen by Admin (Salesforce Developers) 
Shashikant SharmaShashikant Sharma

Finally I could reproduce it and your solution is

use String.valueOf(ct.Start_Date__c)  in your trigger while assigning value to any text field , String.value will conver date into the string that do not show

00 : 00 : 00 

All Answers

Shashikant SharmaShashikant Sharma

Use another SOQL in trigger to fetch information from manager.

 

 you will not get related values in trigger.new , you will have to query on contact again for the manager id and fetch the name

Use this in your trigger

set<id> setMangIds = new Set<>();
Map<ID , ID> map_conIDKey_MangIDValue = new Map<ID ,ID>();
for( Contact c : trigger.new)
   {
    setMangIds.add(c.ManagerID__c);
   map_conIDKey_MangIDValue.put(c.id , c.ManagerID__c);
   }

Map<id , Contact> map_mang = new Map<ID , Contact>([Select Name from Contact where id =: setMangIds]);

for( Contact c : trigger.new)
   {
   
      String mangName;
       ID mangerID = map_conIDKey_MangIDValue.get(c.id);
   
       if(mangerID != null)
            mangName = map_mang.get(mangerID);
   
        // You can use this mang name further in case
   }

 

code

 

 

 

BA_AdminBA_Admin

In the query i have inserted this field but instead of displaying manager name in case it's displaying Id, here is my code

 

public class ContactTriggerClass
{
 
    public void createCase(Map<Id,Contact> TriggerNewValues)
   {
   
    List<Case> c = new List<Case>();
    
 for(Contact ct :[select c.Id,c.Name,c.AccountId,c.Notification_Type__c,c.Manager_Name__r.Manager_Name__c,c.EmployeeType__c,c.Location__c,c.Start_Date__c,c.CostCenter__c,c.HRDepartment__c,c.Employee_List__c,c.JobTitle__c,c.Unix_Account__c, c.Termination_Date__c, c.Termination_Time__c from  Contact c where c.Id IN:TriggerNewValues.keyset()])
    {
       if((ct.Notification_Type__c == 'New Employee') || (ct.Notification_Type__c =='New Contractor'))
       {
                                   
                                       
      c.add(new Case(ContactId = ct.Id,AccountId = ct.AccountId,Type = 'PC SETUP_(New/Reused;Desktops,Laptops)',Status='Assigned',Priority = 'Standard',OwnerId = '00G400000015Yoo',Origin ='Email',Subject = 'New PC Setup :'+ct.Name+'- Start Date :'+ct.Start_Date__c,Description = 'Name :'+ ct.Name+'\r\n\r\nManager :'+ct.Manager_Name__r.Manager_Name__c+'\r\n\r\nEmployee Type :'+ct.EmployeeType__c+'\r\n\r\nLocation:'+ct.Location__c+'\r\n\r\nStart Date :'+ct.Start_Date__c));
                  
                                     
      }
        
     
   }
    insert c;
   }
 
}

 

 

Shashikant SharmaShashikant Sharma

Try with this

change 

c.Manager_Name__r.Manager_Name__c

to

c.Manager_Name__r.Name

 

at both the places i think you are getting manager id of manager in your code.

BA_AdminBA_Admin

ohh that is awesome, it displayed me the name , a small question in the same contact object i have date field called start_date__c, so when the case gets created the date is showing perfect but along with the date it is showing the time format like this 2011-06-24 00:00:00 , i want to avoid this , iam not sure why it is displaying time even though the field is just date type

Shashikant SharmaShashikant Sharma

Your field is datetime field , use date field if you don't want time.

BA_AdminBA_Admin

It is just date field

Shashikant SharmaShashikant Sharma

and what is the return type of formula field , please check it is not datetime either.

BA_AdminBA_Admin

Return type of formula field? i have just created date field Start_Date__c and iam using this in class, i thought it should just display date but it's not

Shashikant SharmaShashikant Sharma

I also tried a formula field of type Date and saw it did not show me 00:00:00 , you can tru one more thing in your foumula select treat blank as blank. 

 

Please let me know how are you filling your formula field , what is the formula.

BA_AdminBA_Admin

Actually iam not creating formula field iam just creating a Date field and iam using this date field in Apex class

Shashikant SharmaShashikant Sharma

Is Start_Date__c is your Date field and you are copying it in the TextField

BA_AdminBA_Admin

Yes Start_Date__c is Date field in Contact Object and if a new contact is created then the case gets created and this Date field from contact would be displaying in the description (Text) field in Case

Shashikant SharmaShashikant Sharma

Finally I could reproduce it and your solution is

use String.valueOf(ct.Start_Date__c)  in your trigger while assigning value to any text field , String.value will conver date into the string that do not show

00 : 00 : 00 

This was selected as the best answer
BA_AdminBA_Admin

You are awesome thank you somuch for your help

Shashikant SharmaShashikant Sharma

Your welcome mate :)

BA_AdminBA_Admin

Thx for being so kind to me and helping me in my lil project, while it is coming to end i have small question where i have no idea to do it, i could do it by WFR but i need to setup atleast like 10 rules, but is it possible to add the manager name to the caseteam when a case is created, ok i knw it's lil confusion but i will explain you, usually when a contact is created , i have a Manager_Name__c field in contact so they will select this manager respective to that contact so when they save the contact a case gets created so is it possible to add this Manager to the case team for respective case, if possible i can post my code but it is similar to the code which i have posted before.

 

 

TIA!

Shashikant SharmaShashikant Sharma

Yes, It is possible, please post your code, I will try to help you if I Could.

BA_AdminBA_Admin

Gr8 thanks so much , here is my code

 

Trigger:  

trigger ContactTrigger on Contact (after insert, after update) {
  
  ContactTriggerClass ct  = new ContactTriggerClass();
   ct.createCase(Trigger.newMap);  
    
}

 

 

Class: public class ContactTriggerClass
{
 
    public void createCase(Map<Id,Contact> TriggerNewValues)
   {
   
    List<Case> c = new List<Case>();
    
    for(Contact ct :[select c.Id,c.Name,c.AccountId,c.Notification_Type__c,c.Manager_Name__r.Name,c.EmployeeType__c,c.Location__c,c.Start_Date__c,c.CostCenter__c,c.HRDepartment__c,c.Employee_List__c,c.JobTitle__c,c.Special_Instructions_Hire__c,c.Special_Instructions_Term__c,c.Unix_Account__c,c.Termination_Date__c, c.Termination_Time__c from  Contact c where c.Id IN:TriggerNewValues.keyset()])
    {
       if((ct.Notification_Type__c == 'New Employee') || (ct.Notification_Type__c =='New Contractor'))
       {
                                       /* ------------For New PC SETUP------------- */
                                       
      c.add(new Case(ContactId = ct.Id,AccountId = ct.AccountId,Type = 'PC SETUP_(New/Reused;Desktops,Laptops)',Status='Assigned',OwnerId = '00G400000015Yoo',Origin ='Email',

Subject = 'New PC Setup :'+ct.Name+'- Start Date :'+string.valueof(ct.Start_Date__c),

Description = 'Name :'+ ct.Name+'\r\n\r\nManager :'+ct.Manager_Name__r.Name+'\r\n\r\nEmployee Type :'+ct.EmployeeType__c));


}
        
     
   }
    insert c;
   }
 
}

 

So if this manager is added to case team then all the updates like case comments , case close, case creation can be sent to manager for his contact   through WFR, but main point is adding manager to the case, i have case team member role called  Managers   and manager has to be added to this role.

 

TIA!     

 

 

Shashikant SharmaShashikant Sharma

First fetch this c.Manager_Name__r.Id from contact then 

 

new Case( Case_MangerField__C = c.Manager_Name__r.Id , 

 

 

I hope will work for you

BA_AdminBA_Admin

I guess this one adds the manager name to case, but it doesn't adds to the case team ryte