1

I have below Apex trigger code which fails to enter first if condition which it should do. Can someone suggest me a way out if by maps or other kind of developments? Please note

System.debug('@@@ABV_VALIDATION_2');

this line is not executing , hence not entering the if-clause .

Apex Trigger

trigger ABV_Validation_Check on Call2_vod__c (after update) {

for (Call2_vod__c Calling: Trigger.new){
System.debug('@@@ABV_VALIDATION_1');
if ((Calling.Parent_Call_vod__r.Status_vod__c == 'Submitted_vod') && (Calling.Account_vod__r.Account_Record_Type_Name_ABT__c != 'Prescriber') && (Calling.Account_vod__r.Account_Record_Type_Name_ABT__c != 'Non-Prescriber with ABS') && (Calling.Attendee_Associated_Prescriber_CIA__c==null) && (Calling.Attendees_vod__c== 0) && (Calling.Is_Parent_Call_vod__c==0))
 {  System.debug('@@@ABV_VALIDATION_2');

  if((Calling.RecordType.DeveloperName.contains('HCP_Detail_Call_CIA')) || (Calling.RecordType.DeveloperName.contains('HCP_Detail_Call_Prescriber')))
   {System.debug('@@@ABV_VALIDATION_3');
   calling.addError('trigger Says: Associated Account is required and must be a Prescriber');}

 }
       }
}
2
  • 2
    You cant get the parent object's fields in this trigger. You would need to query it.
    – AslamK
    Commented Dec 29, 2015 at 10:22
  • Can you please let me know how to do that? Commented Dec 29, 2015 at 10:23

2 Answers 2

1

Do like this way,

for (Call2_vod__c Calling: [SELECT Id, 
                            Parent_Call_vod__r.Status_vod__c, 
                            Account_vod__r.Account_Record_Type_Name_ABT__c
                     FROM Call2_vod__c WHERE Id IN : Trigger.newMap.keySet()]){
//your code here
}
1
  • that is actually a pretty tight solution..! I like it - you need to add the other fields directly on the object (such as Attendees_vod__c) to the query too though. Commented Dec 29, 2015 at 11:01
1

Firstly, out of interest.. is this kind of "validation" of fields to throw an error message on the record not easier/better implemented on a Salesforce validation rule? That way you can test and maintain it a lot easier, and don't need complex unit tests and release processes?

Secondly, you cannot lazy load the parent fields in this trigger. You will need to do something more like:

trigger ABV_Validation_Check on Call2_vod__c (after update) {

List<Id> parentIDs = new List<Id>();
for(Call2_vod__c thisVod : trigger.new) {
     parentIDs.add(thisVod.Parent_Call_vod__c);
}

Map<Id, Parent_call_vod__c> parentvods = new Map<Id, Parent_Call_vod__c>([SELECT Status_vod__c FROM Parent_call_vod__c WHERE Call2_vod__c IN :parentIDs]);

List<Id> accountIDs = new List<Id>();
for(Call2_vod__c thisVod : trigger.new) {
     accountIDs.add(thisVod.Account_vod__c);
}
Map<Id, Account_vod__r> accountvods = new Map<Id, Account_vod__r>([SELECT Account_Record_Type_Name_ABT__c FROM Account_vod__r WHERE Call2_vod__c IN :accountIDs]);

for (Call2_vod__c Calling: Trigger.new){
System.debug('@@@ABV_VALIDATION_1');
if ((parentvods.get(Calling.Parent_Call_vod__c).Status_vod__c == 'Submitted_vod') && (accountvods.get(Calling.Account_vod__c).Account_Record_Type_Name_ABT__c != 'Prescriber') && (accountvods.get(Calling.Account_vod__c).Account_Record_Type_Name_ABT__c != 'Non-Prescriber with ABS') && (Calling.Attendee_Associated_Prescriber_CIA__c==null) && (Calling.Attendees_vod__c== 0) && (Calling.Is_Parent_Call_vod__c==0))
 {  System.debug('@@@ABV_VALIDATION_2');

  if((Calling.RecordType.DeveloperName.contains('HCP_Detail_Call_CIA')) || (Calling.RecordType.DeveloperName.contains('HCP_Detail_Call_Prescriber')))
   {System.debug('@@@ABV_VALIDATION_3');
   calling.addError('trigger Says: Associated Account is required and must be a Prescriber');}

 }
       }
}

So I am loading the parent values into maps, and then "getting" the records in the IF query.

(nb: I hand typed this in the answer box so please don't comment if there is a syntax error!! You seem skilled enough to debug this up to working code!!)

5
  • Shouldn't this triggerIDs.add(thisVod.Id); be triggerIDs.add(thisVod.Parent_Call_vod__c); and use this to query for the parentVods map?
    – AslamK
    Commented Dec 29, 2015 at 10:32
  • Apart from Parent_Call_vod__r.Status_vod__c , all fields are from child call Commented Dec 29, 2015 at 10:38
  • @Simon I am getting below error at Line "Map<Id,Parent_Call_vod__c> Parentvods...." Error: expecting a map literal assignment, found '}' Commented Dec 29, 2015 at 10:49
  • 1
    @AslamK - ah yes, good spot, I have corrected that to use the two queries for parent and account IDs.. cheers (you must have had more coffee than me) Commented Dec 29, 2015 at 10:59
  • 2
    @SFDC_Learner - you are querying two related objects, Parent_Call_vod__r and Account_vod__r. Regarding the "error" - I used the wrong braces, sorry.. I have corrected them from {} to () - but I was hoping that was exactly the kind of syntax error you would have been able to battle out yourself ;) Commented Dec 29, 2015 at 11:00

You must log in to answer this question.

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