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
Dhanesh SubramanianDhanesh Subramanian 

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);
    }


 
ShotShot
Try this one:
trigger CallOwnerAsParticipant on Call__c (after Insert)
{
    List<Participant__c> addparticipant = new List<Participant__c>();
    for(Call__c call: trigger.new)
    {
        if(call.OwnerId != null)
        {
            user us = [Select Name from user WHERE id:=call.OwnerId LIMIT 1];
            Contact con = [Select Id, Name from Contact where Name =: us.Name LIMIT 1];
            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);
}