• Angelika
  • NEWBIE
  • 0 Points
  • Member since 2012

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 17
    Questions
  • 15
    Replies

Hi, I have created an Apex Scheduler Class that sends an email 14 days before an account review date. It's working fine in production, but I don't have enough code coverage. Salesforce seems to think that I am not calling the sendAccountEmail method.....when I am!

 

Here is the 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__c FROM Account WHERE Next_Account_Review_Date__c = : system.Today().addDays(14)])
 
  {
  
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); 

String[] toAddresses = new String[] {'amodawal@projetech.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:  ' + 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&vaAccountID=acc.id');
mail.setSaveAsActivity(false);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail }); 
}
}

static testMethod void myCommission() 
{

    //create the required test data needed for the test scenario
   
 //creating Account
    Account testAccount = new Account(name='DGC financial test');
    insert testAccount;
    
    
//giving the account an account review date 14 days away 
    testAccount.Original_Account_Review_Date__c=system.Today().addDays(14);
   
     AccountEmail hw = new AccountEmail ();
     hw.sendAccountEmail();
}
}

I am new to Apex Coding. Besides an unmanaged Clone List App and many Managed Apps , my Salesforce Org does not have any custom Apex Code in it.

 

I tried to Deploy an Apex Class to production and got the error on a test class, Failure Message: "System.DmlException: Insert failed. First exception on row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, </BR></BR></BR><font color="blue">Redirecting in just a second.</font></BR><SCRIPT LANGUAGE="JavaScript">window.location = "../apex/SFSSDupeCatcher__SFSSDupeCatcher?dId0=MDAxRjAwMDAwMG5TWHg4SUFH&scId=YT...

 

I know how to fix this error (change validation rules on my Dupe Catcher App Temporarily.

 

This "quick fix" works for deploying my code, but what about when my Apex Code stays in production.

 

If new validation rules, either from DupeCatcher or for Contacts, Accounts etc are put into place after my code in deployed in production , this will break my test data. What are the implications of test data/classes/methods breaking in production? I know I won't be able to deploy new code, but is there anything else that happens, particulary to the code that the test method/class/data was written for? Will test methods that are broken break the classes they cover?

 

If you were just begining Apex Development in your organization, what would you do differently? How can Apex Code break in production? How can I prevent this?

 

Thanks,

 

Angelika

 

 

 

I have written a test method for an Apex Birthday Scheduler Class. The class works great in Sandbox, sending an email to the CEO 2 days before an employees birthday.

 

However, I can't transfer this class to production because my class has 0 test coverage despite writting a test method that creates an account and contact that satisfies my class's query. Am I missing something here? Is there a way to "cheat" and get coverage?

 

I've spent hours on this to no avail. 

 

Here is my code and test method (red indicates lines not covered in Salesforce:

 

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

sendBirthdayEmail();

}
public void sendBirthdayEmail()
{

for(Contact con : [SELECT name, Id, Birthdate FROM Contact WHERE Next_Birthday__c = : system.Today().addDays(2) AND Account.Name = 'Company Inc.'])

{
String conId = con.Id;
String conName = con.name;
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
//mail.setTemplateId('00XJ0000000M31w');

mail.setTargetObjectId('005J0000000JWYx');
// mail.setToAddresses('email@company.com');
mail.setsubject('Company Birthday Reminder');
mail.setHtmlBody('This is a scheduler-generated email to notify you that the birthday of Name: <b> ' + con.name + ' </b> is in two days. Birthdate of: ' + con.Birthdate + '. Please wish them a happy Birthday.');
mail.setSaveAsActivity(false);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail });
}

}

 

// test method

