You need to sign in to do that
Don't have an account?
Chelsea 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.
.
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.
.
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
Is this Account Owners Delegated Approver a lookup field on Account?
Thanks,
Neetu
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
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.
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
Now it does not fill in any name.
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