You need to sign in to do that
Don't have an account?
Getting Error while inserting record but i want to achieve the insert and update both
trigger TriggerOnEstimate on Estimate__c (before insert,before update) {
if(Trigger.isInsert)
{
Date d = Date.today();
for ( Estimate__c e : Trigger.new)
{
List<Estimate__c> est = [select Most_Recent_Estimate__c,Estimate_Name__c from Estimate__c ];
System.debug('******All Estimates****'+est+'***Size***'+est.size());
for (Integer i =0 ;i<est.size() ; i++)
{
est[i].Most_Recent_Estimate__c = 'FALSE';
update est[i];
}
// update est;
e.Most_Recent_Estimate__c = 'TRUE';
}
}
for(Estimate__c e: Trigger.new)
{
if(Trigger.isUpdate && e.CreatedDate == Date.today())
{
}
if(Trigger.isUpdate && e.CreatedDate < Date.today() )
{
e.addError('***********Donot edit the estimate***********');
}
}
}
1st of all, never query inside a for loop.
Yes, you shouldn't be querying inside a for loop.
Also, what I see is you want to set the Latest Inserted Estimate as Most_Recent_Estimate. So at a time, only one Estimate will be Most_Recent_Estimate ideally.
So for update, what business goal do you have? On every modification, that record should become the Most_Recent_Estimate?