You need to sign in to do that
Don't have an account?
Mayank Deshpande 2
error while saving a trigger "Error: Compile Error: Expression cannot be assigned at line -1 column -1 "
Hi ,
I write a apex class on task object which will insert a record.
Public class InsertTaskForDEMO {
public static void newtask()
{ List<Task> ACCD = new List<Task>();
for (Integer i=0; i<3; i++)
{
Task Act = new Task(Status='Not Started',Subject='Other');
ACCD.add(Act);
insert ACCD;
}}}
i want to call this newtask method in trigger made on custom object
trigger checkpromotiondetails on Task (after insert)
{
for (Demo__c Task :trigger.new)
{ if (Demo__c.Eligible_for_Bouns__c = 'True')
{
InsertTaskForDEMO.newtask();
}
}}
while trying to same it is givning me error.
I write a apex class on task object which will insert a record.
Public class InsertTaskForDEMO {
public static void newtask()
{ List<Task> ACCD = new List<Task>();
for (Integer i=0; i<3; i++)
{
Task Act = new Task(Status='Not Started',Subject='Other');
ACCD.add(Act);
insert ACCD;
}}}
i want to call this newtask method in trigger made on custom object
trigger checkpromotiondetails on Task (after insert)
{
for (Demo__c Task :trigger.new)
{ if (Demo__c.Eligible_for_Bouns__c = 'True')
{
InsertTaskForDEMO.newtask();
}
}}
while trying to same it is givning me error.
Replace this code
All Answers
Change
to
after changing i am facing below error
Error: Compile Error: Comparison arguments must be compatible types: Schema.SObjectField, String at line 4 column 7
What is filed data type of Eligible_for_Bouns__c .
if this is belongs to boolean means check box use like this of
if (Task.Eligible_for_Bouns__c == True )
if it is text
if (Task.Eligible_for_Bouns__c == 'True' )
Regards ,
Harish.R.
Thanks !
after change in below code
trigger checkpromotiondetails on Task (after insert)
{
for (Demo__c Task :trigger.new)
{ if (Task.Eligible_for_Bouns__c == True)
{
InsertTaskForDEMO.newtask();
}
}}
facing new error
Error: Compile Error: Loop variable must be of type Task at line 3 column 14
Replace this code
to
Code is working fine
i am facing below error after saving demo object record
Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger checkpromotiondetails caused an unexpected exception, contact your administrator: checkpromotiondetails: execution of AfterInsert caused by: System.DmlException: Insert failed. First exception on row 0 with id 00T6F00003LZHjAUAX; first error: INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id]: Class.InsertTaskForDEMO.newtask: line 10, column 1
It's a best practce to handle DML operations outside of loops anway since there are limitations on how many DML operations you can call. You should review the article on bulkifying code (https://developer.salesforce.com/page/Best_Practice%3A_Bulkify_Your_Code) for more tips on how to avoid errors like this in the future.
i did below modification in apex class.
Public class InsertTaskForDEMO {
public static void newtask()
{ List<Task> ACCD = new List<Task>();
for (Integer i=0; i<3; i++)
{
Task Act = new Task(OwnerId='0056F000006PlyK',Status='Not Started',Subject='Other',Priority='Low');
ACCD.add(Act);
}insert ACCD;}}
and in trigger i called a method new task on the condition.
trigger checkpromotiondetails on Demo__c (before insert)
{
for (Demo__c Task :trigger.new)
{ if (Task.Eligible_for_Bouns__c == True)
{
InsertTaskForDEMO.newtask();
}
}}
Condition fulfilled and records got saved but in related list no record created for open activity.
Are you trying to relate the task to the Demo__c record? If so you need to pass the Id for that record into your newTask method and set the WhatId for the task to the Id for the Demo__c record.