0

I am writing a simple before trigger to update a lookup field on lead to user object with the id of owner of record (owner could be a user or a queue). But on update of record i am getting an error.

My code:

trigger FetchTccValues on Lead (before insert, before update) { Group queue = [Select Id, name from Group where type='Queue' and Name='TCC Counsellors']; List<GroupMember> queueMembers = [Select Group.Name, UserOrGroupId From GroupMember where GroupId =: queue.Id ]; for( Lead le : Trigger.new ) { for (GroupMember qm :queueMembers ) { if( le.ownerid == queue.Id || le.ownerid == qm.UserOrGroupId ) { le.TCC_Lead_Status__c = le.Status; le.TCC_Lead_Owner__c = le.ownerid; } } } } 

On updating a record I am getting error:

Apex trigger FetchTccValues caused an unexpected exception, contact your administrator: FetchTccValues: data changed by trigger for field TCC Lead Owner: id value of incorrect type: 00Gp0000000kCaCEAU

1 Answer 1

2

You need to make sure that the owner is user because you can't link a queue in a user's lookup.

There are many ways you can do this.

  • You can do something like

if(String.ValueOf(le.ownerid.getSObjectType()) == 'User'){ le.TCC_Lead_Status__c = le.Status; le.TCC_Lead_Owner__c = le.ownerid; } 
  • As user Record ID Prefix does not change so you can also hard code them.

if(le.ownerid.substring(0,3) == '005'){ le.TCC_Lead_Status__c = le.Status; le.TCC_Lead_Owner__c = le.ownerid; } 
3
  • But record owner could be a queue sometimes..... In that case there is no solution or workaround ? Commented Nov 28, 2016 at 10:00
  • @user3486888 nope because Salesforce don't allow us to link with a queue. Although you can create a text field and if this queue you can store ID in that field. Commented Nov 28, 2016 at 10:01
  • 1
    That's bad news for me :( ..Any way i also adopted the text field option......Thanks for the quick reply ! Commented Nov 28, 2016 at 10:12

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.