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
hurdrahurdra 

Need Help with Task Insert Trigger in Apex

I am developing a simple app to help our sales reps manage orders of product samples sent to customers.  I am currently writing a trigger on the insert event of a custom object called Sample Order.  The code is below; as you will see, I'm getting errors on two lines - the line where I set the Task.WhatId and Task.OwnerId.  Can someone help me with this, please?  I'm very new to Salesforce.com and Apex code.  Thanks!

 

trigger CreateOrderTask on Sample_Order__c (after insert) {
    Task orderTask = new Task();
    orderTask.ActivityDate = date.today() + 7;


    // ERROR for line below is "Illegal assignment from Schema.SObjectField to Id"
    orderTask.WhatId = Sample_Order__c.id;


    // ERROR for line below is "Illegal assignment from Schema.SObjectField to Id
    // Sales_Rep__c is a reference field to User object
    orderTask.OwnerId = Sample_Order__c.Sales_Rep__c.Id;


    orderTask.Priority = 'High';
    orderTask.Status = 'Not Started';
    orderTask.Subject = 'Sample Order Request for ' + Sample_Order__c.Account__c.Name;
    orderTask.IsReminderSet = true;
    orderTask.ReminderDateTime = datetime.now() + 7;
    Insert orderTask;
}

Best Answer chosen by Admin (Salesforce Developers) 
hurdrahurdra

This is now working!  I am posting the full trigger code below for the benefit of others (especially newbies like me) that might have a similar situation:

 

trigger CreateOrderTask on Sample_Order__c (after insert) {
    Sample_Order__c SO = Trigger.new[0];
    Task orderTask = new Task();
    orderTask.ActivityDate = date.today() + 7;
    orderTask.WhatId = SO.id;
    orderTask.OwnerId = SO.Sales_Rep__c;
    orderTask.Priority = 'High';
    orderTask.Status = 'Not Started';
    Account SOAccount = [Select Id, Name from Account where Id = :SO.Account__c];
    String SOAccountName = SOAccount.Name;
    orderTask.Subject = 'Sample Order Request for ' + SOAccountName;
    orderTask.IsReminderSet = true;
    orderTask.ReminderDateTime = datetime.now() + 7;
    Insert orderTask;
}

 

Many thanks to users ashish rai and spraetz for their help on this!

 

The one other thing I could not figure out to do is programmatically set the orderTask to "Send Notification Email" such as what is achieved by selected this checkbox on the standard Task Page Layout.  I checked the documentation for the standard Task object, but there doesn't seem to be a field for this.  If anyone knows how to do this, I would appreciate your sharing this here.  Thanks!

All Answers

ashish raiashish rai

Hello,

     Well use the below code:

 

trigger CreateOrderTask on Sample_Order__c (after insert)

 {   

Sample_Order__c SO=Trigger.new[0];

Task orderTask = new Task();orderTask.ActivityDate = date.today() + 7;   

orderTask.WhatId = SO.id;     orderTask.OwnerId = SO.Sales_Rep__c.Id;   

orderTask.Priority = 'High';   

orderTask.Status = 'Not Started';   

orderTask.Subject = 'Sample Order Request for ' +SO.Account__c.Name;   

orderTask.IsReminderSet = true;   

orderTask.ReminderDateTime = datetime.now() + 7;   

Insert orderTask;

}

 

Think this will help you.

 

hurdrahurdra

This almost worked - but not quite.

 

Here is how I changed the code:

    Sample_Order__c SO = Trigger.new[0];
    Task orderTask = new Task();
    orderTask.ActivityDate = date.today() + 7;
    orderTask.WhatId = SO.id;
    orderTask.OwnerId = SO.Sales_Rep__r.Id;
    orderTask.Priority = 'High';
    orderTask.Status = 'Not Started';
    orderTask.Subject = 'Sample Order Request for ' + SO.Account__r.Name;
    orderTask.IsReminderSet = true;
    orderTask.ReminderDateTime = datetime.now() + 7;
    Insert orderTask;

 

