1

I am trying to write a trigger on Case that if the Contact is null, it will assign a default. I am just trying to prove the concept right now, I eventually want it to look up the Contact ID from another object.

When I insert a Case with a blank Contact, it continues to be blank. I can't see what I am missing. Thanks for any help.

trigger WVCaseTrigger on Case ( before insert) {
  Contact cntct = [Select ID from Contact where ID = '003F000000q6KzW'];
  for(Case cs : trigger.new){
    if (cs.Contact == null){        
        cs.Contact = cntct;
        system.debug('Contact:' + cs.Contact);
    }
  }
}

1 Answer 1

4

The field is ContactId. I am not sure why your trigger would not detect null in this case, but changing to the proper field reference should resolve the issue.

if (cs.ContactId == null) cs.ContactId = cntct.Id;

Also make sure you have activated the Apex Trigger. You can check its metadata for the IsActive flag, or examine it through the UI.

2
  • Wow. That is all it was - thanks so much for the help!
    – Todd Sells
    Mar 9, 2016 at 19:21
  • Always a pleasure. It's the little things, haha. Best of luck. @ToddSells
    – Adrian Larson
    Mar 9, 2016 at 19:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .