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
Drew Gehman 18Drew Gehman 18 

Assistance with Compile Error: Illegal assignment from List<Task> to String

Bet this is easy for someone, but I am so raw I am just cut and pasting... I have created my first trigger which simply takes the account number of an account, and assigns it to a custom task field AccountBannerID__C. I have tested it in my sandbox and it works. I am now creating my test. 
Here is the test code (I think the trigger code in not applicable though I can load it if necessary)

Thank you for your assistance, and I will try to pay it forward by posting the trigger and the successful test to the forums

It doesn't like this line 
String t = [Select AccountBannerID__c from Task where id = :t1.id];
I get this Compile Error: Illegal assignment from List<Task> to String

@isTest
private class TaskRelatedToACCTriggerTestClass { 
static testMethod void myUnitTest() {
// Run test of code
//HERE, WE CREATE AN ACCOUNT RECORD PUTTING A VALUE IN THE FIELD THAT YOU CALL IN YOUR TRIGGER
//WE WANT TO TEST THE COMPLETE FUNCTIONALITY OF YOUR TRIGGER SO IT IS IMPORTANT TO VALIDATE, VIA THE TEST CLASS
//THAT YOUR TRIGGER ACHIEVES THE DESIRED RESULTS.. ...UPDATING A FIELD ON A TASK WITH A VALUE FROM AN ACCOUNT
Account aDS = new Account(name='jbailer', AccountNumber='123');
insert aDS; 
//NOW, CREATE A TASK ASSIGNED TO THE ACCOUNT
Task t1 = new Task(subject='test', WhatId = aDS.id, description = 'test description'); 
insert t1; 
//NOW, THE CRITICAL PART, WE WILL RUN AN ASSERT CALL TO MAKE SURE THAT THE CUSTOM FIELD ON THE TASK HAS THE VALUE FROM THE OPP
//THIS IS WHAT TESTS THE FUNCTIONALITY OF YOUR TRIGGER

String t = [Select AccountBannerID__c from Task where id = :t1.id];
system.AssertEquals('123', t);

}
}
Best Answer chosen by Drew Gehman 18
James LoghryJames Loghry
@isTest
private class TaskRelatedToACCTriggerTestClass { 
static testMethod void myUnitTest() {
// Run test of code
//HERE, WE CREATE AN ACCOUNT RECORD PUTTING A VALUE IN THE FIELD THAT YOU CALL IN YOUR TRIGGER
//WE WANT TO TEST THE COMPLETE FUNCTIONALITY OF YOUR TRIGGER SO IT IS IMPORTANT TO VALIDATE, VIA THE TEST CLASS
//THAT YOUR TRIGGER ACHIEVES THE DESIRED RESULTS.. ...UPDATING A FIELD ON A TASK WITH A VALUE FROM AN ACCOUNT
Account aDS = new Account(name='jbailer', AccountNumber='123');
insert aDS; 
//NOW, CREATE A TASK ASSIGNED TO THE ACCOUNT
Task t1 = new Task(subject='test', WhatId = aDS.id, description = 'test description'); 
insert t1; 
//NOW, THE CRITICAL PART, WE WILL RUN AN ASSERT CALL TO MAKE SURE THAT THE CUSTOM FIELD ON THE TASK HAS THE VALUE FROM THE OPP
//THIS IS WHAT TESTS THE FUNCTIONALITY OF YOUR TRIGGER

Task t = [Select AccountBannerID__c from Task where id = :t1.id];
system.AssertEquals('123', t.AccountBannerID__c);

}

SOQL queries either return an sObject (such as a generic sObject or a Task) or a List of sObjects, and you're attempting to assign it to a String, which is why you're seeing the compile error.  Try changing String to task like in my example, and asserting the AccountBannerId__c field instead.

All Answers

James LoghryJames Loghry
@isTest
private class TaskRelatedToACCTriggerTestClass { 
static testMethod void myUnitTest() {
// Run test of code
//HERE, WE CREATE AN ACCOUNT RECORD PUTTING A VALUE IN THE FIELD THAT YOU CALL IN YOUR TRIGGER
//WE WANT TO TEST THE COMPLETE FUNCTIONALITY OF YOUR TRIGGER SO IT IS IMPORTANT TO VALIDATE, VIA THE TEST CLASS
//THAT YOUR TRIGGER ACHIEVES THE DESIRED RESULTS.. ...UPDATING A FIELD ON A TASK WITH A VALUE FROM AN ACCOUNT
Account aDS = new Account(name='jbailer', AccountNumber='123');
insert aDS; 
//NOW, CREATE A TASK ASSIGNED TO THE ACCOUNT
Task t1 = new Task(subject='test', WhatId = aDS.id, description = 'test description'); 
insert t1; 
//NOW, THE CRITICAL PART, WE WILL RUN AN ASSERT CALL TO MAKE SURE THAT THE CUSTOM FIELD ON THE TASK HAS THE VALUE FROM THE OPP
//THIS IS WHAT TESTS THE FUNCTIONALITY OF YOUR TRIGGER

Task t = [Select AccountBannerID__c from Task where id = :t1.id];
system.AssertEquals('123', t.AccountBannerID__c);

}

SOQL queries either return an sObject (such as a generic sObject or a Task) or a List of sObjects, and you're attempting to assign it to a String, which is why you're seeing the compile error.  Try changing String to task like in my example, and asserting the AccountBannerId__c field instead.
This was selected as the best answer
James LoghryJames Loghry
Also, when you post code on these forums, please use the code formatting button (< >).  Thanks!