+ Start a Discussion
grandmastergrandmaster 

Getting error: You can't relate a prospect to an entity or other object.: [WhatId]

Hi

 

I have a custom VF page (with a controller behind) where a task is created. The task can relate a Contact with a Campaign successfully.

 

However, when I try to relate a Task with a Lead (Prospect) and Campaign, I get the following error:

 

You can't relate a prospect to an entity or other object.

System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, You can't relate a prospect to an entity or other object.: [WhatId]

 

Any suggestion how to fix this?

 

thanks

gm

Rahul_sgRahul_sg
seems you are using WhatId instead of Whoid
grandmastergrandmaster

Thanks Rahul_sg. Well here is the code, and am not too sure whats wrong in it:

public with sharing class GoActivity {
public class TaskRecord {
public Task record { get; set; }
public Boolean selected { get; set; }
public TaskRecord() {
record = new Task(
Subject = 'Follow-Up Action',
Type = 'Action Item',
Priority = 'Normal',
ActivityDate = Date.today().addDays(2)
);
selected = false;
}
}
public List<SelectOption> assignedToOptions { get; private set; }
public String assignedTo { get; set; }
public Task task { get; private set; }
public List<TaskRecord> tasks { get; private set; }
public Boolean selectAll { get; set; }
public Id contactId { get; private set; }
public transient String dmessage { get; private set; }
public GoActivity() {
reset();
contactId = ApexPages.currentPage().getParameters().get('id');
if (contactId != null) {
task.WhoId = contactId;
}
}
private void reset() {
User u = [SELECT Name, BSS__r.Name, Assistant__r.Name FROM User WHERE Id=:UserInfo.getUserId()];
assignedToOptions = new List<SelectOption> {
new SelectOption('', '--None--'),
new SelectOption(u.Id, u.Name)
};
if (u.BSS__r.Name != null) {
assignedToOptions.add(new SelectOption(u.BSS__r.Id, u.BSS__r.Name));
}
if (u.Assistant__r.Name != null) {
assignedToOptions.add(new SelectOption(u.Assistant__r.Id, u.Assistant__r.Name));
}
assignedTo = null;
task = new Task(
OwnerId = UserInfo.getUserId(),
Status = 'Completed',
ActivityDate = Date.today(),
Priority = 'Normal',
Subject = 'Meeting Notes',
Type = 'Meeting Notes'
);
tasks = new List<TaskRecord>();
addTask();
selectAll = false;
contactId = null;
}
public void addTask() {
tasks.add(new TaskRecord());
selectAll = false;
}
public void removeTasks() {
List<TaskRecord> tasks = new List<TaskRecord>();
for (TaskRecord t : this.tasks) {
if (!t.selected) {
tasks.add(t);
}
}
this.tasks = tasks;
if (tasks.isEmpty()) {
addTask();
}
selectAll = false;
}
public void cloneTasks() {
List<TaskRecord> tasks = new List<TaskRecord>();
for (TaskRecord t : this.tasks) {
if (t.selected) {
TaskRecord tClone = new TaskRecord();
tClone.record = t.record.clone(false, true);
tasks.add(tClone);
}
}
this.tasks.addAll(tasks);
selectAll = false;
}
public void updateTasks() {
String field = ApexPages.currentPage().getParameters().get('type');
String value;
if (field == 'assignedTo') {
field = 'OwnerId';
value = assignedTo;
} else {
return;
}
for (TaskRecord t : tasks) {
if (t.selected) {
t.record.put(field, value);
}
}
}
public PageReference save() {
System.SavePoint sp = Database.setSavePoint();
PageReference ref;
try {
Task newTask = task.clone(false, true);
List<Task> newTasks = new List<Task> {
newTask
};
for (TaskRecord t : tasks) {
if (t.record.Subject != null && t.record.OwnerId != null) {
t.record.WhoId = newTask.WhoId;
t.record.WhatId = newTask.WhatId;
newTasks.add(t.record.clone(false, true));
}
}
insert newTasks;
if (contactId != null) {
ref = new PageReference('/' + contactId);
ref.setRedirect(true);
}
reset();
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.CONFIRM, 'Save successful'));
} catch (Exception e) {
Database.rollback(sp);
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, '' + e));
}
return ref;
}
public void saveAndClone() {
Task task = this.task;
List<TaskRecord> tasks = this.tasks;
save();
if (ApexPages.hasMessages(ApexPages.Severity.CONFIRM)) {
this.task = task;
this.tasks = tasks;
}
}
public PageReference cancel() {
PageReference ref = new PageReference('/' + contactId);
ref.setRedirect(true);
return ref;
}


}

grandmastergrandmaster

I'm still the same error.

 

Any help/inputs/suggestions?

 

Thanks