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
Chelsea LukowskiChelsea Lukowski 

Help with populating lookup field with delegated approver

I need help on how to populate a lookup field on a case based on the Account Owners Delegated Approver.

We have a lookup field(Pricing_Analyst__c) on a case(Product Substitution Request). 

Conditions to be met are:
Status = "New" - picklist field
Record Type = "Product Substitution Request" or "012q00000008phM"

Field to update:
Pricing_Analyst__c to be equal to the Account Owners Delegated Approver.

It can be a (before insert, before update) or (after insert) trigger. Any help would be appreciated.
Best Answer chosen by Chelsea Lukowski
Neetu_BansalNeetu_Bansal
Hi Chelsea,

You can try using this code. This will definately work:

trigger CaseTrg on Case( before insert, before update )
{
    Map<Id, Id> userToAccountMap = new Map<Id, Id>();
    Set<Id> accountIds = new Set<Id>();
    for( Case c : trigger.new )
    {
        if( trigger.isInsert
            || ( trigger.isUpdate
                && ( trigger.oldMap.get(c.Id).Status != c.Status
                    || trigger.oldMap.get(c.Id).RecordTypeId != c.RecordTypeId )
                )
            )
        {
            if( c.Status == 'New' && c.RecordTypeId == '012q00000008phM' )
            {
                accountIds.add( c.AccountId );
            }
        }
    }
    
    Set<Id> ownerIds = new Set<Id>();
    Map<Id, Id> ownerToDelegateMap = new Map<Id, Id>();
    for( Account acc : [ Select OwnerId, Id from Account where Id IN: accountIds ])
    {
        userToAccountMap.put( acc.Id, acc.OwnerId );
        ownerIds.add( acc.OwnerId );
    }

    for( User u : [ Select Id, DelegatedApproverId from User where Id IN: ownerIds ])
    {
        ownerToDelegateMap.put( u.Id, u.DelegatedApproverId );
    }


    for( Case c : trigger.new )
    {
        if( trigger.isInsert
            || ( trigger.isUpdate
                && ( trigger.oldMap.get(c.Id).Status != c.Status
                    || trigger.oldMap.get(c.Id).RecordTypeId != c.RecordTypeId )
                )
            )
        {
             if( c.Status == 'New' && c.RecordTypeId == '012q00000008phM' && userToAccountMap.containsKey( c.AccountId ) )
            {
                if( ownerToDelegateMap.containsKey( userToAccountMap.get( c.AccountId )))
                    c.Pricing_Analyst__c = ownerToDelegateMap.get( userToAccountMap.get( c.AccountId ));
            }
        }
    }
}

Thanks,
Neetu

All Answers

Neetu_BansalNeetu_Bansal
Hi Chelsea,

Is this Account Owners Delegated Approver a lookup field on Account?

Thanks,
Neetu
Chelsea LukowskiChelsea Lukowski
The field is called pricing analyst and it is a lookup field to users.
Neetu_BansalNeetu_Bansal
Hi Chelsea,

If my understanding is correct, below code will resolve your problem:

trigger CaseTrg on Case( before insert, before update )
{
    Map<Id, Id> userToAccountMap = new Map<Id, Id>();
    Set<Id> accountIds = new Set<Id>();
    for( Case c : trigger.new )
    {
        if( trigger.isInsert
            || ( trigger.isUpdate
                && ( trigger.oldMap.get(c.Id).Status != c.Status
                    || trigger.oldMap.get(c.Id).RecordTypeId != c.RecordTypeId )
                )
            )
        {
            if( c.Status == 'New' && c.RecordTypeId == '012q00000008phM' )
            {
                accountIds.add( c.AccountId );
            }
        }
    }
    
    for( Account acc : [ Select OwnerId, Id from Account where Id IN: accountIds ])
    {
        userToAccountMap.put( acc.Id, acc.OwnerId );
    }

    for( Case c : trigger.new )
    {
        if( trigger.isInsert
            || ( trigger.isUpdate
                && ( trigger.oldMap.get(c.Id).Status != c.Status
                    || trigger.oldMap.get(c.Id).RecordTypeId != c.RecordTypeId )
                )
            )
        {
            if( c.Status == 'New' && c.RecordTypeId == '012q00000008phM' && userToAccountMap.containsKey( c.AccountId ) )
            {
                c.Pricing_Analyst__c = userToAccountMap.get( c.AccountId );
            }
        }
    }
}

Thanks,
Neetu
Chelsea LukowskiChelsea Lukowski
First off I want to say thank you fro responding to me and helping. I can not tell you how much I appriciate it. This does get me a lot closer than what I was trying to come up with, but I need it to populate the delegated approver on the users profile. This populates the account owner.


To further explain, on the case their is an account associated, that account has an owner and that owner has a delegated approver. I need to populate with the delegate approvers name. Delegated Approver is on the users profile page and is a standard field. The field name is DelegatedApprover.