static testMethod void myTestBirthday() {

//create the required test data needed for the test scenario
//In this case, I need to create a new contact, with the account name of Company and has a birthday 2 days away
Account testAccount = new Account(name='Company Inc.');
insert testAccount;
Contact testContact = new Contact();
testContact.firstName='Jack';
testContact.lastName='Dell';
insert testContact;

testContact.birthdate=system.Today().addDays(2);

update testContact;

testAccount.name = testContact.id;

//verify that the birthdate field was updated in the database

//Contact updatedContact = [SELECT birthdate FROM Contact WHERE Id =:testContact.Id];
//System.assertEquals(system.Today().addDays(2) , updatedContact.birthdate);

 

//Contact nextUpdatedContact = [SELECT Account.name FROM Contact WHERE Id =:testContact.Id];
//System.assertEquals(system.Today().addDays(2) , nextupdatedContact.birthdate);

for(Contact con : [SELECT name, Id, Birthdate FROM Contact WHERE Next_Birthday__c = : system.Today().addDays(2) AND Account.Name = 'Company Inc.'])
{
String conId = con.Id;
String conName = con.name;
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
//mail.setTemplateId('00XJ0000000M31w');

mail.setTargetObjectId('005J0000000JWYx');
// mail.setToAddresses('company@company.com');
mail.setsubject('Company Birthday Reminder');
mail.setHtmlBody('This is a scheduler-generated email to notify you that the birthday of Name: <b> ' + con.name + ' </b> is in two days. Birthdate of: ' + con.Birthdate + '. Please wish them a happy Birthday.');
mail.setSaveAsActivity(false);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail });
}
}

 

}

I have created a an Apex Birthday Scheduler that sends an email to a manager when a employees birthday is two days away. I have run tests in sandbox and my code works. 

 

I am currently building a test method. I get the error,  Error: Compile Error: Expression cannot be assigned at line -1 column -1 when I try to save my code.

 

This confuses me because my code ran before I added the test method. Why is the compiler saying there is an error at line -1 column -1...before my code? How is that even possible? 

 

Here is my code With test method:

 

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

sendBirthdayEmail(); 

}
public void sendBirthdayEmail()
{

   for(Contact con : [SELECT name, Id, Birthdate FROM Contact WHERE Next_Birthday__c = : system.Today().addDays(2) AND Account.Name = 'Projetech Inc.'])
 
  {
  String conId = con.Id;
  String conName = con.name;
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); 
//mail.setTemplateId('00XJ0000000M31w'); 

mail.setTargetObjectId('005J0000000JWYx');
// mail.setToAddresses('');
mail.setsubject('Birthday Reminder');
mail.setHtmlBody('This is a scheduler-generated email to notify you that the birthday of Name:  <b>  ' + con.name + '  </b> is in two days. Birthdate of: ' + con.Birthdate + '. Please wish them a happy Birthday.');
mail.setSaveAsActivity(false);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail }); 
}

}

static testMethod void myTestBirthday() {

//create the required test data needed for the test scenario
//In this case, I need to create a new contact, with the account name of Projetech and has a birthday 2 days away

Contact testContact = new Contact();
testContact.firstName='Jack';
testContact.lastName='Dell';
insert testContact;

testContact.birthdate=system.Today().addDays(2);

update testContact;

//verify that the birthdate field was updated in the database

Contact updatedContact = [SELECT  birthdate FROM Contact WHERE Id =:testContact.Id];
System.assertEquals(system.Today().addDays(2) , updatedContact.birthdate);

// do the same for account name

Account.name='Tests.';
update testContact;

Contact nextUpdatedContact = [SELECT Account.name FROM Contact WHERE Id =:testContact.Id];
System.assertEquals(system.Today().addDays(2) , nextupdatedContact.birthdate);

}



}

 

 

 

 

I have made an Apex Scheduler Class for Birthdays. The class works and successfully sends an email to our director when a birthday is 2 days away.

 

However, the email template that is sent when there is a birthday two days away needs to contain the contact's first, last name and birthdate. (The email template looks like this: This is a scheduler-generated email to notify you that the birthday of First Name: {!Contact.FirstName} Last Name: {!Contact.LastName} is on {!Contact.Next_Birthday__c}.

 

In my scheduler, I use a SOQl statement to query the database for the contact's id and first name. 

 

After I call the query, how do I convert the information I just queried to a string/method to be used in the email template?

 

My guess is return the results of the query as a string? Any Advice is appreciated!

 

Here is my code:

 

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


sendBirthdayEmail(); 

}
public void sendBirthdayEmail()
{

   for(Contact con : [SELECT Contact.name, Id FROM Contact WHERE Next_Birthday__c = : system.Today().addDays(2)])
 
  {
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); 
mail.setTemplateId('00XJ0000000M31w'); 
mail.setTargetObjectId('005J0000000JWYx'); 
mail.setSaveAsActivity(false);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail }); 
}
}


}

