0

I wrote a trigger on Lead:

trigger LeadTrigger on Lead(before insert) {

  User u = [select id,Team__c from User where id=:UserInfo.getUserId()];

  for(Lead ll: Trigger.new){
    if(ll.Team__c==null)
      ll.Team__c = u.Team__c;
  }        
}

The trigger should update the custom field Team__c in Lead with the value of Team__c on User. I want the user to be able to change the field if the prepopulated value is not desired.

Unfortunately, at the moment this trigger doesn't seem to work at all because the field is not prepopulated. Tia.

  • 1
    Are you sure that Team__c is populated for the user that is causing the trigger to fire? – BarCotter Dec 8 '14 at 11:17
  • Yes. Team__c in User has a value set for that user. – Lily Dec 8 '14 at 11:22
  • I have updated the code in which the trigger name is updated to follow better naming convention. A trigger named Lead is itself quite confusing. This is good concise reference to naming conventions. – Mahmood Dec 8 '14 at 12:32
  • Did this resolve your issue? If not, are both fields of the same type? – crmprogdev Dec 8 '14 at 15:12
  • Are you sure you are only testing this on Lead Record creation? This trigger will only work when creating a lead, and not on updates. – Double A Dec 8 '14 at 16:31
0

"I want the user to be able to change the field if the prepopulated value is not desired."

If the above is true, you've not told us what values would not be desired, so I'm going to 'assume', that would be any value at all and you can add conditions later. In essence, the trigger should be able to change the value whether the value is pre-populated or not. Your trigger should look more like the following:

trigger LeadTrigger on Lead(before insert) {

  User u = [select id,Team__c from User where id=:UserInfo.getUserId()];

  for(Lead ll: Trigger.new){
    if(string.isEmpty(ll.Team__c)){
       ll.Team__c = u.Team__c;
    }else{                             // can always add `else if` conditions here later
       ll.Team__c = u.Team__c;
    }
}

Strings values are generally not null, instead they should be tested to see if they're empty. This is likely the reason your trigger isn't populating the fields that don't have a value. Also make certain that both Team__c on Lead and on User are the same Type, which I'm assuming is a string. If not, you'll need to do something like string.valueOf(u.Team__c) if Team__c is a number instead of a string, but is a text string in Lead.

  • crmprogdev -- I think you want if (String.isEmpty(ll.Team__c)) because if team__c is null, you'll get an attempt to de-reference null object and isEmpty is not an instance method on String – cropredy Dec 8 '14 at 17:32
  • Yup! Thank you, will correct the code. That's what happens when writing it on the fly. Just knew I wanted to convey that it needed to be isEmpty instead of null. – crmprogdev Dec 8 '14 at 18:03
  • np - some other sfse maven pointed the same issue out to me not that long ago! – cropredy Dec 8 '14 at 20:14
  • That's part of what makes this place so great. Lots of nuances one can pick up that aren't always so obvious. – crmprogdev Dec 8 '14 at 21:36

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.