I tried running this directly (no test case); I got this error:

System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, CreateOrderTask: execution of AfterInsert

caused by: System.DmlException: Insert failed. First exception on row 0; first error: INVALID_CROSS_REFERENCE_KEY, Assigned To ID: owner cannot be blank: [OwnerId]

Trigger.CreateOrderTask: line 12, column 5:

 

Afterward, I developed a simple test case with a try/catch block; I got the same exception message.

 

I've done some searches on Google and here on "INVALID_CROSS_REFERENCE_KEY", but I cannot find anything that sheds light on my particular problem.

 

Any thoughts?

spraetzspraetz

Invalid cross refernce key means you're attempting to set a lookup or master-detail field to a value that isn't an ID or null.

 

data from related objects does not come populated in Trigger.new.

 

Your assigning of OwernId to So.Sales_Rep__r.id isn't working beceause So.Sales_rep__r is blank.  In order to populated it, you would need to do a query.

hurdrahurdra

I'm assuming by doing a query that you mean that I need to select the just inserted Sample Order into a local variable.  Since I'm very new at this, would you mind giving me an example of how to do this, please?

spraetzspraetz

I meant you'd have to query the Sales_Rep__c record, but...

 

Actually, now that I look at it again, you should be able to simply assign SO.Sales_Rep__c into there as lookup fields are stored as ids.  This would prevent you from having to query the ID specifically.

 

You do however have to make sure that the Sales_Rep__c field is populated on all your SO records or this will error.

 

hurdrahurdra

Almost home!  What you suggested worked; it entered the new Task as expected.  However, I have a similar problem with the Subject line;  the reference "SO.Account__r.Name" returns a null value:   

 

   Sample_Order__c SO = Trigger.new[0];
    Task orderTask = new Task();
    orderTask.ActivityDate = date.today() + 7;
    orderTask.WhatId = SO.id;
    orderTask.OwnerId = SO.Sales_Rep__c;
    orderTask.Priority = 'High';
    orderTask.Status = 'Not Started';
    orderTask.Subject = 'Sample Order Request for ' + SO.Account__r.Name;  // returns "Sample Order Request for null"
    orderTask.IsReminderSet = true;
    orderTask.ReminderDateTime = datetime.now() + 7;
    Insert orderTask;

 

I assume from your earlier post that I would need to do something like this:

 

    Account myAcct = [Select Name from Account Where Id = SO.Account__c];

 

However, when I tried this, I get a compile error that says that "SO.Account__c" is "unexpected token".  What is the correct way to do this?

spraetzspraetz

When inserting variables into SOQL queries, you need to prefix it with a colun (:)

 

So your query should look like

 

Account myAcct = [SELECT Name FROM Account WHERE id = :SO.Account__c];

hurdrahurdra

This is now working!  I am posting the full trigger code below for the benefit of others (especially newbies like me) that might have a similar situation:

 

trigger CreateOrderTask on Sample_Order__c (after insert) {
    Sample_Order__c SO = Trigger.new[0];
    Task orderTask = new Task();
    orderTask.ActivityDate = date.today() + 7;
    orderTask.WhatId = SO.id;
    orderTask.OwnerId = SO.Sales_Rep__c;
    orderTask.Priority = 'High';
    orderTask.Status = 'Not Started';
    Account SOAccount = [Select Id, Name from Account where Id = :SO.Account__c];
    String SOAccountName = SOAccount.Name;
    orderTask.Subject = 'Sample Order Request for ' + SOAccountName;
    orderTask.IsReminderSet = true;
    orderTask.ReminderDateTime = datetime.now() + 7;
    Insert orderTask;
}

 

Many thanks to users ashish rai and spraetz for their help on this!

 

The one other thing I could not figure out to do is programmatically set the orderTask to "Send Notification Email" such as what is achieved by selected this checkbox on the standard Task Page Layout.  I checked the documentation for the standard Task object, but there doesn't seem to be a field for this.  If anyone knows how to do this, I would appreciate your sharing this here.  Thanks!

This was selected as the best answer