I have written an Apex Scheduler class to send an email when a colleagues Birthday is 2 days away. I have created a contact with a birthday 2 days away. The contact's birthday is the July 29, 2012. Today's date is July 27, 2012. 

 

I'm stuck. I don't get an error message or anything. I have scheduled the class to run today at 12.  Please help!

 

 

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

sendBirthdayEmail();

}
public void sendBirthdayEmail()
{

for(Contact con : [SELECT Name FROM Contact WHERE Next_Birthday__c = : system.Today().addDays(2)])

{
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setTemplateId('00XJ0000000M31w');
mail.setTargetObjectId('005J0000000');
mail.setSaveAsActivity(false);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail });
}
}


}

I am new to apex and have built an apex schedule class that runs everyday. If an account has an account review date for commissions is two weeks (14 days) away the scheduler will send an email to our Sales Department. The email contains a link to a flow. The flow starts a process to calculate the new commission. To do that, the flow needs to understand to pull information from the account that triggered the email to be sent, the account that has an account review date in 2weeks.

 

Currently, the email sends out fine with the link to the flow. The link to the flow works, but once you get to the flow, the flow does not know/understand what account it should be pulling information from. I’m not sure if I need to alter my code in my scheduler and/or the code in my flow.

 

Here is how the flow is accessing information:

 

Before the first screen, a record lookup in flow occurs. It pulls the information from the account ID and a variable, vaAccountID. I think that the variable {!vaAccountID} is populated with the AccountID that is linked to the commission. (I didn’t make this flow. I’m not 100% sure

 

 

 

 

 

 

 

When I run my scheduler, I have it determine if there is an account with an account review date based upon the account Id. It sends the email template with the flow link to the sales department.

 

The scheduler sends the email and is “happy”. The criteria it needs to execute the method is complete (there is an account with an account review date 14 days from now and it sends the email).

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

sendEmail();

}
public void sendEmail()
{

for(Account acc : [SELECT Id FROM Account WHERE Next_Account_Review_Date__c = : system.Today().addDays(14)])
{
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setTemplateId('00XF0000000LfE0');
mail.setTargetObjectId('005J0000000JWYx');
mail.setSaveAsActivity(false);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail });
}
}


}

 

The flow however, is not “happy”. It does not know what account to pull information from.

 

In the first screen of the flow, it’s supposed to show the recurring revenue and link to the account page. The recurring revenue field is blank. The link to account page does not work.

 

If I try to advance past the first screen of the flow, I get a generic error screen from Salesforce and a detailed error message from Salesforce. It says:

 

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 = '{!Commission__c.AccountId__c}')

                                         ^ ERROR at Row:1:Column:89 invalid ID field: {!Commission__c.AccountId__c} (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 = '{!Commission__c.AccountId__c}')

                                         ^ ERROR at Row:1:Column:89 invalid ID field: {!Commission__c.AccountId__c} (There was a problem executing your command.) > RETRIEVE

 

Salesforce Error ID: 580775287-15539 (1733087783)

 

What does this error message mean? How can I get more information on Salesforce errors and how to trouble shoot on this matter?

I understand this process is very complex but it relates back to one fundamental question: How do I get the flow to realize that it needs to pull information from the account that had the review date 14 days away in my apex scheduler? 

I am new to apex and am trying to build an apex schedule class that runs everyday. If the account review date for commisions is two weeks (14 days) away the scheduler will send an email to our Sales Department.

 

I have a made a class that implements the schedulable interface. I am currently testing to make sure my code works. I've created a test method, but I'm not sure it works. I got this error email:

 

Sandbox

 

 

caused by: System.EmailException: SendEmail failed. First exception on row 0; first error: INVALID_SAVE_AS_ACTIVITY_FLAG, saveAsActivity must be false when sending mail to users.: []

 

Class.AccountReviewSchedulerOtherObjectIDETest.sendEmail: line 15, column 1

Class.AccountReviewSchedulerOtherObjectIDETest.execute: line 5, column 1

 

 

