0

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

You'll have to split your logic into 2 loops.

First you loop all the calls in the trigger, and get the name of the users in those calls, and put them in a Set. Then you query those contacts, loop the calls again, and find the contact in the queried contacts.

Something like this, (hasnt been tested, just written here, could contain bugs, but you'll get the gist)

List<Participant__c> addparticipant = new List<Participant__c>();
Map<Id,user> usermap =new Map<Id,user>([Select Id,Name from user]);
Set<String> userNames = new Set<String>();
for(Call__c call: trigger.new)
{
    if(call.OwnerId != null)
    {
        User us = usermap.get(call.Ownerid);
        userNames.add(us.Name);
    }
}

if(userNames.size() > 0)
{
      List<Contact> userNameContacts = [Select Id, Name from Contact where Name IN : userNames];
}

for(Call__c call : trigger.new)
{
     User us = usermap.get(call.Ownerid);
     Contact foundContact;
     for(Contact contact : userNameContacts)
     {
          if(contact.Name == us.Name)
          {
                foundContact = contact;
                break;
          }
     }

     if(foundContact != null)
     {
          Participant__c  part = new Participant__c();
          part.CallId__c = call.Id;
          part.Contact_Id__c = contact.Id;
          addparticipant.add(part);
     }
     else
     {
          // do something, contact not found
     }
}

if(addparticipant .size()>0)
{
    database.insert(addparticipant);
}
| improve this answer | |
0

Get only the needed user records and its corresponding contact records.

trigger CallOwnerAsParticipant on Call__c (after Insert) { 

List<Participant__c> addparticipant = new List<Participant__c>();


//Collect the owner ids in a set
set ownerIds<id> = new set<id>();

for(Call__c callForVar: trigger.new){
    ownerIds.add(callForVar.Ownerid);
}

//Collect the user records corresponding tot the owner ids 
List<user> userList = new List<user>([Select Id,Name from user where id IN : ownerIds]);

//User ids and its corresponding user records
Map<Id,user> usermap =new Map<Id,user>();

//Collect the user name in a set to get corresponding contact records
set<String> userNameSet  = new set<String>();

for(user userForVar : userList){

    usermap.put(userForVar.id , userForVar);
    userNameSet.add(userForVar.name);

}

//User name and its corresponding contact records
Map userNameAndContactMap<string,Contact> = new Map<string,Contact>();

//Get the corresponding contact records and put that in userNameAndContactMap 
for( Contact contactForVar : [Select Id, Name from Contact where Name IN : userNameSet]){

    userNameAndContactMap.put( contactForVar.Name , contactForVar);

}

//Write your logic 
for(Call__c call: trigger.new)
{
    if(call.OwnerId != null)
    {
        user us = usermap.get(call.Ownerid);
        Contact con = userNameAndContactMap.get(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);
}

}

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.