1

I am running into the good ol' variable does not exist error while creating a simple trigger on the case object. As other posts here have suggested, I've checked for a class with the same name as Case but I see none - there are however existing triggers present for the Case object, some of which are managed code. I have also tried changing the for loop to for(Schema.Case)

trigger caseInvalid on Case (after insert) 
{
    for(Case cas : trigger.new)
    {
        if(cas.status = ISPICKVAL(status, 'New') && cas.origin = ISPICKVAL(origin, 'Email - Client Collections Services') && !subject.contains('RE:'))
        {
            cas.status = ISPICKVAL(status, 'Invalid');
        }
    }
}
2
  • Likely status in here ISPICKVAL(status. Use e.g. cas.status == 'New' instead. Note = is assignment not comparison.
    – Keith C
    Commented Jun 17 at 20:41
  • Also, it's always a good idea to include the full error message + stack trace, *verbatim*. Shortening the error or paraphrasing means you're almost certainly leaving out important information.
    – Derek F
    Commented Jun 17 at 20:49

1 Answer 1

1

You can't mix formula functions and Apex, they're mutually exclusive.

So ISPICKVAL() isn't a thing in Apex, and that's the source of your error.

In Apex, picklists are effectively strings. Also, as KeithC mentioned, = (single equals) is the assignment operator whereas == (double equals) is the equality operator.

// You probably want this to be a "before insert" trigger rather than an
//   "after insert" one.
// We can modify values in trigger.new/trigger.newMap in before triggers
//   and have them be applied without DML
// If you use an after trigger, you'd need to use DML
trigger caseInvalid on Case (before insert) 
{
    // Apex is generally not case-sensitive "Case" == "case"
    // Using "case" for the loop variable name is fine, you'd just
    //   probably need to use Schema.Case if you wanted to declare
    //   a new Case variable (or create a new Case instance)
    //   inside the scope of this loop
    for(Case case : trigger.new)
    {
        // Picklists are basically treated as strings in Apex
        // == for string comparison
        // The String class has a .contains() method, so that part is fine
        //   but you do need to access that field using dot notation
        //   just like every other Case field
        if(case.status == 'New' && case.origin == 'Email - Client Collections Services' && !case.subject.contains('RE:'))
        {
            // Setting a picklist field is just assigning a string literal to it
            // Be careful if your picklist is defined as "Restricted"
            cas.status = 'Invalid';
        }
    }
}
0

You must log in to answer this question.

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