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
Mayank Deshpande 2Mayank 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.

 
Best Answer chosen by Mayank Deshpande 2
Harish RamachandruniHarish Ramachandruni
Hi,

 
trigger checkpromotiondetails on Task (after insert)
{
for (Task T :trigger.new)
{ if (t.Eligible_for_Bouns__c == True)
{
InsertTaskForDEMO.newtask();
}

}}

Replace this code 

All Answers

John TowersJohn Towers
Checking for equality in an if statement requires a == - you have just one:

Change
if (Demo__c.Eligible_for_Bouns__c = 'True')

to
 
if (Demo__c.Eligible_for_Bouns__c == 'True')

 
Mayank Deshpande 2Mayank Deshpande 2
Hi John,

after changing i am facing below error


Error: Compile Error: Comparison arguments must be compatible types: Schema.SObjectField, String at line 4 column 7
John TowersJohn Towers
Is Eligible_for_Bonus__c a boolean field? If so, you need to remove the quotes around 'True'. Booleans are their own type and don't get quoted.
Harish RamachandruniHarish Ramachandruni


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.
 
Mayank Deshpande 2Mayank Deshpande 2
Hi Harish,
              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
Harish RamachandruniHarish Ramachandruni
Hi,

 
trigger checkpromotiondetails on Task (after insert)
{
for (Task T :trigger.new)
{ if (t.Eligible_for_Bouns__c == True)
{
InsertTaskForDEMO.newtask();
}

}}

Replace this code 
This was selected as the best answer
John TowersJohn Towers
Trigger.new is a list of Tasks, not Demo__c. You need to change 
 
for (Demo__c Task :trigger.new)

to
 
for (Task Task :trigger.new)

 
Mayank Deshpande 2Mayank Deshpande 2
Hi John/Harish,

Code is working fine
Mayank Deshpande 2Mayank Deshpande 2
Hi 

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
John TowersJohn Towers
This is a problem with your loop. You are adding the new task to a list and then inserting the list all inside of the loop.This causes you to try to insert each object in the list for every iteration of the loop. When you get to the second iteration you've already inserted the first task you aded to the list. since it has an Id already, you cannot insert it again. Modify your loop like this:
 
for (Integer i=0; i<3; i++)
{
 Task Act = new Task(Status='Not Started',Subject='Other');
   ACCD.add(Act);
}

insert ACCD; // insert outside of the loop so all tasks are inserted at once at the end of the loop

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.
Mayank Deshpande 2Mayank Deshpande 2
Hi John ,

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.
John TowersJohn Towers
Your task isn't being associated with anything when you create it. You have to set either the WhoId on the task to relate it to a contact or the WhatId to relate it to another object.

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.