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
aaryansriaaryansri 

Trigger for if more than 2 minuts should not allow to edit

Hi

  Trying to write a trigger for my sobject. If record created time is more than 2 minitus for modified date and time it should not allow to edit.
already i have returned a trigger code,it's not working properly and try to change the existing code it getting error . please some budy help to resolve the issue. The code is below

 trigger Hourschange on transaction__c (before update) {
for(transaction__c s :Trigger.new){
double tm;
tm=((  System.now()-  s.CreatedDate.getTime() ) *1440);
//tm=double.ValueOf(system.NOW()- s.CreatedDate.GetTime())/365;

system.debug('hi::::::::::::::'+DateTime.now());
system.debug('hi::::::::::::::'+s.LastModifiedDate );
system.debug('hi::::::::::::::'+s.CreatedDate ); 

system.debug('hi::::::::::::::'+tm);

if(tm<=1){
system.debug('hi::::::::::::::'+tm);
s.payment_discription__c=' hi';
}
else
{
system.debug('bye::::::::::::::');
s.adderror('Alredy more than 60 minitus');
}
}
}


Before i worked the codition is  
double tm;
 tm= Math.Floor(integer.valueOf( s.LastModifiedDate.getTime()  -  s.CreatedDate.getTime() ) *1440);

Best Answer chosen by aaryansri
Vi$hVi$h
Hi ,

I guess the following example code should solve your problem ..

trigger UpdateTime on CustomObject __c (before update) {
   
    for(CustomObject__c cust : trigger.new)
    {
       system.debug('>>>>>>>>>>>>'+cust.CreatedDate.getTime()+'>>>>>>'+cust.SystemModstamp.getTime());
      
       long dt = (DateTime.now().getTime()/60000)-(cust.CreatedDate.getTime()/60000);// divide by 60000 to get value in minutes
       system.debug('Minutesssss'+dt);
       if(dt>2)
       {
        cust.adderror('Too much time man!');
       }
      
    }

}

Thanks,
Vishal 

All Answers

Atul111Atul111
Hi,

You don't need to right a trigger for this. You can right a timebase workflow and on the bases of time you can change the record type. Or also you can create a validation msg.

Please let me know if you need further help on this

Thanks Atul
aaryansriaaryansri
Hi Atul

  Thanks for respone. Already the smae work done in workflow but the they specifically asking in trigger only.
 
Vi$hVi$h
Hi ,

I guess the following example code should solve your problem ..

trigger UpdateTime on CustomObject __c (before update) {
   
    for(CustomObject__c cust : trigger.new)
    {
       system.debug('>>>>>>>>>>>>'+cust.CreatedDate.getTime()+'>>>>>>'+cust.SystemModstamp.getTime());
      
       long dt = (DateTime.now().getTime()/60000)-(cust.CreatedDate.getTime()/60000);// divide by 60000 to get value in minutes
       system.debug('Minutesssss'+dt);
       if(dt>2)
       {
        cust.adderror('Too much time man!');
       }
      
    }

}

Thanks,
Vishal 
This was selected as the best answer
aaryansriaaryansri
Hi Vishal

Thank for solution.

Regards
Ashok