Without a test method, I get no response from my scheduler (not an error email or the email I am trying to send.

 

Here is my Apex Scheduled Class:

 

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

sendEmail(); 

}
public void sendEmail()
{


Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); 
mail.setTemplateId('00XF0000000LfE0'); 
mail.setTargetObjectId('005J0000000JWYx'); 
Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail }); 
}

public static testMethod void testschedule() {

Test.StartTest();
AccountReviewSchedulerOtherObjectID sh1 = new AccountReviewSchedulerOtherObjectID();

Test.stopTest();

}
}

 

 What does this error mean? Is my test class written the wrong way?

 

Thanks for all your help!

 

 

 

I am new to apex and built an apex schedule class that runs everyday. If the account review date for commisions is two weeks (14 days) away the scheduler will send an email to our Sales Department.

I have scheduled the class through the apex scheduler (NOT system.schedule)

How do I test my code?

Here is my schedulable class:

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

sendEmail();

}
public void sendEmail()
{


for(Account acc : [SELECT Id FROM Account WHERE Next_Account_Review_Date__c = : system.Today().addDays(14)])
{
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setTemplateId('00XF0000000LfE0');
mail.setTargetObjectId('005J0000000JWYx');
Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail });
}
}
}

To test the class would it be:

test.starttest();
AccountReviewSchedulerOtherObjectID sco = new scheduledCreateOpportunity();
test.stopTest();

If so, where would I put this class (what would I call it) and how would I schedule it?

 

Though my schedule class has a submited, start and next scheduled run on "Scheduled Apex Jobs"......no email is sent out. How do I test if the email method works? The test class? 

 

What exactly does a test class do besides providing test coverage? 


 

 

I am new to apex and am trying to build an apex schedule class that runs everyday. If the account review date for commisions is two weeks (14 days) away the scheduler will send an email to our Sales Department.

I'm almost done with the program but I keep getting this error:

Error: Compile Error: Comparison arguments must be compatible types: Schema.SObjectField, Date at line 9 column 9 where the if statement is

 

Here is my code:

global class AccountReviewScheduler implements Schedulable
{
    global void execute (SchedulableContext ctx) 
    {
        sendEmail();
    }
public void sendEmail()
{
    if (Account__r.Next_Account_Review_Date__c == System.today().addDays(14))
    {
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        Mail.setTemplateId('00XF0000000LfE1');
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail });
    }
}
  
}

 

I did some research and a commenter on another discussion board told me that it's not working because Account__r.Next_Account_Review_Date__c is just a field.

I have to use it with a record to make it have a value. Then I can compare that value with system.today().

I'm not sure what that means....create a custom object? I'm not sure how to do this. Here is my revised code I'm having trouble with. Here is my revised code:

Here is the revised code(asterisks ** denote change. Not included in actual code.)

global class AccountReviewScheduler implements Schedulable 
{ 
    global void execute (SchedulableContext ctx)  
    { 
        sendEmail(); 
    } 
public void sendEmail() 
{ 
    **CustomObj__c co = [select Account__r.Next_Account_Review_Date__c from CutomObj__c limit 1];** 
    **if(co.Account__r.Next_Account_Review_Date__c == system.today().addDays(14))**  
    { 
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); 
        Mail.setTemplateId('00XF0000000LfE1'); 
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail }); 
    } 
} 
 
} 

 

Changes are denoted by 2 asterisks ** Not included in real code...I know the custom obj code might be wrong...I got it off someone else and I'm not sure what to do.

NOTE: Time based workflows will not work here because this email has to occur every year (for many years) 14 days before the account review date. Time based workflows will work ONCE after they are set to trigger one year, but do not repeat. They do not recur by design. See: http://success.salesforce.com/questionDetail?qId=a1X30000000JnPMEA0

I have read the scheduling documentation: http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_scheduler.htm

and gone over the Apex workbook tutorial:http://blogs.developerforce.com/developer-relations/2010/02/spring-10-saw-the-general-availability-of-one-of-my-favorite-new-features-of-the-platform-the-apex-schedulerwith-the-apex-s.html as well as tutorial 14 in the official Apex guide but I still am having trouble.

MY REAL QUESTION IS: How do you declare/create a custom object in Apex? Do you think this is what is wrong with my code?

Any help/insight into my code/links to new resources would be greatly appreciated.

Thank you!

