You need to sign in to do that
Don't have an account?
Sourav P
To Count the records remaining/not used for a particular within the object
Hi,
I have a custom object, with one checkbox field as "Record Used". I want to know that if the remaining Record Used with checkbox not ticked ( false) become less than or equal to 100, it should send us the email alert, so that we can add more records to be used .
I am trying to create a formula field for this but i think its not going to work, can anyone plz suggest me a solution for this ? its within the same object and there is no cross object involved.Thnx
,
I have a custom object, with one checkbox field as "Record Used". I want to know that if the remaining Record Used with checkbox not ticked ( false) become less than or equal to 100, it should send us the email alert, so that we can add more records to be used .
I am trying to create a formula field for this but i think its not going to work, can anyone plz suggest me a solution for this ? its within the same object and there is no cross object involved.Thnx
,
Try this (changed Test.stopTest position)
All Answers
trigger ObjTrigger on Obj__c (after insert, after update)
{
Integer intCnt = [SELECT COUNT()
FROM Obj__c
WHERE Not_Used_c = FALSE];
if(intCnt <= 100)
{
// send email logic here
}
}
something like this can do.
Everytime you insert a record with not used, you will recieve email until count reaches 101
Thanks
MS
Below code can be usefull in update case.
i was trying to use the above code, But its showing some issue , can you plz check and suggest where i went wrong ?
Also, I need to do the below , which i am not able to accommodate in the right place, thanks
1. I want to use four emails to be send.
2. The emails to be sent everyday till the remaining not used records are 500 or less
But on insert case, if you insert 5 records one after other then will send email....
Better go for schedule apex.. Scheduling it every day midnight say 11.45 ....So per day only once email will send
I tried to write a schedule apex, Can you plz check if its OK ? How to schedule the time , Is that i have to write within the code or it will happen automatically by the midnight everyday ?
- Your code looks fine to me. Please don't forget to comment out / delete trigger since the approach is now works from scheduled apex.
- Scheduling the class end of the day some where around 11.55 or something makes sense because you are collectively querying the records when day is almost ends and sending one email based on criteria which means next day morning the email receiver can take action accordingly.
I apologize if you feel I am going out of context.
Below link has explanation for writing test class related to Scheduled apex:
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_scheduler.htm
For the above, in line
lstObj.add(new Obj__c(Name = 'Test'+i, Record_Used__c = FALSE));
Its showing ,
Error: Compile Error: Field is not writeable: Obj__c.Name at line 15 column 13
The Name type is an auto-number. We need to change somthing here ?
lstObj.add(new Obj__c(Record_Used__c = FALSE));
Is that we need to take the 2022 below ? or its OK.?
// Verify the next time the job will run
System.assertEquals('2022-09-03 00:00:00',
String.valueOf(ct.NextFireTime));
If this status is OK, or if you need some more info plz ?
This is another way we can write scheduled apex test class.
http://www.infallibletechie.com/2013/12/test-class-for-schedulable-class-in.html
1. if(cnt <= 500)
has to be changed to
2. if(cnt >0 && cnt <= 500)
Because in #1, even if there is no record with No_Used = false (0 count), email will be sent. Ideally, if atleast one record is there then email should be sent I think (#2)
May i know plz why we have kept this, I dont see any use of it, if it will make any difference ? Thnx
for(Integer i=0; i<50; i++)
for(Integer i=0; i<50; i++) // just to insert 50 records in bulk (which satisfies cnt <=500 condition)
Still you have error?
Seems its failing to run the test code as below, Here , i have posted the screenshots of the errors, plz suggest, Thanks
The below is line 40, if some issue here ?
If even if you remove this line, you will get 0% I think.
Did you changed anything in the code?
Can you paste both class and test class present in your org here once?
Try this (changed Test.stopTest position)