+ Start a Discussion
Sean ClarkSean Clark 

Creating an Apex Trigger to have an Alert on the Contact

Hi, 
I am trying to create an Apex Trigger to create an Alert saying 'Are you sure you want to save without filling in the Availability?' if the field: Availability is blank and the PickList field: Candidate Status is either Consult Hot, Consult Warm, Perm Active, Perm Passive, Assigned, Placed or Prospect.

Can anyone help me with this?

The errors are showing: Line7 - Expecting ')' but was: 'AND'
                                       Line14 - Expecting ';' but was: ')'


trigger ContactAlert2 on Contact (before update) {
    
    for(Contact c :Trigger.New){
     
        If(
           ISBLANK(Availability__c)
        AND(
           ISPICKVAL(c.TR1__Candidate_Status__C = 'Consult Hot') || 
           ISPICKVAL(c.TR1__Candidate_Status__c, 'Consult Warm') || 
           ISPICKVAL(c.TR1__Candidate_Status__c, 'Perm Active') ||
           ISPICKVAL(c.TR1__Candidate_Status__c, 'Perm Passive') ||
           ISPICKVAL(c.TR1__Candidate_Status__c, 'Placed') ||
           ISPICKVAL(c.TR1__Candidate_Status__c, 'Assigned') ||
           ISPICKVAL(c.TR1__Candidate_Status__c, 'Prospect')))
            
           c.addError('Are you sure you want to save without filling in the Availability?');

            }
}

 
Raj VakatiRaj Vakati
Try this
trigger ContactAlert2 on Contact (before update) {
    
    for(Contact c :Trigger.New){
     
       IF (c.Availability__c==NULL && 
     
           ( c.TR1__Candidate_Status__C == 'Consult Hot' || 
         c.TR1__Candidate_Status__c== 'Consult Warm' || 
            c.TR1__Candidate_Status__c== 'Perm Active' ||
            c.TR1__Candidate_Status__c== 'Perm Passive' ||
            c.TR1__Candidate_Status__c== 'Placed' ||
            c.TR1__Candidate_Status__c== 'Assigned' ||
            c.TR1__Candidate_Status__c== 'Prospect')){
            
           c.addError('Are you sure you want to save without filling in the Availability?');

            }
}

 
Raj VakatiRaj Vakati
i think you can able to do it with the validation rule no need of any code 
 
AND(
           ISBLANK(Availability__c),
		   NOT(ISNEW()), 
        OR(
           ISPICKVAL(c.TR1__Candidate_Status__C = 'Consult Hot') || 
           ISPICKVAL(c.TR1__Candidate_Status__c, 'Consult Warm') || 
           ISPICKVAL(c.TR1__Candidate_Status__c, 'Perm Active') ||
           ISPICKVAL(c.TR1__Candidate_Status__c, 'Perm Passive') ||
           ISPICKVAL(c.TR1__Candidate_Status__c, 'Placed') ||
           ISPICKVAL(c.TR1__Candidate_Status__c, 'Assigned') ||
           ISPICKVAL(c.TR1__Candidate_Status__c, 'Prospect')))



 
Sean ClarkSean Clark
@Raj V

Thank you for this.

The problem with doing this with a Validation Rule is that it wont let you save the record without amending the field. 

What i'm looking for is for an alert to pop up reminding them that they haven't filled in the field but if they haven't got the information to input, they can save it anyway.

Is this possible? As i know the code will do the exact same as a validation rule, could you make it so that the user can still save the record?