1

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 have created 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 have debug the CompanySurvey Trigger in ERROR logs it show the error message but not showing the standard-lone.Some body please help me.

Company lookup with Account Object.

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);
    }
}
1
  • 1
    Can you provide us with trigger code? Because I guess it cause the problem Commented Mar 3, 2014 at 8:31

1 Answer 1

2

First of all, the trigger needs to be bulkified:

Trigger SurveySurvery on Company__c (AFTER INSERT) {
Map<ID,Account> aIdToAccountMap = new Map<ID,Account>();  // handle case where multiple Company__c have same Account parent


for (Company__c sur: Trigger.new)
   if (!aIdToAccountMap.containsKey(sur.account_name__c)) // not best practice for naming a lookup field - should be account__c
       aIdToAccountMap.put(sur.account_name__c,new Account(id = sur.account_name__c,  
                                                           Last_Survey__c = Date.valueOf(sur.CreatedDate)
                            ));
  else {  // 2nd, 3rd, ... nth Company for same account, use greatest survey date
    Account aWork = aIdToAccountMap .get(sur.account_name__c);
     if (aWork.Last_Survey__c  < Date.valueOf(sur.CreatedDate)) {
         aWork.Last_Survey__c  = Date.valueOf(sur.CreatedDate);
         aIdToAccountMap.put(aWork.id,aWork);
     } 
  }


try {update aIdToAccountMap.values();}
catch (DmlException e) {// do something to force transaction rollback, log error, report error, etc.}
}

As for the validation rule, it will be true if all the sub conditions are true:

  • Account.name is not blank (will always be true as this is a required field in SFDC)
  • Account.Account_level__c is 'Company'
  • Account.address__c is blank/null
  • Running user Status__c is false (presumably this is a check box on User - again, best practice would be to name this field in the form of an assertion 'Is xxx?' as status is not a good name for a true/false value)

So, based on your test, depending on which Company__c is being updated, its parent Account may satisfy all of the conditions above. You can easily see this in the debug logs if you monitor workflow events

2
  • I have removed TRY CATCH block now it throwing the exceptions Commented Mar 15, 2014 at 20:40
  • Ramesh - is this a question or a confirmation of desired results?
    – cropredy
    Commented Mar 16, 2014 at 18:01

You must log in to answer this question.

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