I am new to apex and am trying to build an apex schedule class that runs everyday. If the account review date for commisions is two weeks (14 days) away the scheduler will send an email to our Sales Department.

 

I'm almost done with the program but I keep getting this error:

 

Error: Compile Error: Comparison arguments must be compatible types: Schema.SObjectField, Date at line 9 column 9

 

What does this error mean? How can I trouble shoot it?

 

Here is the code: (Line 9 is where the if statement begins)

 

global class AccountReviewScheduler implements Schedulable
{
    global void execute (SchedulableContext ctx) 
    {
        sendEmail();
    }
public void sendEmail()
{
    if (Account__r.Next_Account_Review_Date__c == System.today().addDays(14))
    {
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        Mail.setTemplateId('00XF0000000LfE1');
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail });
    }
}
  
}

 

I am trying to implement a schedule class in Apex. I got my code from a bunch of different websites.

 

There seems to be a problem with the system.schedule method (the last line of code).

 

It says "Error: Compile Error: expecting a right parentheses, found 'Daily Commision Check' at line 21 column 16"

 

refering to the last line of code

system.schedule('Daily Commision Check', s, m); 

 

Here is my code:

 

global class AccountReviewScheduler implements Schedulable

{

    global void execute (SchedulableContext ctx)

    {

        sendEmail();

    }

public void sendEmail()

{

    if (Account.Next_Account_Review_Date_c == System.today().addDays(14))

    {

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

        Mail.setTemplateId('00XF0000000LfE1');

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

    }

}

    newScheduledClass m = new newScheduledClass();  

//schedule set for 2am everyday //

String s = '0 0 2 * * ?'; 

system.schedule('Daily Commision Check', s, m); 

}

 

Thanks,

 

Angelika

 

 

I am new to apex and am trying to build an apex schedule class that runs everyday. If the account review date for commisions is two weeks (14 days) away the scheduler will send an email to our Sales Department.

 

I think I have all the information I need. I have a made a class that implements the schedulable interface.

 

I have a system schedule method but I don't know where to put it....in the scheduable class or in a new class?

 

Here is my code...the major issue I am having right now is writting a test class. Why does Salesforce say you need one? What is the benefit of having one? Can someone jump start me with the code of Apex scheduler test class or at least tell me what I need to put in there(format)? I've read the documentation but I still don't get it.

 

global class AccountReviewScheduler implements Schedulable

{

    global void execute (SchedulableContext ctx)

    {

        sendEmail();

    }

public void sendEmail()

{

    if (Account.Next_Account_Review_Date_c == System.today().addDays(14))

    {

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

        Mail.setTemplateId('00XF0000000LfE1');

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

    }

}

    newScheduledClass m = new newScheduledClass();  

//schedule set for 2am everyday //

String s = '0 0 2 * * ?';

 

//  I got this code from someone else. //  

system.schedule('Daily Commision Check', s, m); 

}

 

 

 

I am new to apex and am trying to build an apex schedule class that runs everyday. If the account review date for commisions is two weeks (14 days) away the scheduler will send an email to our Sales Department.

 

I think I have all the information I need. I have a made a class that implements the schedulable interface.

 

I have a system schedule method but I don't know where to put it....in the scheduable class or in a new class?

 

Do I have to make a test class for this once my other code is all right? How do I build a test class?

 

Here is my code:

 

