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
MaggieSumitMaggieSumit 

I have written a validation on account which cause an exception.

I have written a validation on the account which causes an exception. Like this validation hitting one trigger.
----------------Error message on Email --------------------
Fwd: Developer script exception from SeamlessDocs : CalculateNoOfContact : CalculateNoOfContact: execution of AfterUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id 0011a00000LnAbbAAF; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, Please select Primary Consultation Call Contact for creating opportunity: [Primary_Demo_Contact__c] ()
---------------- Validation -----------------------------------
AND(
ISBLANK( Primary_Demo_Contact__c ),
NOT(ISBLANK( Demo_Date_and_Time__c ))
)
------------------This is the trigger -----------------------------------------
trigger CalculateNoOfContact on Contact (after insert,before delete,after update) {
    
    Set<Id>accid = new Set<Id>();
    List<contact>contactlist = new List<contact>();
    List<account>listacc = new List<account>();
    Map<Id,integer>mapCount = new Map<Id,integer>();
    Map<Id,integer>mapCountOld = new Map<Id,integer>();
    Map<Id,List<Contact>> currentAcc_Contact = new Map<Id,List<Contact>>();


    if(trigger.isinsert || trigger.isupdate){
        for(contact con:trigger.new){
            accid.add(con.accountId);
        }
    }
    if (trigger.isdelete){
        for(contact con:trigger.oldMap.values()){
            accid.add(con.accountId);
            if(currentAcc_Contact.containsKey(con.accountId)){
                List<Contact> currConList=currentAcc_Contact.get(con.accountId);
                currConList.add(con);
                currentAcc_Contact.put(con.accountId,currConList);
            }
            else{
                 currentAcc_Contact.put(con.accountId,new List<Contact>{con});
            }
        }
    } 
    
    if(accid.size() > 0){
         contactlist=[SELECT id,name,accountid,Account.Number_of_Contacts__c FROM contact WHERE accountid IN:accid];
    }
      System.debug('contactlist ::>'+contactlist);
    
    if(contactlist.size() >0 ){
        for(contact c:contactlist){
            if(c.Account.Number_of_Contacts__c > 0){
                    mapCountOld.put(c.accountId,c.Account.Number_of_Contacts__c.intvalue());
            }
            
            if(trigger.isinsert || trigger.isupdate ){
               if(mapCount.ContainsKey(c.accountId)){
                Integer prevCnt=mapCount.get(c.accountId);
                prevCnt++;
                mapCount.put(c.accountId,prevCnt);
                }else {
                   mapCount.put(c.accountId,1);
              }
            }
        
        }
    }
   System.debug('mapCount ::>'+mapCount);
   
   if(mapCount.size() >0 ){
      for(Id tmp:mapCount.keySet()){
          Account acc=new Account();
              acc.Id=tmp;
              acc.Number_of_Contacts__c=mapCount.get(tmp);
          listacc.add(acc);
      }   
    }
    else{
        if(trigger.isdelete && mapCountOld.size() > 0){
            for(Id tmp1:mapCountOld.keySet()){
              Account acc=new Account();
              acc.Id=tmp1;
                 System.debug('tmp1 ::>'+tmp1);
              acc.Number_of_Contacts__c= mapCountOld.get(tmp1) - currentAcc_Contact.get(tmp1).size();
                 System.debug('tmp1 ::>'+mapCountOld.get(tmp1));
                 System.debug('tmp1 ::>'+currentAcc_Contact.get(tmp1).size());
              listacc.add(acc);
            }
        }
        
    }
    
    
    System.debug('listacc ::>'+listacc);

    if(listacc.size()>0){
         update listacc;
    }
   
}



Thanks,
Sumit
AjazAjaz
The account associated with the contact which you have updated is failing the validation. It means that the account's primary contact field is blank but the contact date has been filled. Please check the account whose contact you have tried to update