0

I have few custom child objects (lookup relationship) of Contact. My requirement is if I change the Contact owner from User A to User B, I want the child records of that Contact to be transferred to User B. And this transfer is being done by the user with Standard User profile. Kindly suggest. Please help me with a sample code if in case this is doable with triggers.

Trigger-

Trigger ContactUpdate on Contact (after update)
 {
List<ABC__c> abcList = new List<ABC__c>();
List<id> conIds = new List<id>();

for(Contact con : trigger.new)
{
  if(Trigger.oldmap.get(con.id).Ownerid != Con.Ownerid)
  {
   conIds.add(con.id);
 }
}
for(ABC__c abc : [Select id,Contact__c, OwnerId from ABC__c where Contact__c in:conIds])
{

 abc.Contact__c = Trigger.newMap.get(abc.OwnerId).OwnerId;
 abcList.add(abc);

}

 if(abcList.size()>0)
{
 update abcList;
}
}

New Trigger-

trigger OwnerUpdate on Contact(after update) {
try {
    Set<Id> contactIds = new Set<Id>(); 
    Map<Id, String> oldOwnerIds = new Map<Id, String>(); 
    Map<Id, String> newOwnerIds = new Map<Id, String>(); 
    ABC__c[] abcUpdates = new ABC__c[0]; 


    for (Contact a : Trigger.new) { 
        if (a.OwnerId != Trigger.oldMap.get(a.Id).OwnerId) {
            oldOwnerIds.put(a.Id, Trigger.oldMap.get(a.Id).OwnerId); 
            newOwnerIds.put(a.Id, a.OwnerId); 
            contactIds.add(a.Id); 
        }
    }

    if (!contactIds.isEmpty()) { 
        for (Contact con : [SELECT Id, (SELECT Id, OwnerId FROM ABC__c) FROM Contact WHERE Id in :contactIds]) { 
            String newOwnerId = newOwnerIds.get(con.Id); 
            String oldOwnerId = oldOwnerIds.get(con.Id); 
            for (ABC__c c : con.ABC__c) { 
                if (c.OwnerId == oldOwnerId) { 
                    ABC__c updatedabc = new ABC__c(Id = c.Id, OwnerId = newOwnerId); 
                    abcUpdates.add(updatedabc); 
                }
            }

        }
        update abcUpdates; 

    }
} catch(Exception e) { 
    System.Debug('OwnerUpdate failure: '+e.getMessage()); 
}
}
5
  • Did you try something ? It's not the intention of this site to offer free solution design services. We're here to help you progress or explain after you've tried to do something, and are unable to reach a solution. – Samuel De Rycke Nov 10 '14 at 10:01
  • Yes, I did wrote the trigger but it didn't worked. Updating the same – Salesforce Learner Nov 10 '14 at 10:04
  • Good!. What's not working- receiving any error ? – Samuel De Rycke Nov 10 '14 at 10:50
  • No, not receiving any error, but the child records are not getting transferred to new owner. – Salesforce Learner Nov 11 '14 at 4:31
  • I also tried another trigger, but its getting saved and gives an error "Didn't understand relationship 'ABC__c' in FROM part of query call. If you are attempting to use a custom relationship, be sure to append the '__r' after the custom relationship name. I'm attaching the new trigger as well with the questions. Please help. – Salesforce Learner Nov 11 '14 at 5:57
1

A trigger on Contact when the owner is Changed, change the owners of the child records ......

3
  • Could you please share a sample code. That would be really helpful. Thanks in advance. – Salesforce Learner Nov 10 '14 at 7:36
  • @SalesforceLearner this is not a forum where you can expect people to do the work for you. We're here to help you do that, but it means you have to at least try and do it yourself first. – Samuel De Rycke Nov 10 '14 at 9:57
  • Thanks for your response. Since I'm not getting the required result, that is why I asked for a sample code. – Salesforce Learner Nov 10 '14 at 10:10

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.