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

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;
}
| improve this answer | |
  • But record owner could be a queue sometimes..... In that case there is no solution or workaround ? – Dnyaneshwar Nov 28 '16 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. – Tushar Sharma Nov 28 '16 at 10:01
  • 1
    That's bad news for me :( ..Any way i also adopted the text field option......Thanks for the quick reply ! – Dnyaneshwar Nov 28 '16 at 10:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.