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
PawelWozniakPawelWozniak 

Problems with modifying trigger to be bulk. Trigger gets Owner Role Name.

Dear Community I am struggling with conversion of trigger to be bulk.

Trigger is set on Opportunity object, before insert and before update.

Task is simple. Get Owner Role Name and put it to field ptt_Owner_Role__c (Text 255)

 

I have already written code like this:

trigger ptt_Opportunity_Trigger on Opportunity (before insert, before update) {

updateOwnerRoleField();

public static void updateOwnerRoleField() { for (Opportunity currentOpp : Trigger.new) { Id ownerId = currentOpp.OwnerId; If (ownerId != null) { Id ownerRoleId = [Select UserRoleId From User WHERE User.Id=:ownerId].UserRoleId; String roleName = [SELECT Name FROM UserRole WHERE UserRole.Id=:ownerRoleId].Name; currentOpp.ptt_Owner_Role__c = roleName; } } }
}

 

 This works fine untill I need to do mass update of records. It is not bulk trigger and try to go beyound the limits because of SOQL statements in for loop. I understand the problem.

 

So I am trying to make it bulk using this article as a source http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_triggers_bulk_idioms.htm I have written this:

 

trigger ptt_Opportunity_Trigger on Opportunity (before insert, before update) {

updateOwnerRoleField();

public static void updateOwnerRoleField() {
	// For every Opportunity record, add its associated OwnerId 
    // to a set so there are no duplicates. 
    
    Set<Id> ownerIds = new Set<Id>();
    for (Opportunity opp : Trigger.new) 
        ownerIds.add(opp.OwnerId);
        
    // Query the Users for their associated RoleId
    
    Map<Id, User> entries = new Map<Id, User>(
        [select User.UserRoleId from User where id in :ownerIds]);
         
    // Now use the map to set the appropriate RoleId every Opportunity Owner Role field by the trigger. 
    
    for (Opportunity opp : Trigger.new)
(error)        opp.ptt_Owner_Role__c  = entries.get(User.UserRoleId).opp.OwnerId;
		
	}
}

 (error) means - Save error: Incompatible key type Schema.SObjectField for MAP<Id,User> 

and now I stuck.

 

I now that at this monet there is missing part for finding role name but at least I would like to see RoleID in field  

ptt_Owner_Role__c then I will add another step to discovery its name.

 

Please point what I am doing wrong. 

Navatar_DbSupNavatar_DbSup

Hi,

Try the below code asreference:

 

 trigger ptt_Opportunity_Trigger on Opportunity (before insert, before update)
{
// For every Opportunity record, add its associated OwnerId
// to a set so there are no duplicates.

Set<Id> ownerIds = new Set<Id>();
for (Opportunity opp : Trigger.new)
ownerIds.add(opp.OwnerId);

// Query the Users for their associated RoleId

Map<Id, User> entries = new Map<Id, User>(
[select User.UserRoleId,userrole.name from User where id in :ownerIds]);

// Now use the map to set the appropriate RoleId every Opportunity Owner Role field by the trigger.

for (Opportunity opp : Trigger.new)
opp.ptt_Owner_Role__c = entries.get(opp.ownerId).userrole.name;


}

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

PawelWozniakPawelWozniak

This didn't solve the problem. Still getting:

Incompatible key type Schema.SObjectField for MAP<Id,User>

 

As I found in Schema (using eclipse) "User" object do not contains information about UserRole.Name there is only field RoleId. 

This information is stored in separate object called "UserRole" which has two most interesting fields "Id" (equals to value from User.RoleId) and "Name" which is final information that i need to get and store in ptt_Owner_Role__c.

 

I suppose that I need to write another step with separate query to find name when I have Role.Id.

 

At current stage I would like to have even RoleId saved in ptt_Owner_Role__c but with working code which can be saved without errors. 

I do not understand error:  Incompatible key type Schema.SObjectField for MAP<Id,User>