You need to sign in to do that
Don't have an account?

Lookup autofill with a selected picklist value?
Hi all,
I have a picklist that has names (text) that are the same as my contact names (Appointment_with__c)
I have a lookup field to contact that I want to autofill whenever a name is selected from my picklist. The lookup field is called (Staff__c)
I made a basic trigger but encountering silly errors and perhaps not the best code as I am a beginner:
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;
}
}
I keep getting 'Invalid type: Staff__c'
Variable does not exist: id'
I have a picklist that has names (text) that are the same as my contact names (Appointment_with__c)
I have a lookup field to contact that I want to autofill whenever a name is selected from my picklist. The lookup field is called (Staff__c)
I made a basic trigger but encountering silly errors and perhaps not the best code as I am a beginner:
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;
}
}
I keep getting 'Invalid type: Staff__c'
Variable does not exist: id'
All Answers
Staff__c is my lookup field
This is in my Event object.
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);
}
}
//Query records and loop on them to prepare a map where key is the name
Map<String,Staff__c> dclocationMap = new Map<String,Staff__c>();
for(Staff__c rec: [SELECT Id, Name FROM Staff__c WHERE Name in: ids]) {
dclocationMap.put(rec.Name, rec);
}
for(Event evt1: Trigger.New)
{
evt1.Staff__c = dclocationMap.get(evt1.Appointment_with__c).Id;
}
}
Unfortunately getting more errors :(