+ Start a Discussion
GMASJGMASJ 

Account Validation fires when opportunity is create/update

Hi,

  Added below validation on a custom field in account not sure why this is getting fired when opportunity is created or updated. Please suggest me how to make this validation to fire only on a account. 

 NOT( REGEX( Email_Domain__c , "[a-zA-Z0-9.']+"))

Thanks
Sudhir
Best Answer chosen by GMASJ
Shruti SShruti S
We have ISCHANGED method in Saleforce which would return a TRUE if a particular field used on the ISCHANGED function has changed. Hence you can modify your Validation Rule as below -
NOT(REGEX(Email_Domain__c, "[a-zA-Z0-9.']+")) && ISCHANGED(Email_Domain__c)
This validation rule would only fire if the Email_Domain__c was changed and changed to an unacceptable value. Now the Validation Rule will not fire as the trigger is trying to update the Support_Email__c  field and not the Email_Domain__c field on Account.

Feel free to get back to me if you have any doubts.

All Answers

Shruti SShruti S
Please check if there is any Process, Wokflow Rule or a Trigger which updates some field on the Account records and gets fired on Create or Update of Opportunity. Or it could be even that there is a Roll up summary field on the Account which depends on the Opportunity.
GMASJGMASJ

Thanks Shruti for reply. If there is a cross object update this is the issue on validation getting fired. How to resolve this do we you have any suggestion. 

Thanks

Sudhir

Shruti SShruti S
Please let me know what the cross object update is doing.
GMASJGMASJ
Below is a trigger updating the account field when opportunity is updated. 
 
trigger update_account_contact_email  on Opportunity (before insert, before update) {

   if(accountcontactemail_checkRecursive.runOnce()){

    Set<Id> accountIds = new Set<Id>();
    for(Opportunity opp:System.Trigger.new){ 
        accountIds.add(opp.AccountId);
    }

    Map<Id,Account> accountMap = new Map<Id,Account>([Select Support_Email__c From Account Where Id in :accountIds And (Support_Email__c = null Or Support_Email__c = '')]);
   

    Map<Id,Account> accountsToUpdate = new Map<Id,Account>();
    for(Opportunity opp : Trigger.new){
        Account acct = accountMap.get(opp.AccountId);
        if(acct != null){
           
            acct.Support_Email__c = opp.Customer_Support_Contact_Email__c;
            accountsToUpdate.put(acct.Id,acct);
        }
    }

    update accountsToUpdate.values();
    
  }  
}

Thanks
Sudhir​
Shruti SShruti S
We have ISCHANGED method in Saleforce which would return a TRUE if a particular field used on the ISCHANGED function has changed. Hence you can modify your Validation Rule as below -
NOT(REGEX(Email_Domain__c, "[a-zA-Z0-9.']+")) && ISCHANGED(Email_Domain__c)
This validation rule would only fire if the Email_Domain__c was changed and changed to an unacceptable value. Now the Validation Rule will not fire as the trigger is trying to update the Support_Email__c  field and not the Email_Domain__c field on Account.

Feel free to get back to me if you have any doubts.
This was selected as the best answer
GMASJGMASJ
Thanks Shruti this is helpfull just want know other than ISCHANGED do you have any other suggestion. 
Shruti SShruti S
Sorry, this is the best solution I could think of.