+ Start a Discussion
sswaminasswamina 

No Records to Display in Related Lists

Hi all,

 

I have a look-up field on my lead object to custom object events i.e. my lead can be registered for a single event. On event object I have look-up to lead so that can have related list for event on my lead detail page.

 

However although I assign the event object by using a look-up icon on the lead the related event does not show up in the related list. How can I make it available, which table I need to update using APEX

 

Regards

Shuba

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

First of all I do not underStand why do you want a related list for event when as per ur data model it seems you will only have a single event with a lead as u r  having a lookup of event in lead. So when ever you will add any event in ur lead in detail view you will find a link to ur event. So no need for related list.
But if related list is ur reequirement then here is ur trigger :

 

trigger on MyTrigger Lead(before insert , before update)
	{
               Set<ID> setEventID = new Set<ID>();
                Map<ID ,ID> map_EveentID_LeadID = new MAP<ID ,ID>();
               Integer Index = 0;
               for(Lead l : trigger.new)
                {
                       if(l.eventID__c != null)
				{
                                     if(trigger.isUpdate && trigger.old[index].eventID__c != l.eventID__c)
					{ 


                                         setEventID.add(l.eventID__c); 
                                        }
                                        else
                                       setEventID.add(l.eventID__c);
                                        map_EveentID_LeadID.put( l.eventID__c , l.id );
                               }
                 }
        
            List<CustomEvent__c> listEvents = [Select id From CustomEvent__c where id in : setEventID];
            
            for(CustomEvent__c e : listEvents)
		{
                        if(map_EveentID_LeadID.containsKey(e.id))
                           e.LeadID__c = map_EveentID_LeadID.get(e.id);
                }
            update listEvents;
        }

 Don't forgot to add the related list in layout if it is not there.

All Answers

Ankit AroraAnkit Arora

Req is bit strange, per my understanding you have a lead lookup in event and event lookup in lead. So when you create a lookup of event on lead then related list of lead will automatically be created on event. When you select any event on lead, that lead will be shown on event related list.

 

You also have a lookup of lead on event, that means related list of event will be shown on lead but you won't be able to see any record as you are not selecting any lead on event.

 

If you want to have lead selected on event automatically when you save a lead record with event lookup, then create a trigger which will update the lead lookup on event when lead is saved with event lookup.

 

Thanks

Ankit Arora

Blog | Facebook | Blog Page

sswaminasswamina

Thanks Ankit.

 

I need to lookup field for event on lead because I need to select a event for which to create the lead. The only reason the to have lookup of lead on event is to be able to show the related list of the concerned event and event fields on lead.

 

I tried to update the event by adding the lead to lead array on event but it does not show the record.  how else can I update the lead lookup on event?

Ankit AroraAnkit Arora

As I have suggested before also, trigger will be a good option for you. Create a trigger on after insert of lead, when you have value in event lookup then your trigger will update the value of lead Id in event record. Which will in end show the event related list on lead.

 

Thanks

Ankit Arora

Blog | Facebook | Blog Page

Shashikant SharmaShashikant Sharma

First of all I do not underStand why do you want a related list for event when as per ur data model it seems you will only have a single event with a lead as u r  having a lookup of event in lead. So when ever you will add any event in ur lead in detail view you will find a link to ur event. So no need for related list.
But if related list is ur reequirement then here is ur trigger :

 

trigger on MyTrigger Lead(before insert , before update)
	{
               Set<ID> setEventID = new Set<ID>();
                Map<ID ,ID> map_EveentID_LeadID = new MAP<ID ,ID>();
               Integer Index = 0;
               for(Lead l : trigger.new)
                {
                       if(l.eventID__c != null)
				{
                                     if(trigger.isUpdate && trigger.old[index].eventID__c != l.eventID__c)
					{ 


                                         setEventID.add(l.eventID__c); 
                                        }
                                        else
                                       setEventID.add(l.eventID__c);
                                        map_EveentID_LeadID.put( l.eventID__c , l.id );
                               }
                 }
        
            List<CustomEvent__c> listEvents = [Select id From CustomEvent__c where id in : setEventID];
            
            for(CustomEvent__c e : listEvents)
		{
                        if(map_EveentID_LeadID.containsKey(e.id))
                           e.LeadID__c = map_EveentID_LeadID.get(e.id);
                }
            update listEvents;
        }

 Don't forgot to add the related list in layout if it is not there.

This was selected as the best answer
sswaminasswamina

Thanks Shashikant.