1

I want to check in my trigger on a case whether the email Id of the person account for the case is an null or not. I have a lookup to contact(person account) on the case page. How to achieve this.

for (case caseObj : trigger.new){
 ......
}

in this for loop I tried

caseObj.account.personal email == null;
caseObj.contact.email == null;
caseObj.ContactEmail == null;

but it didn't work out can anyone help.

  • 1
    Even I faced similar issue. Ultimately I had to post a query for that particular contact. Keep us posted if you find anything regarding this. – A-d Jul 2 '14 at 13:25
  • Posting a query was the possible solution. But solution by @Marty C. also holds value and is worth a look. – Bhoopesh Chauhan Jul 3 '14 at 7:08
1

The reason why your null tests fail is that in a Trigger on Case, the lookup objects are not in scope. To wit:

for (Case caseobj : Trigger.new)
    caseObj.contact.email == null;

Trigger.new will have in scope the fields on Case, but not the parent fields on account or contact. You need to query for these:

Map<ID,Case> caseIdToCaseWithParentMap = new Map<ID,Case>(
        [select id, contact.email from Case in :Trigger.new]);

for (Case caseobj: Trigger.new)
    if (caseIdToCaseWithParentMap.get(caseObj.id).contact.email == null) // do something 
| improve this answer | |
  • Thanks crop1645. I implemented this approach to solve my problem as this was the most suitable one. – Bhoopesh Chauhan Jul 3 '14 at 6:34
  • I have one Question though. I do have a standard field 'Contact Email' on the case, which gets populated once I select the contact and save the case. Can't I use this field to check if contact has email or not. – Bhoopesh Chauhan Jul 3 '14 at 7:16
  • 1
    Only standard fields documented here salesforce.com/us/developer/docs/object_reference/index.htm will be available. With Cases, SFDC makes available some convenience fields in the UI which aren't actually on the object that a trigger references. 'Contact Email' is not one of the actual fields in the Case SObject and hence is not available to the trigger.new context – cropredy Jul 3 '14 at 18:44
1

Bhoopesh, one way you can access the related Contact record's Email value is to create a Formula (Text) field on the Case object called Contact Email (ContactEmail__c). The formula just needs to be: Contact.Email

Once you have this formula field, you'll be able to use this formula field to determine whether the Contact record has a blank Email, as demonstrated in the following example.

trigger RequireContactEmail on Case (before insert) {
    for (Case eachCase : Trigger.new) {
        System.assert(eachCase.ContactEmail__c != null,
            'The Contact must have an Email value');
    }
}

See the screenshot below for the definition of the formula field, paying special attention to the setting to treat blank fields as blanks.

enter image description here

| improve this answer | |
  • But sadly I can't create a new formula field due to some restrictions from business. So I fetched it from the system using SOQL query just as @crop1645 mentioned. – Bhoopesh Chauhan Jul 3 '14 at 7:05
  • I have one Question though. I do have a standard field 'Contact Email' on the case, which gets populated once I select the contact and save the case. Can't I use this field to check if contact has email or not. – Bhoopesh Chauhan Jul 3 '14 at 7:17
  • 1
    The standard Contact Email field is not available for use in formula fields or Apex at the current time. – Marty C. Jul 3 '14 at 11:39
0

I strongly suspect the problem you're having is that you're testing for "null". An email field is a special case of a text field. You need to be testing for isEmpty() == false. Your code should look more like this:

Boolean emlResult = caseObj.contact.email.isEmpty();
if(emlResult == true){

 // perform operation

}else{

// report no email address and do something else

}
| improve this answer | |

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.