global class AccountReviewScheduler implements Schedulable {

 

   global void execute (SchedulableContext ctx) { //what does this line of code do?//

 

{

SendEmail();

}

 

 

public void SendEmail()

{

 

      if (Account.Next_Account_Review_Date_c == System.Today().addDays(14))

{

 

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

                 Mail.setTemplateId('00XF0000000LfE1;);

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

}

 

}

 

// I'm not sure how to link this information to the previous class, make another one? I don't think I can put it in the above class. This schedules my class to run every day at 2:00 am.

 

newScheduledClass m = new newScheduledClass();  

//schedule set for 2am everyday //

String s = '0 0 2 * * ?';

 

// I'm not sure what "Job Name" specifies. I got this code from someone else. //  

system.schedule('Job Name', s, m);

 

Thanks for your help. I have read over the tutorial in the Apex workbook and the Apex Scheduler in the documentation...but I still don't know where to put my system.schedule method or what "job name" means in my code.

 

Please help!

Hi,

 

I am new to apex and am trying to build an apex schedule class that runs everyday. If the account review date for commisions is two weeks (14 days) away the scheduler will send an email to our Sales Department.

 

I have hit a wall....How does apex understand time? I need an email to be sent 2 weeks before the account review date but I'm not sure how to tel apex this.  I would want my code logic to run something like this...

 

if {!Account.Next_Account_Review_Date_c} == two weeks away OR

 

If today is two weeks before account review date

{

sends email

}

 

I don't have official code yet....but is my logic sound? How would I code in apex "two weeks/14 days away"?

 

Thanks for your help!

 

 

The salesdepartment in my organization needs to review contracts 14 days before they are due recurring every year. We need to find a way that reminds them to review the information 14 days before the review date EVERY YEAR (recurring).
 
Upon the advice of this message board, I have decided to use a scheduled apex class. According to message board post, the class would run every day, find the records that meet the criteria, build the emails off of those records and email the correct recipients in the sales team.
 
So, the scheduled class would run everyday and determine if the review date is 14 days away. If the criteria is met (the review date is 14 days away) the scheduled class will send out an email to the sales team. If not, no emails are sent because the criteria is not met.
 
I have no experience programming in APEX, yet alone a scheduler class. Any advice for tutorials to look at or sample code?
 
Thank you so much!
 
 Hi,
 
The salesdepartment in my organization needs to review contracts 14 days before they are due recurring every year. We need to find a way that reminds them to review the information 14 days before the review date EVERY YEAR (recurring).
 
Time based work flows will not work because they will only occur once. I am considering using a custom APEX trigger to create a new evaluation task every time the previous one is completed or using a scheduled apex class. According to message board post, the class would run every day, find the records that meet the criteria, build the emails off of those records and email the correct recipients in the sales team.
 
 
 

I am new to Apex Coding. Besides an unmanaged Clone List App and many Managed Apps , my Salesforce Org does not have any custom Apex Code in it.

 

I tried to Deploy an Apex Class to production and got the error on a test class, Failure Message: "System.DmlException: Insert failed. First exception on row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, </BR></BR></BR><font color="blue">Redirecting in just a second.</font></BR><SCRIPT LANGUAGE="JavaScript">window.location = "../apex/SFSSDupeCatcher__SFSSDupeCatcher?dId0=MDAxRjAwMDAwMG5TWHg4SUFH&scId=YT...

 

I know how to fix this error (change validation rules on my Dupe Catcher App Temporarily.

 

This "quick fix" works for deploying my code, but what about when my Apex Code stays in production.

 

If new validation rules, either from DupeCatcher or for Contacts, Accounts etc are put into place after my code in deployed in production , this will break my test data. What are the implications of test data/classes/methods breaking in production? I know I won't be able to deploy new code, but is there anything else that happens, particulary to the code that the test method/class/data was written for? Will test methods that are broken break the classes they cover?

 

If you were just begining Apex Development in your organization, what would you do differently? How can Apex Code break in production? How can I prevent this?

 

Thanks,

 

Angelika

 

 

 

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?

I have written a test method for an Apex Birthday Scheduler Class. The class works great in Sandbox, sending an email to the CEO 2 days before an employees birthday.

 

However, I can't transfer this class to production because my class has 0 test coverage despite writting a test method that creates an account and contact that satisfies my class's query. Am I missing something here? Is there a way to "cheat" and get coverage?

 

I've spent hours on this to no avail. 

 

Here is my code and test method (red indicates lines not covered in Salesforce:

 

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

sendBirthdayEmail();

}
public void sendBirthdayEmail()
{

for(Contact con : [SELECT name, Id, Birthdate FROM Contact WHERE Next_Birthday__c = : system.Today().addDays(2) AND Account.Name = 'Company Inc.'])

{
String conId = con.Id;
String conName = con.name;
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
//mail.setTemplateId('00XJ0000000M31w');

mail.setTargetObjectId('005J0000000JWYx');
// mail.setToAddresses('email@company.com');
mail.setsubject('Company Birthday Reminder');
mail.setHtmlBody('This is a scheduler-generated email to notify you that the birthday of Name: <b> ' + con.name + ' </b> is in two days. Birthdate of: ' + con.Birthdate + '. Please wish them a happy Birthday.');
mail.setSaveAsActivity(false);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail });
}

}

 

// test method

static testMethod void myTestBirthday() {

//create the required test data needed for the test scenario
//In this case, I need to create a new contact, with the account name of Company and has a birthday 2 days away
Account testAccount = new Account(name='Company Inc.');
insert testAccount;
Contact testContact = new Contact();
testContact.firstName='Jack';
testContact.lastName='Dell';
insert testContact;

testContact.birthdate=system.Today().addDays(2);

update testContact;

testAccount.name = testContact.id;

//verify that the birthdate field was updated in the database

//Contact updatedContact = [SELECT birthdate FROM Contact WHERE Id =:testContact.Id];
//System.assertEquals(system.Today().addDays(2) , updatedContact.birthdate);

 

//Contact nextUpdatedContact = [SELECT Account.name FROM Contact WHERE Id =:testContact.Id];
//System.assertEquals(system.Today().addDays(2) , nextupdatedContact.birthdate);

for(Contact con : [SELECT name, Id, Birthdate FROM Contact WHERE Next_Birthday__c = : system.Today().addDays(2) AND Account.Name = 'Company Inc.'])
{
String conId = con.Id;
String conName = con.name;
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
//mail.setTemplateId('00XJ0000000M31w');

mail.setTargetObjectId('005J0000000JWYx');
// mail.setToAddresses('company@company.com');
mail.setsubject('Company Birthday Reminder');
mail.setHtmlBody('This is a scheduler-generated email to notify you that the birthday of Name: <b> ' + con.name + ' </b> is in two days. Birthdate of: ' + con.Birthdate + '. Please wish them a happy Birthday.');
mail.setSaveAsActivity(false);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail });
}
}

 

}

I have created a an Apex Birthday Scheduler that sends an email to a manager when a employees birthday is two days away. I have run tests in sandbox and my code works. 

 

I am currently building a test method. I get the error,  Error: Compile Error: Expression cannot be assigned at line -1 column -1 when I try to save my code.

 

This confuses me because my code ran before I added the test method. Why is the compiler saying there is an error at line -1 column -1...before my code? How is that even possible? 

 

Here is my code With test method:

 

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

sendBirthdayEmail(); 

}
public void sendBirthdayEmail()
{

   for(Contact con : [SELECT name, Id, Birthdate FROM Contact WHERE Next_Birthday__c = : system.Today().addDays(2) AND Account.Name = 'Projetech Inc.'])
 
  {
  String conId = con.Id;
  String conName = con.name;
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); 
//mail.setTemplateId('00XJ0000000M31w'); 

mail.setTargetObjectId('005J0000000JWYx');
// mail.setToAddresses('');
mail.setsubject('Birthday Reminder');
mail.setHtmlBody('This is a scheduler-generated email to notify you that the birthday of Name:  <b>  ' + con.name + '  </b> is in two days. Birthdate of: ' + con.Birthdate + '. Please wish them a happy Birthday.');
mail.setSaveAsActivity(false);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail }); 
}

}

static testMethod void myTestBirthday() {

//create the required test data needed for the test scenario
//In this case, I need to create a new contact, with the account name of Projetech and has a birthday 2 days away

Contact testContact = new Contact();
testContact.firstName='Jack';
testContact.lastName='Dell';
insert testContact;

testContact.birthdate=system.Today().addDays(2);

update testContact;

//verify that the birthdate field was updated in the database

Contact updatedContact = [SELECT  birthdate FROM Contact WHERE Id =:testContact.Id];
System.assertEquals(system.Today().addDays(2) , updatedContact.birthdate);

// do the same for account name

Account.name='Tests.';
update testContact;

Contact nextUpdatedContact = [SELECT Account.name FROM Contact WHERE Id =:testContact.Id];
System.assertEquals(system.Today().addDays(2) , nextupdatedContact.birthdate);

}



}

 

 

 

 

I have made an Apex Scheduler Class for Birthdays. The class works and successfully sends an email to our director when a birthday is 2 days away.

 

