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
NewToSalesforce2012NewToSalesforce2012 

Having Flow understand and read info from an Apex Schedulable Class

I have built an Apex Schedulable Class that sends an email to our Sales director when an account has an account review date 14 days away.

 

In the email, their needs to be a link to a flow that starts the calculation for a commision process.

 

How do I make the flow understand that it needs to the pull information from the Account that has the account review date in 14 days? Currently, I can click on the link in the email, but the flow doesn't know where to pull the account information from and I get an error message after the first screen.

 

My guess would be to populate a variable with all the account information and set that equal to the variable in flow that does the account lookup.

 

However, it is my understanding that variables in flow only exist in flow and variables in Apex only exist in Apex.

 

How do I sync my Apex class in flow?

RajaramRajaram

you need to do the following

1. Create a variable for the accountID (say vaAccountID) and make sure its set to Input only or input/output

2. Configure the flow to make sure the flow will handle the account ID not being set. I would have a decision element which checks for this value and have a screen which says, you need to have an account ID if its not set - this way, the flow will need the account id for it work properly

3. Set the variable in the link to the email. You can do this similar to setting any url values such as <blah>/flow/commisionflow?vaAccountID={!ID}

 

Having said this, just curious on why you are using the scheduled apex for this and not time-based workflow? Seems like you can set this up easily with TBWF and also set the email template and the URL easily without any code!

AngelikaAngelika

Hi Rajaram!

 

Thank you for your help. 

 

I have a few questions:

 

1. When you say to create a variable for the accountID, do you mean in flow or in my Apex Schedulable class?

 

In flow, the first screen performs an account lookup (Lookup Account, Field: ID equals {!vaAccountID}. {!vaAccountID} is a variable a predesor created in flow. The data type for vaAccountID is text and the Input/Output type is Input and Output.

 

2. How does the flow know that it should pull information from the Account with the Next_Account_Review_Date__c 2 weeks away?

 

Here is my code: 

 

 

global class AccountEmail implements Schedulable{
global void execute (SchedulableContext ctx)
{

sendAccountEmail();

}
public void sendAccountEmail()
{

 

  OrgWideEmailAddress Orgid = [Select Id,displayname from OrgWideEmailAddress]; 

 

for(Account acc : [SELECT Id, Commission.name FROM Account WHERE Next_Account_Review_Date__c = : system.Today().addDays(14)])

{

// making the account variable to the variable in flow doesn't work because you can't access variables outside of flow by default

 

//acc.id = {!vaAccountID}

Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

String[] toAddresses = new String[] {'company@company.com'};

mail.setToAddresses(toAddresses);

mail.setOrgWideEmailAddressId(Orgid.Id);

 

mail.setsubject('Account Reminder');
mail.setHtmlBody('This is a scheduler-generated email to notify you that the Account Review Date of Name: <b> ' + acc.name + ' </b> is in two weeks. Please click the link to calculate commission: https://cs10.salesforce.com/flow/Organic_Commission_Determination_Flow?OldCommissionID=&vaAccountID=');
mail.setSaveAsActivity(false);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail });
}
}

 

 

}

 

I am not using TBWF because this action needs to be repeated every year until the Account funding is depleated. Time Based Work Flows are no good for continually re-curring actions. They will trigger once after being implemented. See http://success.salesforce.com/questionDetail?qId=a1X30000000JnPMEA0 and http://success.salesforce.com/questionDetail?qId=a1X30000000HvOQEA0 . 

 

Thanks again!

AngelikaAngelika

Hi Rajaram, 

 

Let me clarify my problem. I have asked our Salesforce Consultants as well as those in the Cincinnati Salesforce User Group about this issue. Many people in the Cincinnati Salesforce group are running into this issue, as well as those in the Idea Exchange. I want to make sure that my Commission process is able to be implemented with an Apex Scheduler Class and Visual Flow, as of the Summer '12 release of Salesforce. 

 

As Director of Product Management for Visual Workflow, I am hoping you could share insight on our problem. 

 

It is my understanding that TBWF only trigger once. See http://success.salesforce.com/questionDetail?qId=a1X30000000JnPMEA0 , http://success.salesforce.com/questionDetail?qId=a1X30000000HvOQEA0 and more formal documentation: http://na10.salesforce.com/help/doc/en/workflow_time_action_considerations.htm.

 

I have Workflow rules in place that send an email to our Sales Director two weeks before an account review date for an account and triggers a flow which calculates the commission. This process needs to repeat EVERY YEAR indefinitely until the account is dead.

 

In addition, there are two flows to calculate commission based on how much an account has grown.

 

As a work around, I have built an Apex Scheduler class that sends an email and link to the flow that processes the commission to the Sales Director, two weeks before the Account Review Date. The email tells them there is an Account with an Account Review Date Two Weeks away.

 

However, when you click on the link to flow, you are able to get to the first screen and then the flow terminates. There is no link between flow and the scheduler. Flow doesn't know what account to lookup and grab information from and terminates. Without TBWF, there is no link between flow and where to pull the account information from. Is there a way to link flow and my Apex Scheduled Class so the flow knows what account to pull the information from?

 

The flow uses Lookup Account [Field] Id equals{!vaAccountID} which is a variable in flow populated with account information.

 

In theory, I wanted to make the Account ID from the Account With the Next Account Review Date equal to {!vaAccountID}  in flow and then the flow could complete the account lookup. However, it is my understanding that variables in flow can't exist outside of flow. 

 

There also needs to be a way for the scheduled apex class to determine which flow to use to calculate commission based on account growth. Is this possible as of Summer ’12?

 

Below is the code I have written with comments. I can provide clarification and/or screenshots of flows if needed.

 

Thank you for your time and attention to this matter.

 

May the force be with you,

 

Angelika (Same user as NewToSalesforce2012, my DEV account)

 

//implements schedulable interface

 

global class AccountEmail implements Schedulable{ 


global void execute (SchedulableContext ctx) 

 

// sends email

sendAccountEmail(); 

}
public void sendAccountEmail()

{
// allows the from address to be an organizational wide email address


 OrgWideEmailAddress Orgid = [Select Id,displayname from OrgWideEmailAddress]; 

// SOQL Query that grabs the Account ID and commission name when the Next Account Review Date is 14 days away.


    for(Account acc : [SELECT Id, Commission_Name__c FROM Account WHERE Next_Account_Review_Date__c = : system.Today().addDays(14)])
 
  {
  

// Sets up the email to be sent


Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); 

 

// sets up to address


String[] toAddresses = new String[] {'jrampello@projetech.com'}; 

mail.setToAddresses(toAddresses);

 

// sets up from address to be an organizational email address


 mail.setOrgWideEmailAddressId(Orgid.Id);  

 

// In theory, this piece of code would set the Account ID of the Account that has a Next_Account_Review_Date to the {!vaAccountID} variable in flow, prompting a successful account lookup. However, variables created in flow don’t exist outside of flow, so I get the Apex error Variable not found


//acc.id = vaAccountID;

// sets email subject


mail.setsubject('Account Reminder');

 

// set HTML body with commission name and flow link. Flow link does not populate with account info.


mail.setHtmlBody('This is a scheduler-generated email to notify you that the Account Review Date of Name:  ' + acc.Commission_Name__c + '  is in two weeks. Please click the link to calculate commission: https://cs10.salesforce.com/flow/Organic_Commission_Determination_Flow?OldCommissionID=&vaAccountID=');


mail.setSaveAsActivity(false);


Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail }); 
}
}


}

 

 

RajaramRajaram

Yes.. TBWF is not periodic, so  you have to go the scheduled class route yes.

From your code it looks like are indeed doing the right thing where the body of the email is

http://blah/flow/myFlow?vaAccountID=acc.id

 

This should pass the acc.id value to vaAccountID. 

 

Are you seeing this set in the URL of the email?

You can test this manually by setting the URL value in the app. Did that work?

 

