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
cbrocbro 

Help to change Lead Trigger to only change Account Owner when none exists

Help with this trigger:

 

  • I don't want to change the Account Owner if there is ALREADY an Account Owner
  • This Lead should become an Opportunity for the person who is converting it (and it should be in their name), but it should NOT change the Account Owner at all unless the following is true: 
  1. ADD an Account Owner (to the converter of the Lead) if they are creating a NEW Account from a Lead. 
  2. ADD an Account Owner if there is no Account Owner for the existing Account.

How would I change this trigger to do so?

 

This trigger was built by someone else a long time ago - and I am not sure I understand all of the pieces.  :/

 

Any help would be appreciated...

 

Additionally - I have put in a validation rule that does not allow specific users to Change Account Owners - (which is what's causing this to stop working...) - that said - I still don't want them to change Account Owners - I just want them to be able to add an Account Owner if it is null or if they are creating a new Account.  (See my validation rule below)... would I need to change that, as well?

 

trigger Lead_UpdateAccountsOwner on Lead (after update) {

System.debug('DEBUG::------->Lead_UpdateAccountOwner - Converted Account ID : '  + trigger.new[0].convertedAccountId );
    
    //this needs to be updated every time a new Profile is added that converts Leads
    List<String> lstOfProfiles=new List<String>();
    lstOfProfiles.add('Product Specialist Standard User');
    lstOfProfiles.add('Product Specialist System Admin User');
    lstOfProfiles.add('Product Specialist User');
    lstOfProfiles.add('Product Specialist System Admin User 2');
    
    //this needs to be updated every time a new Role is added that converts Leads
    List<String> roleNames= new List<String>();
    roleNames.add('PM SuperUser');
    roleNames.add('Performance Manager');
    
    //calling function from validating class
     Boolean resultp=ProfileValidator.validateLoginUserProfile(lstOfProfiles);
     Boolean result = false;
     result=  ProfileValidator.CheckLoginUserRole(UserInfo.getUSerId());
     
  if(result == true)
  {
    //get converted account ids in set
    set<id> accountIds = new Set<Id>();
    set<id> contactIds = new Set<Id>();
    
    for(Lead ld: trigger.new)
    {
        accountIds.add(ld.convertedAccountId);
        contactIds.add(ld.convertedContactId);
    }
    
    //query all converted accounts with owenr id
    Map<id,Account> map_AssociatedAccounts = new Map<id,Account> ([SELECT id, OwnerId from Account WHERE id in: accountIds and Type = 'Prospect']);
    Map<id,Contact> map_AssociatedContacts = new Map<id,Contact> ([SELECT id, OwnerId from Contact WHERE id in: contactIds]);
    
    //change owner of associated accounts
    for(Lead ld: trigger.new)
      {
         Account act = map_AssociatedAccounts.get(ld.convertedAccountId);
         if(act!=null)
          {
             act.OwnerId = ld.OwnerId;
          }
         
         Contact con = map_AssociatedContacts.get(ld.convertedContactId);
         if(con!=null)
          {
             con.OwnerId = ld.OwnerId;
          }
      }
    
    //update accounts
    update map_AssociatedAccounts.values();
    update map_AssociatedContacts.values();


  }
 

}

 

 

 VALIDATION RULE: Account_Ownership_Change_Not_Allowed

 

NOT(ISNEW()) &&
AND(
OR( $User.ProfileId ='00e80000001KC2s',
$User.ProfileId ='00e80000001K8tw',
$User.ProfileId ='00e30000000eH6i',
$User.ProfileId ='00e30000000eH6h',
$User.ProfileId ='00e80000001KCUK'),
ISCHANGED(OwnerId))

 Error Message: You cannot change the Account Owner.