However, the email template that is sent when there is a birthday two days away needs to contain the contact's first, last name and birthdate. (The email template looks like this: This is a scheduler-generated email to notify you that the birthday of First Name: {!Contact.FirstName} Last Name: {!Contact.LastName} is on {!Contact.Next_Birthday__c}.

 

In my scheduler, I use a SOQl statement to query the database for the contact's id and first name. 

 

After I call the query, how do I convert the information I just queried to a string/method to be used in the email template?

 

My guess is return the results of the query as a string? Any Advice is appreciated!

 

Here is my code:

 

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


sendBirthdayEmail(); 

}
public void sendBirthdayEmail()
{

   for(Contact con : [SELECT Contact.name, Id FROM Contact WHERE Next_Birthday__c = : system.Today().addDays(2)])
 
  {
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); 
mail.setTemplateId('00XJ0000000M31w'); 
mail.setTargetObjectId('005J0000000JWYx'); 
mail.setSaveAsActivity(false);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail }); 
}
}


}

I am new to apex and am trying to build an apex schedule class that runs everyday. If the account review date for commisions is two weeks (14 days) away the scheduler will send an email to our Sales Department.

 

I'm almost done with the program but I keep getting this error:

 

Error: Compile Error: Comparison arguments must be compatible types: Schema.SObjectField, Date at line 9 column 9

 

What does this error mean? How can I trouble shoot it?

 

Here is the code: (Line 9 is where the if statement begins)

 

global class AccountReviewScheduler implements Schedulable
{
    global void execute (SchedulableContext ctx) 
    {
        sendEmail();
    }
public void sendEmail()
{
    if (Account__r.Next_Account_Review_Date__c == System.today().addDays(14))
    {
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        Mail.setTemplateId('00XF0000000LfE1');
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail });
    }
}
  
}

 

I am new to apex and am trying to build an apex schedule class that runs everyday. If the account review date for commisions is two weeks (14 days) away the scheduler will send an email to our Sales Department.

 

I think I have all the information I need. I have a made a class that implements the schedulable interface.

 

I have a system schedule method but I don't know where to put it....in the scheduable class or in a new class?

 

Here is my code...the major issue I am having right now is writting a test class. Why does Salesforce say you need one? What is the benefit of having one? Can someone jump start me with the code of Apex scheduler test class or at least tell me what I need to put in there(format)? I've read the documentation but I still don't get it.

 

global class AccountReviewScheduler implements Schedulable

{

    global void execute (SchedulableContext ctx)

    {

        sendEmail();

    }

public void sendEmail()

{

    if (Account.Next_Account_Review_Date_c == System.today().addDays(14))

    {

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

        Mail.setTemplateId('00XF0000000LfE1');

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

    }

}

    newScheduledClass m = new newScheduledClass();  

//schedule set for 2am everyday //

String s = '0 0 2 * * ?';

 

//  I got this code from someone else. //  

system.schedule('Daily Commision Check', s, m); 

}

 

The salesdepartment in my organization needs to review contracts 14 days before they are due recurring every year. We need to find a way that reminds them to review the information 14 days before the review date EVERY YEAR (recurring).
 
Upon the advice of this message board, I have decided to use a scheduled apex class. According to message board post, the class would run every day, find the records that meet the criteria, build the emails off of those records and email the correct recipients in the sales team.
 
So, the scheduled class would run everyday and determine if the review date is 14 days away. If the criteria is met (the review date is 14 days away) the scheduled class will send out an email to the sales team. If not, no emails are sent because the criteria is not met.
 
I have no experience programming in APEX, yet alone a scheduler class. Any advice for tutorials to look at or sample code?
 
Thank you so much!
 
 Hi,
 
The salesdepartment in my organization needs to review contracts 14 days before they are due recurring every year. We need to find a way that reminds them to review the information 14 days before the review date EVERY YEAR (recurring).
 
Time based work flows will not work because they will only occur once. I am considering using a custom APEX trigger to create a new evaluation task every time the previous one is completed or using a scheduled apex class. According to message board post, the class would run every day, find the records that meet the criteria, build the emails off of those records and email the correct recipients in the sales team.
 
 
 

Hi,

I am attempting to send email from apex class using a template. How can I pass values into the merge fields?

 

Thank you, appreciate the help  

  • December 23, 2011
  • Like
  • 0