AngelikaAngelika

Hi Rajaram,

 

Manually changing the flow link did not work.

 

I changed it to:

 

https://cs10.salesforce.com/flow/Organic_Commission_Determination_Flow?OldCommissionID=&vaAccountID=acc.id

 

Before it was:

https://cs10.salesforce.com/flow/Organic_Commission_Determination_Flow?OldCommissionID=&vaAccountID=

 

I got this page instead of the flow. I am a sys Admin. 

An unhandled fault has occurred in this flow
An unhandled fault has occurred while processing the flow. Please contact your system administrator for more information. 

 

I got this email:

 

Subject: Sandbox: Unhandled process fault from Projetech : Organic_Commission_Determination_Flow : interaction.dal.exception.DALExecutionFault: ; nested exception is: common.exception.ApiQueryException: Account.Annual_RR__c FROM Account WHERE (Account.Id = '...

 

Sandbox

 

Encountered unhandled fault when running process Organic_Commission_Determination_Flow/301J000000001Tx exception by user/organization: 00DJ00000000YTl/{4} Source organization: 00DA0000000KZI8 (null)

interaction.dal.exception.DALExecutionFault: ; nested exception is:

                common.exception.ApiQueryException:

Account.Annual_RR__c FROM Account WHERE (Account.Id = 'acc.id') LIMIT 1

                                         ^ ERROR at Row:1:Column:89 invalid ID field: acc.id (There was a problem executing your command.) > RETRIEVE

 

caused by element : Data lookup.Lookup_Account

 

caused by: interaction.dal.exception.DALExecutionFault: ; nested exception is:

                common.exception.ApiQueryException:

Account.Annual_RR__c FROM Account WHERE (Account.Id = 'acc.id') LIMIT 1

                                         ^ ERROR at Row:1:Column:89 invalid ID field: acc.id (There was a problem executing your command.) > RETRIEVE

 

Salesforce Error ID: 317095012-39494 (-2117906407)

 

It's saying my ID field is Invalid.

 

On a much more important note, I wasn't clear about my second issue with flow. Depending on where we are in the commission process and what type of commission we are processing, there are two different flow that can run.

 

If we are processing an initial commission, We have a flow that runs to create it into an Organic Growth Commission.

If we are processing an Organic Growth Commission for Recurring Revenue Accounts, there is different flow. If the account has grown more than 5% in a year, the rep will get receive an additional 10% of the increase in account growth. This repeats indefinitely until an account is dead. 

 

is there a way for an Apex Schedulable class to decide which flow to email based on criteria?

 

 

Thank you for your time and help!

 

 

 

 

RajaramRajaram

The URL is incorrect

It should be in yoru apex class

 

https://cs10.salesforce.com/flow/Organic_Commission_Determination_Flow?vaAccountID=acc.id

 

 

Not sure what the OldCOmmissionID is supposed to bem, but  you are not setting it or its null.

 

NewToSalesforce2012NewToSalesforce2012

Rajaram,

 

Thank you for your help. I think I have what I need to fix the URL.

 

I have another problem, mentioned in the previous post. Depending on where we are in the commission process and what type of commission we are processing, there are two different flows that can run.

 

If we are processing an initial commission, We have a flow that runs to create it into an Organic Growth Commission.

 

If we are processing an Organic Growth Commission for Recurring Revenue Accounts, there is a different flow. If the account has grown more than 5% in a year, the rep will receive an additional 10% of the increase in account growth. This repeats indefinitely until an account is dead. 

 

is there a way for an Apex Schedulable class to decide which flow to email based on criteria?

 

I apreciate your help so much!

 

Angelika

 

RajaramRajaram

If you already have 2 flows, then yes, you will need to put the logic in Apex and send the appropriate flow link in the email.

If the logic is simple, you could just have 1 flow and pass in the values and let the flow do the logic of deterniming which path to take. In this approach, you will have 1 flow and branch on separate paths where you can call another flow as a subflow.

 

Hope this helps.

Raja