I hope that helps. 
Neetu_BansalNeetu_Bansal
Hi Chelsea,

Ok got it, try this:

trigger CaseTrg on Case( before insert, before update )
{
    Map<Id, Id> userToAccountMap = new Map<Id, Id>();
    Set<Id> accountIds = new Set<Id>();
    for( Case c : trigger.new )
    {
        if( trigger.isInsert
            || ( trigger.isUpdate
                && ( trigger.oldMap.get(c.Id).Status != c.Status
                    || trigger.oldMap.get(c.Id).RecordTypeId != c.RecordTypeId )
                )
            )
        {
            if( c.Status == 'New' && c.RecordTypeId == '012q00000008phM' )
            {
                accountIds.add( c.AccountId );
            }
        }
    }
    
    Set<Id> ownerIds = new Set<Id>();
    Map<Id, Id> ownerToDelegateMap = new Map<Id, Id>();
    for( Account acc : [ Select OwnerId, Id from Account where Id IN: accountIds ])
    {
        userToAccountMap.put( acc.Id, acc.OwnerId );
    }
    
    for( User u : [ Select Id, DelegatedApproverId from User where Id IN: ownerIds ])
    {
        ownerToDelegateMap.put( u.Id, u.DelegatedApproverId );
    }

    for( Case c : trigger.new )
    {
        if( trigger.isInsert
            || ( trigger.isUpdate
                && ( trigger.oldMap.get(c.Id).Status != c.Status
                    || trigger.oldMap.get(c.Id).RecordTypeId != c.RecordTypeId )
                )
            )
        {
            if( c.Status == 'New' && c.RecordTypeId == '012q00000008phM' && userToAccountMap.containsKey( c.AccountId ) )
            {
                if( ownerToDelegateMap.containsKey( userToAccountMap.get( c.AccountId )))
                    c.Pricing_Analyst__c = ownerToDelegateMap.get( userToAccountMap.get( c.AccountId ));
            }
        }
    }
}

Thanks,
Neetu
Chelsea LukowskiChelsea Lukowski
Hi Neetu,

Now it does not fill in any name. 
Chelsea LukowskiChelsea Lukowski
No we do not. I have GoToMeeting.
Chelsea LukowskiChelsea Lukowski
Sorry, I got pulled away Friday. It still is not pulling in the delegated approver. 
Chelsea LukowskiChelsea Lukowski
No, it still is not updating and the code looks fine. The delegated approver is filled in on the user profile (I made sure I used Accounts that I knew for sure had a delegated approver). I am still stuck. 
Neetu_BansalNeetu_Bansal
Hi Chelsea,

You can try using this code. This will definately work:

trigger CaseTrg on Case( before insert, before update )
{
    Map<Id, Id> userToAccountMap = new Map<Id, Id>();
    Set<Id> accountIds = new Set<Id>();
    for( Case c : trigger.new )
    {
        if( trigger.isInsert
            || ( trigger.isUpdate
                && ( trigger.oldMap.get(c.Id).Status != c.Status
                    || trigger.oldMap.get(c.Id).RecordTypeId != c.RecordTypeId )
                )
            )
        {
            if( c.Status == 'New' && c.RecordTypeId == '012q00000008phM' )
            {
                accountIds.add( c.AccountId );
            }
        }
    }
    
    Set<Id> ownerIds = new Set<Id>();
    Map<Id, Id> ownerToDelegateMap = new Map<Id, Id>();
    for( Account acc : [ Select OwnerId, Id from Account where Id IN: accountIds ])
    {
        userToAccountMap.put( acc.Id, acc.OwnerId );
        ownerIds.add( acc.OwnerId );
    }

    for( User u : [ Select Id, DelegatedApproverId from User where Id IN: ownerIds ])
    {
        ownerToDelegateMap.put( u.Id, u.DelegatedApproverId );
    }


    for( Case c : trigger.new )
    {
        if( trigger.isInsert
            || ( trigger.isUpdate
                && ( trigger.oldMap.get(c.Id).Status != c.Status
                    || trigger.oldMap.get(c.Id).RecordTypeId != c.RecordTypeId )
                )
            )
        {
             if( c.Status == 'New' && c.RecordTypeId == '012q00000008phM' && userToAccountMap.containsKey( c.AccountId ) )
            {
                if( ownerToDelegateMap.containsKey( userToAccountMap.get( c.AccountId )))
                    c.Pricing_Analyst__c = ownerToDelegateMap.get( userToAccountMap.get( c.AccountId ));
            }
        }
    }
}

Thanks,
Neetu
This was selected as the best answer
Chelsea LukowskiChelsea Lukowski
That worked! Thank you!
Chelsea LukowskiChelsea Lukowski
What would be the test class for this trigger?