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

Trigger error
I have a Call master object and Participant as Detail object. Whenever a call gets created the call owner(user lookup) should be added as a Participant record. Participant name is (Contact Lookup). I used the below trigger, its hitting the governor limits. Since i have used "Select statement" in For loop. Is there any workaround. I do have to map the User name with the Contact name based on the owner id.
trigger CallOwnerAsParticipant on Call__c (after Insert)
{
List<Participant__c> addparticipant = new List<Participant__c>();
Map<Id,user> usermap =new Map<Id,user>([Select Id,Name from user]);
for(Call__c call: trigger.new)
{
if(call.OwnerId != null)
{
user us = usermap.get(call.Ownerid);
Contact con = [Select Id, Name from Contact where Name =: us.Name];
Participant__c part = new Participant__c();
part.CallId__c = call.Id;
part.Contact_Id__c = con.Id;
addparticipant.add(part);
}
}
if(addparticipant .size()>0)
{
database.insert(addparticipant);
}
trigger CallOwnerAsParticipant on Call__c (after Insert)
{
List<Participant__c> addparticipant = new List<Participant__c>();
Map<Id,user> usermap =new Map<Id,user>([Select Id,Name from user]);
for(Call__c call: trigger.new)
{
if(call.OwnerId != null)
{
user us = usermap.get(call.Ownerid);
Contact con = [Select Id, Name from Contact where Name =: us.Name];
Participant__c part = new Participant__c();
part.CallId__c = call.Id;
part.Contact_Id__c = con.Id;
addparticipant.add(part);
}
}
if(addparticipant .size()>0)
{
database.insert(addparticipant);
}
Try this one: