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
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.
Tushar Shah 7Tushar Shah 7
@shruti
When creating new opportunity and saving it, I have VR being fired from account object (because either or both of the Billing city and/ billing coutry field are empty. VR on account object is pretty simple i.e. ISBLANK( BillingCity )

could you please guide how to fix triggering on account related VR on opp creation page?

Thank you
Tushar