+ Start a Discussion
Michael MMichael M 

Owner assignment help

Hello, I need some help assigning the owner of Contact records because of the way in which I am creating these records in the first place. We have an integration built which automatically creates Leads. Now, we have a Trigger (on Lead) that picks up information in some custom Lead fields, and based on those creates new Contact records. 

I want the owner of the Contact record to ALWAYS be the same as the owner of the Lead record. The reason this is complicated is because immediately when Leads are generated by our integration, they are at first (in the first milisecond) owned by the person's account who built the integration. Then, we have a Lead Process Builder that changes the Lead owner based on certain criteria. However, since  in our scenario, the Contact records are being generated from a Lead trigger (which comes before Process Builder in the order of execution), the owner of the Contact keeps being assigned to the ORIGINAL lead owner (the account of the integration builders.) I tried using Contact Process Builder to assign the owner of the contact based on the parent Lead's owner, but that doesn't seem to be working.  What would you advise? 
Best Answer chosen by Michael M
SarvaniSarvani
Hi Michael,

I would suggest implementing a before Update trigger on lead record as you want the contact records owner to be updated each time lead owner is been updated.

As there is no direct relationship to query on contact records related to lead,  you have been creating using the existing trigger on lead record. You may have to query on the contact records using the custom fields that were copied from lead trigger in first place.

Lets say you have created contact record using lead's custom fields like field1__c and field2__c. mapping to contact fields Firstname, and LastName. Then query on contacts like : [select id, Firstname, lastName from contact where Firstname=:Field1__c and Lastname=:Field2__c];
which fetches contact records created during your lead trigger process.

For example in the below code, Assuming Intial contact records are been created using lead's email and phone field. Likewise you can query on contact using you custom fields.
 
trigger UpdateContactOwner on lead (before update) {

    list<lead> collectleads=new list<lead>();
    list<string> LeadEmails=new list<string>();
    list<string> LeadPhones=new list<string>();
    list<contact> UpdateContacts=new list<Contact>();
 
    for (lead l : Trigger.new) {
    // Access the old veersion of record by its ID in Trigger.oldMap
    lead oldleadrecord = Trigger.oldMap.get(l.Id);

   // fetch old version record owner
    String oldleadOwner = oldleadrecord.ownerId;
    String newleadOwner = l.ownerId;
    
    // Check that if the lead owner is been updated 
    if (oldleadOwner!=newleadOwner) {
        collectleads.add(l);
        LeadEmails.add(l.email);
        LeadPhones.add(l.Phone);
    }
  }

//Collecting all contact having matcing email and phone with leads.
list<contact> contactlist=[select id,lastname,email,phone from contact where email in :LeadEmails and Phone in :LeadPhones];

    // If leads phone and email matches with contacts phone and email Updating contact owner to lead new owner.
for(lead l:collectleads){
for(contact c:contactlist){
if(l.Email==c.email && l.phone==c.phone){
c.ownerId=l.ownerId;
updatecontacts.add(c);
}
}
}
if(updatecontacts.size()>0){
Update updatecontacts;
}
}
Hope this helps! 

Thanks

All Answers

SUCHARITA MONDALSUCHARITA MONDAL

Hi Michael,

1. Could it be possible to keep Account Owner same as Lead owner and when Contact will be created, it'll be same.
https://success.salesforce.com/answers?id=90630000000ZbWhAAK

2. Please go through this link, that might help in terms of person account
https://help.salesforce.com/articleView?id=000328427&language=en_US&type=1&mode=1

3. If none of the above works, try to implement your logic in Trigger itself. Trigger will always win (if things going on between Workflows, process builder and trigger.) Putting link for the same (.i.e why trigger will win)

https://www.youtube.com/watch?v=6CQk2BOVGcI

Hope this will give some direction to your solution approach.

Share your thoughts,

Thanks,
Sucharita

 

 

SarvaniSarvani
Hi Michael,

I would suggest implementing a before Update trigger on lead record as you want the contact records owner to be updated each time lead owner is been updated.

As there is no direct relationship to query on contact records related to lead,  you have been creating using the existing trigger on lead record. You may have to query on the contact records using the custom fields that were copied from lead trigger in first place.

Lets say you have created contact record using lead's custom fields like field1__c and field2__c. mapping to contact fields Firstname, and LastName. Then query on contacts like : [select id, Firstname, lastName from contact where Firstname=:Field1__c and Lastname=:Field2__c];
which fetches contact records created during your lead trigger process.

For example in the below code, Assuming Intial contact records are been created using lead's email and phone field. Likewise you can query on contact using you custom fields.
 
trigger UpdateContactOwner on lead (before update) {

    list<lead> collectleads=new list<lead>();
    list<string> LeadEmails=new list<string>();
    list<string> LeadPhones=new list<string>();
    list<contact> UpdateContacts=new list<Contact>();
 
    for (lead l : Trigger.new) {
    // Access the old veersion of record by its ID in Trigger.oldMap
    lead oldleadrecord = Trigger.oldMap.get(l.Id);

   // fetch old version record owner
    String oldleadOwner = oldleadrecord.ownerId;
    String newleadOwner = l.ownerId;
    
    // Check that if the lead owner is been updated 
    if (oldleadOwner!=newleadOwner) {
        collectleads.add(l);
        LeadEmails.add(l.email);
        LeadPhones.add(l.Phone);
    }
  }

//Collecting all contact having matcing email and phone with leads.
list<contact> contactlist=[select id,lastname,email,phone from contact where email in :LeadEmails and Phone in :LeadPhones];

    // If leads phone and email matches with contacts phone and email Updating contact owner to lead new owner.
for(lead l:collectleads){
for(contact c:contactlist){
if(l.Email==c.email && l.phone==c.phone){
c.ownerId=l.ownerId;
updatecontacts.add(c);
}
}
}
if(updatecontacts.size()>0){
Update updatecontacts;
}
}
Hope this helps! 

Thanks
This was selected as the best answer
Michael MMichael M
Thank you for the idea!