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
Ramesh SomalagariRamesh Somalagari 

Validation rule on Account Object

I have a validation rule on the Account Object as below:

AND( 
NOT ISBLANK( Name),
ISPICKVAL (Account_Level__c, "Company" ),
ISBLANK( Address__c ),
NOT $User.Status__c
)
The above rule working when the Account is Created/Updated.Now I am create a Trigger on the Custom Object(Company):Trigger name is CompanySurvey. In CompanySurvey Trigger I Update the one field in Account Object.At the time does't showing the ERROR in standard-lone.But Account is not Updated because is validation rule executes.I debug the CompanySurvey Trigger in ERROR logs it show the error message but not showing the standard-lone.

Company lookup with Account Object.

Best Regards,
Ramesh
Best Answer chosen by Ramesh Somalagari
NishBNishB
The validation error message is not displayed in the standard page because of the exception handling...i.e the try,catch block there is a DML exception because of the validation rule. Inorder to display the error check if the e.getMessage() contains 'field custom validation' string if it does then do a substring to get the message and do a Trigger.addError  this should display the message on the standard page. Also as a good practice avoid writing a update statement in the for loop. You can collect all the records in a list and update it after the for loop.

All Answers

NishBNishB
Hi,
     Can you post your trigger code
Ramesh SomalagariRamesh Somalagari
This is my Trigger code:
Trigger SurveySurvery on Company__c (AFTER INSERT)
{
    TRY
    {           
        FOR(Company__c sur : TRIGGER.NEW)
        {

                    Account  acc           = NEW Account();                                
                    acc.Id                 = sur.Account_Name__c;
                    acc.Last_Survey__c =  Date.valueOf(sur.CreatedDate);                     
                    UPDATE acc;  
        }
    }  
    CATCH(EXCEPTION E)
    {
        SYSTEM.DEBUG('MAIN ERROR : COMPANYSURVEY'+E);
    }
}
NishBNishB
The validation error message is not displayed in the standard page because of the exception handling...i.e the try,catch block there is a DML exception because of the validation rule. Inorder to display the error check if the e.getMessage() contains 'field custom validation' string if it does then do a substring to get the message and do a Trigger.addError  this should display the message on the standard page. Also as a good practice avoid writing a update statement in the for loop. You can collect all the records in a list and update it after the for loop.
This was selected as the best answer
Ramesh SomalagariRamesh Somalagari
Yes ,Your are abouslty correct.I have removed the try catch block it throwing exception.