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
Sourav PSourav P 

Visualflows issue while checking the number of remaining record numbers under certain criteria within the same custom 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) less than or equal to 500, it should send us the alert, so that we can add more records to be used .
I tried to create it through VisualFlows, and later called it through the PB, But It seems i am not able to create a proper flows, can anyone plz modify me, where i need to add elements. I as well need to send email to specific people if it reached 500.
Below is my VF screenshots,

I used ,

1. Fast lookup element - To fetch all object Records
2. Loop element - To loop through all object records
3. Assignment element - To assign the potential value to a variable and find the count.
4. Record Update element - To update all object records with the total count.

1.
User-added image
2.
User-added image
3.
User-added image
4.
User-added image
5.
User-added image
Best Answer chosen by Sourav P
Sourav PSourav P
Hi Nagendra , I used the scheduled apex as below, Is the below is correct plz ? Also, how to set the timing for a schedule apex class to run ?
How can i test now ? Thnx
 
global class ObjScheduledClass Implements Schedulable
{
global void execute(SchedulableContext sc)
{
integer cnt=[SELECT COUNT() FROM Obj__c WHERE Record_Used__c = FALSE];
If(cnt<=500)
{
sendmail();
}
}
public void sendmail()
{
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();  
string body = 'Hi ';
String[] toAddresses = new String[] {'sourav@xxxx.com','sn@xxxx.com'}; 
mail.setToAddresses(toAddresses);
mail.setSubject('Remaining Record is less than 500');  
mail.setSaveAsActivity(false);  
for(OrgWideEmailAddress owa : [select id, Address, DisplayName from OrgWideEmailAddress]) 
{
 if(owa.Address.contains('service')) 
  mail.setOrgWideEmailAddressId(owa.id); 
}
mail.setHtmlBody('The remaining record is less than 500, Take action' );  
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
}

 

All Answers

NagendraNagendra (Salesforce Developers) 
Hi Sourav,

You can not achieve it through Flow or process builder. You will have to go with custom code solution. 
  • For each time record update either true or false, you will have to increase or decrease the counter in Total count. Total count must be stored somewhere either in custom setting, to which each time you can update. And once if you find that counter reached to 500, send email alert through Code. 
Hope this helps.

Thanks,
Nagendra
Sourav PSourav P
Hi Nagendra , Thanks
I tried the below code, But what i need is the email should send once , every day till the count is 500 or less. But i think what i have wrote is , whenever teh field " Not used" is changed it will send emails. So, Is that a way in teh beelow code that i can make it send once per day?
 
trigger ObjTrigger on Obj__c(after insert, after update)
{
    Boolean blnChanged = false;
    for(Obj__c objC : Trigger.New)
    {
        if(objC.Not__Used__c = FALSE && (Trigger.isInsert || (Trigger.isUpdate && objC.Not__Used__c != Trigger.oldMap.get(objC.Id).Not__Used__c)))
        {
            blnChanged = true;
            break;
        }
    }

    if(blnChanged)
    {
Integer intCnt = [SELECT COUNT() FROM Obj__c WHERE Not__Used__c = FALSE];
if(intCnt <= 500)
{
// send email logic 
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();  
string body = 'Hi ';
String[] toAddresses = new String[] {'sourav@xxxx.com', 'abc@xxxx.com', 'xyz@xxxx.com'}; 
mail.setToAddresses(toAddresses);
mail.setSubject('Remaining Compulsory Barcode is less than 500');  
mail.setSaveAsActivity(false);  
for(OrgWideEmailAddress owa : [select id, Address, DisplayName from OrgWideEmailAddress]) 
{
 if(owa.Address.contains('service')) 
  mail.setOrgWideEmailAddressId(owa.id); 
}
mail.setHtmlBody('The remaining not used records are less than 500' );  // This will be my email body , its correct ?
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
}
}

 
Sourav PSourav P
Hi Nagendra , I used the scheduled apex as below, Is the below is correct plz ? Also, how to set the timing for a schedule apex class to run ?
How can i test now ? Thnx
 
global class ObjScheduledClass Implements Schedulable
{
global void execute(SchedulableContext sc)
{
integer cnt=[SELECT COUNT() FROM Obj__c WHERE Record_Used__c = FALSE];
If(cnt<=500)
{
sendmail();
}
}
public void sendmail()
{
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();  
string body = 'Hi ';
String[] toAddresses = new String[] {'sourav@xxxx.com','sn@xxxx.com'}; 
mail.setToAddresses(toAddresses);
mail.setSubject('Remaining Record is less than 500');  
mail.setSaveAsActivity(false);  
for(OrgWideEmailAddress owa : [select id, Address, DisplayName from OrgWideEmailAddress]) 
{
 if(owa.Address.contains('service')) 
  mail.setOrgWideEmailAddressId(owa.id); 
}
mail.setHtmlBody('The remaining record is less than 500, Take action' );  
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
}

 
This was selected as the best answer