+ Start a Discussion
DasGDasG 

Hi friends! without validation rule, I wanna date Error Message on Account Obj.

ex: am trying like this but it won't  workout. then how????
trigger acctrig on Account (before insert,after update) {
Account acc =new Account();
          if(acc.SLAExpirationDate__c != System.today())
             acc.addError('Error! enter only today date');
}
or
trigger acctrig on Account (before insert,after update) {
Account acc =new Account();
acc.today__c = date.today();
if(acc.SLAExpirationDate__c !=acc.today__c)
acc.addError('Error! enter only today date');
}

so pls share anather better sample code for this.

Best Answer chosen by Admin (Salesforce Developers) 
PremanathPremanath

Hi Das,

 

Your asking about for loop as you know this loop for new trigger memory allocation.

It takes the values from current record and Existing records.

 

Account a=new Account();

 

It is used for Appending or Adding records to that Object.And Take the values from existing records.

 

So our critiria reaches using for loop way..

 

 

 

Ok if you satisfied with above answer please make it as Soluble ..  For Other's it may benifit.

 

 

Regards

Prem

All Answers

PremanathPremanath

Hi DasG,

Follow this code it's working now..

 

trigger acctrig on Account (after insert,after update) {
for(Account acc :trigger.new){
Datetime cDT = Date.Today();
System.Debug('reocrd date=====>'+acc.Today__c);
System.Debug('----->'+cDT);
if(acc.Today__c!=cDT){
acc.addError('Error! enter only today date');
System.Debug('------>inner loop------------------->');
}
else{
System.Debug('----------->Outerloop-->');
}
}
}

HariDineshHariDinesh

Hi DasG,

 

Here you are creation the instance of account and trying to check that data and you are not validation the account which causes the trigger that is the problem with your code. So please update your code with code given by “Premanath” which will work for you.

DasGDasG
hi premanath thanks for ur support. my criteria is satisfied. but why u used for loop or like SFDC for loop : for(Account acc :trigger.new) is it neccessary loop for new trigger memory allocation. see my code like without for loop. any way ur code is quite good. but am know abt forloop why u used like that tell that purpose. my code: trigger acctrig on Account (before insert,after update) { Account acc=trigger.new[0]; acc.today__c = date.today(); if(acc.SLAExpirationDate__c != acc.today__c) acc.addError('Error! enter only today date'); } or trigger acctrig on Account (before insert,after update) { Account acc=trigger.new[0]; Datetime cDT = Date.today(); if(acc.today__c!=cDT){ acc.addError('Error! enter only today date'); } -- Bhagavan Das
PremanathPremanath

Hi Das,

 

Your asking about for loop as you know this loop for new trigger memory allocation.

It takes the values from current record and Existing records.

 

Account a=new Account();

 

It is used for Appending or Adding records to that Object.And Take the values from existing records.

 

So our critiria reaches using for loop way..

 

 

 

Ok if you satisfied with above answer please make it as Soluble ..  For Other's it may benifit.

 

 

Regards

Prem

This was selected as the best answer