Example of Assigning Account Owner as the Owner of a Lead

You can configure your sales application to automatically set a lead owner based on the owner of the associated account.

For example, an employee in your organization who isn't a salesperson, has created a lead with an associated account. You can use a Groovy script to automatically assign that lead to the salesperson who owns the associated account.

You are a sales administrator and your management has asked you to create a script to automate setting the account owner as the lead owner after the lead is created or assigned. You can use Groovy scripts to cater for the these scenarios:

  • When a lead is created with an associated account and the assignment process is run

  • When a lead is created without an account, but the account is added later and the assignment process is run

Note: You can't set the account owner as the lead owner for leads created or updated using import management.

Set Account Owner as the Lead Owner After Lead is Created or Assigned

Here's how to set the account owner as the lead owner after the lead is created or assigned:
  1. Sign in using the sales administrator role.

  2. Create a sandbox and activate it.

    For more information about sandboxes, see the Overview of Sandboxes topic.
  3. Navigate to Application Composer and expand Standard Objects, then Sales Lead and then click Server Scripts.

  4. Click the Triggers tab, and then click the Create icon in the Object Triggers section.

  5. Select the Before Insert in Database trigger type. Enter a name such as UpdatLeadOwnerOnCreate in the Trigger Name field. Copy and paste the following content of the script to the Trigger Definition section:

    if(nvl(CustomerId,null) != null)
    {
       def account = newView('OrganizationProfile');
       def criteria = account?.createViewCriteria();
       def criteriaRow = criteria?.createRow();
       criteria?.insertRow(criteriaRow);
       def criteriaItem = criteriaRow?.ensureCriteriaItem('PartyId');
       criteriaItem?.setValue(CustomerId);
       account?.appendViewCriteria(criteria);
       account.executeQuery();
       if(account.hasNext())
       {
            def accRow = account.next();
            def leadResourceIterator = nvl(MklLeadResources,null);
            if(nvl(accRow?.OwnerPartyId,null) !=null)
           {
             setAttribute('OwnerId' , accRow?.OwnerPartyId);
             def resourceId = accRow?.OwnerPartyId;
             boolean alreadyOnSalesTeam = false;
             leadResourceIterator?.reset();
             for (; leadResourceIterator?.hasNext(); ) {
             def mklLeadResourcesVORowImpl = leadResourceIterator.next();
             Long existingResourceId = mklLeadResourcesVORowImpl.getAttribute('ResourceId');
             if (resourceId.equals(existingResourceId)) {
             alreadyOnSalesTeam = true;
             break;
            }
              }
              if(!alreadyOnSalesTeam) {
              def leadResourceVO = leadResourceIterator.createRow();
              leadResourceVO.setAttribute('LeadId',LeadId);
              leadResourceVO.setAttribute('ResourceId',resourceId);
              leadResourceVO.setAttribute('PrimaryFlag','Y');
            }
          }
        }
    } 
  6. Click Save and Close. Verify that you see the newly created trigger in the Object Triggers section.

  7. Next, click the Create icon in the Object Triggers section to create another object trigger.

  8. Select Before Update in Database trigger type. Enter a name such as UpdatLeadOwnerOnUpdate in the Trigger Name field. Copy and paste the following content of the script to the Trigger Definition section:

     if(isAttributeChanged('LastAssignmentDate')){
       def account = newView('OrganizationProfile');
       def criteria = account?.createViewCriteria();
       def criteriaRow = criteria?.createRow();
       criteria?.insertRow(criteriaRow);
       def criteriaItem = criteriaRow?.ensureCriteriaItem('PartyId');
       criteriaItem?.setValue(CustomerId);
       account?.appendViewCriteria(criteria);
       account.executeQuery();
       if(account.hasNext())
       {
          def accRow = account.next();
          def leadResourceIterator = nvl(MklLeadResources,null);
          if(nvl(accRow?.OwnerPartyId,null) !=null)
          {
             setAttribute('OwnerId' , accRow?.OwnerPartyId);
             def resourceId = accRow?.OwnerPartyId;
             boolean alreadyOnSalesTeam = false;
             leadResourceIterator?.reset();
             for (; leadResourceIterator?.hasNext(); ) {
        def mklLeadResourcesVORowImpl = leadResourceIterator.next();
        Long existingResourceId = mklLeadResourcesVORowImpl.getAttribute('ResourceId');
        if (resourceId.equals(existingResourceId)) {
        alreadyOnSalesTeam = true;
        break;
        }
       }
         if(!alreadyOnSalesTeam) {
         def leadResourceVO = leadResourceIterator.createRow();
         leadResourceVO.setAttribute('LeadId',LeadId);
         leadResourceVO.setAttribute('ResourceId',resourceId);
         leadResourceVO.setAttribute('PrimaryFlag','Y');
       }
      }
     }
    }
  9. Click Save and Close. Verify that you see the newly created trigger in the Object Triggers section.

  10. Thoroughly test these scenarios in your sandbox environment:

    • When a lead is created with an associated account and the assignment process is run.

    • When a lead is created without an account, but the account is added later and the assignment process is run.

  11. Publish the sandbox after successfully verifying your tests.