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
Synthia B.Synthia B. 

Apex Trigger: Showing Rec Id's and not Rec Name

The trigger below works great when a new opportunity is created however, if I need to make a change to the record types I would have to update the trigger in the sanidbox and send it back to the live enviornment. 
 
trigger IMPBI on Plan_Year__c (after insert)
{
List <Implementation__c> impToInsert = new List <Implementation__c>();
Map<Integer, String> impMap = new Map<Integer, String>();
impMap.put(1, 'Account/Billing Structure');
impMap.put(2, 'Benefit Effective Date');
impMap.put(3, 'Communications');
impMap.put(4, 'Contracts/Required Documents');
impMap.put(5, 'Data Requirements');
impMap.put(6, 'Open Enrollment');
impMap.put(7, 'Planning/Implementation');
impMap.put(8, 'Post Enrollment');
impMap.put(9, 'Post Implementation');
impMap.put(10, 'Pre-Implementation');
impMap.put(11, 'Required Documents');
impMap.put(12, 'Technical/Reporting');
impMap.put(13, 'Vendor Service Agreement');


for (Plan_Year__c currPlanYear : Trigger.New)
{
   for(Integer i=1; i<=13; i++)
   {
	   Implementation__c rec = new implementation__c (Name = impMap.get(i), Plan_Year_1__c = currPlanYear.Id); 
	   ImpToInsert.add(rec);
   }
}

Database.saveResult[] sr = database.insert(impToInsert,false);  

 for(integer x=0;x<sr.size();x++)
 {
    if(sr[x].isSuccess() == false)
      system.debug(logginglevel.error,sr[x].getErrors()[0].getMessage());
 }    
}

 User-added image

 ********************************************************************************************************************
The trigger below works as well but it only shows the record Id's when a new opportunity is created and not the name of the recodes. I am not sure what I am missing. 
 
trigger planTrigger_AT on Plan_Year__c (after insert) {
 list<RecordType> RT=new list<RecordType>();
  RT=[select id from RecordType where SObjectType = 'Implementation__c' and isActive=true];
 list<Implementation__c> List_imp = new list<Implementation__c>();
 if(trigger.isInsert){
 if(trigger.isAfter){

   for(Plan_Year__c PY: Trigger.new){
        for(RecordType RC : RT){
            Implementation__c imp = new Implementation__c();
             imp.RecordTypeid = RC.id;
             imp.Plan_Year_1__c = PY.id;
             List_imp.add(imp);

        }

    }
  if(RT.size()>0){
     insert List_imp;
    }
 }
 }
}

User-added image


Thanks in advance. 
 
Krishna SambarajuKrishna Sambaraju
Is Plan_Year_1__c a text field? Then change it to a lookup to the object Plan_Year__c, which will display the name of Plan_Year__c instead of id.
VahidVahid
Hi, First thing is have you crated lookup field or not on Implementation Object for Plan Year 1? the you have to create lookup field instead of text as suggested above. and Second if you are talking about Implementation's Name field then please check that on the object is it text or autonumber field, if its text then we have to put text while inserting record. 
If don't do then it will display record id instead of name. It does happen when we insert the record using apex or api. 

Thanks
Abdul Vahid
Synthia B.Synthia B.
Hi Krishana, 

I created the following filed on the Opportunity object as well as on the Implementation object. I am still getting the Id's and not the names of the records. 

Plan_Year_1__c Lookup(Plan Year) 



 
Synthia B.Synthia B.
Hello Vahid, 

I am not following what you advised "check that on the object is it text or autonumber field, if its text then we have to put text while inserting record."Can you provide steps for this? 

Thanks!
 
Krishna SambarajuKrishna Sambaraju
Have you checked the values in the Name field for the Plan Year records? Is it displaying the Id?
Synthia B.Synthia B.
Hi Krishna, 

The records are only on the Implementation Object as they belong to Implementation. The Plan Year Object is the Opportunity Object. There are no records on the Plan Year Object
Synthia B.Synthia B.
Thank you Krishna and Vahid.I will just use the trigger I have below since the user would like the records in a perticular order. 
 
trigger IMPAI on Plan_Year__c (after insert)
{
List <Implementation__c> impToInsert = new List <Implementation__c>();
Map<Integer, String> impMap = new Map<Integer, String>();
impMap.put(1, 'Pre-Implementation');
impMap.put(2, 'Planning/Implementation');
impMap.put(3, 'Account Structure/Billing');
impMap.put(4, 'Contracts/Required Documents');
impMap.put(5, 'Communications- (TO BE CONFIRMED)');
impMap.put(6, 'Data Requirements');
impMap.put(7, 'Open Enrollment');
impMap.put(8, 'Post Enrollment');
impMap.put(9, 'Post Implementation');

for (Plan_Year__c currPlanYear : Trigger.New)
{
   for(Integer i=1; i<=9; i++)
   {
       Implementation__c rec = new implementation__c (Name = impMap.get(i), Plan_Year_1__c = currPlanYear.Id); 
       ImpToInsert.add(rec);
   }
}

Database.saveResult[] sr = database.insert(impToInsert,false);  

 for(integer x=0;x<sr.size();x++)
 {
    if(sr[x].isSuccess() == false)
      system.debug(logginglevel.error,sr[x].getErrors()[0].getMessage());
 }    
}

Thank you!
William TranWilliam Tran
Synthia,

For 
trigger planTrigger_AT on Plan_Year__c (after insert) ..............

{ Implementation__c imp = new Implementation__c();
imp.RecordTypeid = RC.id;
imp.Plan_Year_1__c = PY.id;

Need to add: imp.Name = ....(what ever name you like to see)

List_imp.add(imp); } }
if(RT.size()>0){ insert List_imp; } } } }


you are not getting the name because you are not adding the name

You need to add: imp.Name = .... (what ever name you like to see)

thx.