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
VeMan1542VeMan1542 

Trigger on Web2Case-sourced Cases for checking and modifying the ContactID

I'm attempting to write atrigger that will check the Case Origin to se if it's Web2Case, and, if so, remove the ContactID from the Contact field  (or the Contact Name, but as it's a lookup field I believe I should use the ID#).
 
We have two sets of contact records, one is used exclusively for Support Cases, the other for Sales Activity. When a Case comes in from the web, occasionally the only record with a matching email may be a sales prospect, which then gets attached to the case. We are trying to prevent this, and the web2case settings and configuration doesn't allow us to prevent this activity.
 
So, I hope someone can tell me what I'm doing wrong. My understanding is that a 'before insert' trigger acts upon the pending sObject, so I don't have to create it with a Trigger.new call. Is that right? Here is what I've got so far:
 
trigger Web2CaseContact on Case (before insert) {
    if(Case.Origin = 'Web2Case') {
    Case.ContactID = "";
  }
}
 
This gives me an error message: Compile Error: Invalid field for upsert, must be an External Id custom or standard indexed field: ContactID at line 3 column 5
 
I feel like I'm close, but that I'm still using the wrong case field references, or something. Any help so I can understand this better would be appreciated!
 
VeMan
 
 
Best Answer chosen by Admin (Salesforce Developers) 
TehNrdTehNrd
Contact and Account are not even fields on Case, whoops. I'm not sure why it even let you save.

Code:
trigger Web2CaseContact on Case (before insert, before update) {

 for(Case cas : Trigger.New){
  if((cas.Origin == 'Web2Case') &&  (cas.ContactId != null)) {
   cas.ContactId = null;
   cas.AccountId = null;
  }
 }
}

 



Message Edited by TehNrd on 12-20-2007 04:25 PM

All Answers

VeMan1542VeMan1542

OK

So, I think I'm closer with this version:

trigger Web2CaseContact on Case (before insert) {
  Case[]newCase = Trigger.new;
 if((newCase[0].Origin == 'Web2Case') &&  (newCase[0].Contact != null)) {
    newCase[0].Contact = null;
    newCase[0].Account = null;}
 }

This was able to save successfully at least. However, I think I need to pass the null values back to to the fields. I'm not quite sure how to do that.

I tried:

newCase[0].Contact.set(null);

but this is obviously wrong as it wouldn't compile. I thought that ..."Contact = null;" in a before insert trigger wouild be enough to clear the field prior to saving the case, but I created and saved a Web2Case case and the Contact value was still there.

I know I'm a baby coder and that my mistakes must be incredibly elementary, but any help or pointers would be appreciated.

TehNrdTehNrd
Try this: 

Code:
trigger Web2CaseContact on Case (before insert) {

 for(Case case : Trigger.New){
  if((case.Origin == 'Web2Case') &&  (case.Contact != null)) {
   case.Contact = null;
   case.Account = null;
  }
 }
}

 

VeMan1542VeMan1542

I tried that and got the same result - the case saves without removing the Contact Name and the Account Name. It could be the way I'm testing it. I'm basically creating a new case, changing the Origin to 'Web2Case' and adding a Sales Contact name, then saving the Case. The case saves with name and account intact.

So, I'm going to try adding a criteria around the Record Type of the Contact, but I can't help but believe that I'm still not passing the null values back into the case, even though "newCase.Contact = null" would seem to be doing exactly that.

Is there some kind of "set" command I need to use?

TehNrdTehNrd
Contact and Account are not even fields on Case, whoops. I'm not sure why it even let you save.

Code:
trigger Web2CaseContact on Case (before insert, before update) {

 for(Case cas : Trigger.New){
  if((cas.Origin == 'Web2Case') &&  (cas.ContactId != null)) {
   cas.ContactId = null;
   cas.AccountId = null;
  }
 }
}

 



Message Edited by TehNrd on 12-20-2007 04:25 PM
This was selected as the best answer
VeMan1542VeMan1542

That did it!! It was looking for an ID number. I thought I tried that earlier, but I was having some trouble with my syntax (using '=' instead of '==').

Thank you so much for responding and for your help.

VeMan

TehNrdTehNrd
No prob.