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
Faizan Ali 17Faizan Ali 17 

Invalid type?

Hi all, I am trying to create a trigger that will autopopulate my lookup field when a value is selected from the picklist however I am getting these errors and I don't know why.

My lookup field is to contact (which is filtered).

User-added image
Abdul KhatriAbdul Khatri
Hi Faizan,

What is the difference between Appointment_with__c and Staff__c, looks like both are referncing the same data point, one is picklist and other is lookup?

Is there a possibility that a person may not exist in Staff__c SObject, selected in Appointment_with__c?

Keeping the above perspective, don't see it a good design and also not is also not taking care of null pointers which may endup getting exceptions.

Please also share the code instead of screenshot.
Faizan Ali 17Faizan Ali 17
Appointment_with__c is my picklist and Staff__c is my lookup field.

Whenever an Appointmet_with__c is selected, there is nothing in the lookup. I want to autofill with the value in the picklist.

My code is as follows:

trigger Autofill on Event (before insert, before update) {
    
    Set<String> ids = new Set<String>();
    
    for(Event evt: Trigger.New)
    {
        if(evt.Appointment_with__c != null)
        {
            ids.add(evt.Appointment_with__c);
        }
    }
    
    List<Staff__c> dclocation = [SELECT Id, Name FROM Staff__c WHERE Name in: ids];
    
    for(Event evt1: Trigger.New)
    {
        evt1.Staff__c = dclocation[0].id;
    }
}

 
Abdul KhatriAbdul Khatri
Hi Faizan,

It is complaining about Staff__c SObject. Is this exist in your org? Can you please verify?

Here is the right way to handle your requirement
 
trigger Autofill on Event (before insert, before update) {
    
    Set<String> ids = new Set<String>();
    
    for(Event evt: Trigger.New)
    {
        if(evt.Appointment_with__c != null)
        {
            ids.add(evt.Appointment_with__c);
        }
    }
    
    if(ids.isEmpty()) return;
    
	Map<String, Staff__c> staffMap = new Map<String, Staff__c>();
    for(Staff__c dclocation : [SELECT Id, Name FROM Staff__c WHERE Name in: ids]){
        staffMap.put(dclocation.Name, dclocation);
    }
    
    if(staffMap.isEmpty()) return;
    
    for(Event evt1: Trigger.New)
    {
        if(staffMap.get(evt1.Appointment_with__c) != null)
        	evt1.Staff__c = staffMap.get(evt1.Appointment_with__c).id;
    }
}

 
Abdul KhatriAbdul Khatri
Actually this one is better code than above
trigger Autofill on Event (before insert, before update) {
    
    Map<String, Event> eventMap = new Map<String, Event>();
    
    for(Event evt: Trigger.New)
    {
        if(evt.Appointment_with__c != null)
        {
            eventMap.add(evt.Appointment_with__c, evt);
        }
    }
    
    if(eventMap.isEmpty()) return;
    
	Map<String, Staff__c> staffMap = new Map<String, Staff__c>();
    for(Staff__c dclocation : [SELECT Id, Name FROM Staff__c WHERE Name in: eventMap.keyset()]){
        staffMap.put(dclocation.Name, dclocation);
    }
    
    if(staffMap.isEmpty()) return;
    
    for(Event evt1: eventMap.values())
    {
        if(staffMap.get(evt1.Appointment_with__c) != null)
        	evt1.Staff__c = staffMap.get(evt1.Appointment_with__c).id;
    }
}

 
MoggyMoggy

first i would try and make the lookup filter optional, it looks like the name which is picked and the related contact are not valid according to the filter on the lookup.

Also i would move the logic into a handler class 

Abdul KhatriAbdul Khatri
Hi Faizan,

Any feedback or update?