+ Start a Discussion
andy_hudsonandy_hudson 

Apex code for Alerts and Reminders

Hey all

 

I am looking to write some Apex code for a task manager app that I am building that allows the user to set a reminder and alert for the completiion of their task.  I understand I want to use:

 

IsReminderSet

ReminderDateTime

 

I'm not very good at code though and not sure how I would write the code to do what I want to do, can anyone help please?!

 

Many thanks

 

Andy

Chamil MadusankaChamil Madusanka

Hi,

 

here is the code example,

 

<apex:page controller="testtask">
<apex:form >
<apex:commandButton value="Create Task" action="{!createTask}"/>
</apex:form>
</apex:page>

 

public class testtask {

public PageReference createTask() {

Task newTask = new Task(Description = 'Survey Transaction111',
Priority = 'Normal',
Status = 'Inbound Email',
Subject = 'Other',
IsReminderSet = true,
ReminderDateTime = System.now()+1,
WhoId = 'UserIdHere'
);
insert newTask
return null;
}

}

 Please set a user Id in WhoId.

 

If a reply to a post answers your question or resolves your problem, please mark it as the solution to the post so that others may benefit.

Viraj MViraj M
I tried to pass userid i am getting below error ..here is my code., can you please help me on this

System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, Related To ID: id value of incorrect type: 0054D000000TNxfQAG: [WhatId]
Error is in expression '{!createTask}' in component <apex:commandButton> in page investmentcontactpersonreminder: Class.testtask.createTask: line 13, column 1
public class testtask {

public PageReference createTask() {
System.Debug('message') ;
Task newTask = new Task(Description = 'Survey Transaction111',
Priority = 'Normal',
Status = 'Inbound Email',
Subject = 'Other',
IsReminderSet = true,
ReminderDateTime = System.now()+1,
WhoId = UserInfo.getUserId() 
);
insert newTask;
return null;
}

}





 
Tyler Dahle 9Tyler Dahle 9
This is kind of old... but the error above is due to the fact that you can only Ids to lookup fields that are actually a part of the lookup... WhoId is limited to Contacts and Leads, and you are trying to pass a UserId to it. Same goes for WhatId, or any lookup field.