• Ryan Neel
  • NEWBIE
  • 30 Points
  • Member since 2020

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 3
    Questions
  • 2
    Replies
Hi folks, I had the following code working (Pic1), but I wanted to change the SOSL query to SOQL (Pic2) so I could limit the fields the user was searching through.  After changing the query to SOQL however, I'm getting the error "Illegal assignment from Account to List<Account> (29:13)".

SOSL Query:
public with sharing class AccountSearchController {
    @AuraEnabled
    public static List<Account> searchAccounts( String searchTerm ) {
        List<Account> accounts = new List<Account>();
        if ( String.isNotBlank( searchTerm ) ) {
            List<List<sObject>> searchResults = [
                FIND :searchTerm
                RETURNING Account(
                    Id, Name, Phone, Website,
                    BillingStreet, BillingCity,
                    BillingState, BillingPostalCode
                   ORDER BY Name
                   LIMIT 10
              )
            ];
            accounts = searchResults[0];
        }
        return accounts;
    }
}
SOQL Query:
public with sharing class AccountSearchController {
    @AuraEnabled
    public static List<Account> searchAccounts( String searchTerm ) {
        List<Account> accounts = new List<Account>();
        if ( String.isNotBlank( searchTerm ) ) {
            List<List<sObject>> searchResults = [
                SELECT Id
                      ,Name
                      ,Phone
                      ,Website
                      ,BillingStreet
                      ,BillingCity
                      ,BillingState
                      ,BillingPostalCode
                FROM Account
                WHERE BillingState = :searchTerm OR
                      BillingPostalCode = :searchTerm
                LIMIT 50  
            ];
            accounts = searchResults[0];
        }
        return accounts;
    }
}

 
Here's the controller:
public class UpdateStoreVisitInventoryController {
  @Auraenabled //For the init function of UpdateStoreVisitInventory comoponent. Gets the Product Catalogs from the Ship To the store visit is associated with
    public static List<Ship_To_Product_Catalog__c> shipcat(Id storevisitId){
        system.debug(storevisitId);
        return [Select Id,Get_Inventory__c,Product__r.Name, Product__r.NoPrefixProduct__c, Product__r.Product_Category__c,Quantity__c FROM Ship_To_Product_Catalog__c WHERE Ship_To__c =: storevisitId ORDER BY Get_Inventory__c DESC];
    }
  @Auraenabled //For the combobox filter. Gets all Product Categories and turns them into a list.	
    public static List<sObject> productcats(Id storevisitId) {
//        return [SELECT Product_Category__c FROM Product2 WHERE Product_Class__c NOT IN ('Installs', 'None', 'Repair', 'Resale', 'Strings') GROUP BY Product_Category__c];
		return [SELECT Product__r.Product_Category__c FROM Ship_To_Product_Catalog__c WHERE Ship_To__c =: storevisitId GROUP BY Product__r.Product_Category__c];
    }
    
  @AuraEnabled //For the Update fucntion in UpdateStoreVisitInventory component. Takes the List of Product Catalogs compares to the Updated SPIds and updates accordingly
    public static void updateSPCat(List<Ship_To_Product_Catalog__c> SPCat, List<Id> SPId, Id storevisitId ){
        system.debug(SPCat);
        system.debug(SPId);
        List<Ship_To_Product_Catalog__c> upSPCat = new List<Ship_To_Product_Catalog__c>();
        
        for(Ship_To_Product_Catalog__c temp : SPCat){
            system.debug(temp);
            if(SPId.contains(temp.Id)){
                temp.Date_Last_Reported_In_Store__c = date.today();
                upSPCat.add(temp);
            }
        }
        update upSPCat;
    } 
}

 
I've tried two bits of code now and I keep getting the error:

Challenge not yet complete in My Trailhead Playground 3
The 'challengeComplete' method in the 'UnitOfWorkTest' class has not successfully passed all tests. Ensure that you run the tests and it passes successfully before attempting this challenge again.


Here's the code I'm trying to run:

//Code attempt #2
public class UnitOfWorkTest {
    public static void challengeComplete() {
        fflib_SObjectUnitOfWork uow = new fflib_SObjectUnitOfWork(
            new Schema.SObjectType[]{
                Account.SObjectType,
                Contact.SObjectType, 
                Note.SObjectType});
        for (Integer i = 0; i < 100; i++) {
            Account acc = new Account(Name = 'TestAcc' + i);
            uow.registerNew(acc);
            for (Integer j = 0; j < 5; j++) {
                Contact c = new Contact(LastName = 'TestContact' + i + '_' + j);
                uow.registerNew(c, Contact.AccountId, acc);
                Note n = new Note(Title = 'TestNote' + i + '_' + j, Body = 'Test note body.');
                uow.registerNew(n, Note.ParentId, acc);
            }
        }
        uow.commitWork();
        System.assertEquals(100, [SELECT Id from Account].size());
        System.assertEquals(500, [SELECT Id from Contact].size());
        System.assertEquals(500, [SELECT Id From Note].size());
    }
}
/*My Original Code, attempt #1
public class UnitOfWorkTest {
    public static void challengeComplete(){
        //Create the unit of work
        fflib_SObjectUnitOfWork uow = new fflib_SObjectUnitOfWork(
            new Schema.SObjectType[]{
                Account.SObjectType,
                Contact.SObjectType,
                Note.SObjectType
            }
        );
        //Do Some Work!
        for(Integer a=[select id from account].size(); a<100; a++){
            Account acc = new Account();
            acc.Name = 'UoW Test Name' + a;
            uow.registerNew(acc);
            for(Integer c=0; c<5; c++){
                Contact cont = new Contact();
                cont.FirstName = 'James ';
                cont.LastName = acc.name + c;
                uow.registernew(cont, Contact.AccountId, acc);
                Note nt = new Note();
                nt.title = 'UoW Note #' + c;
                nt.Body = 'UoW Body ' + c;
                uow.registernew(nt, Note.ParentId, acc);
            }
        }
        //Commit the work to the database
        uow.commitWork();
        
        System.assertEquals(100, [select id from account].size());
        System.assertEquals(500, [select id from contact].size());
        System.assertEquals(500, [select id from note].size());
    }
}
*/
Here's the controller:
public class UpdateStoreVisitInventoryController {
  @Auraenabled //For the init function of UpdateStoreVisitInventory comoponent. Gets the Product Catalogs from the Ship To the store visit is associated with
    public static List<Ship_To_Product_Catalog__c> shipcat(Id storevisitId){
        system.debug(storevisitId);
        return [Select Id,Get_Inventory__c,Product__r.Name, Product__r.NoPrefixProduct__c, Product__r.Product_Category__c,Quantity__c FROM Ship_To_Product_Catalog__c WHERE Ship_To__c =: storevisitId ORDER BY Get_Inventory__c DESC];
    }
  @Auraenabled //For the combobox filter. Gets all Product Categories and turns them into a list.	
    public static List<sObject> productcats(Id storevisitId) {
//        return [SELECT Product_Category__c FROM Product2 WHERE Product_Class__c NOT IN ('Installs', 'None', 'Repair', 'Resale', 'Strings') GROUP BY Product_Category__c];
		return [SELECT Product__r.Product_Category__c FROM Ship_To_Product_Catalog__c WHERE Ship_To__c =: storevisitId GROUP BY Product__r.Product_Category__c];
    }
    
  @AuraEnabled //For the Update fucntion in UpdateStoreVisitInventory component. Takes the List of Product Catalogs compares to the Updated SPIds and updates accordingly
    public static void updateSPCat(List<Ship_To_Product_Catalog__c> SPCat, List<Id> SPId, Id storevisitId ){
        system.debug(SPCat);
        system.debug(SPId);
        List<Ship_To_Product_Catalog__c> upSPCat = new List<Ship_To_Product_Catalog__c>();
        
        for(Ship_To_Product_Catalog__c temp : SPCat){
            system.debug(temp);
            if(SPId.contains(temp.Id)){
                temp.Date_Last_Reported_In_Store__c = date.today();
                upSPCat.add(temp);
            }
        }
        update upSPCat;
    } 
}

 
I've tried two bits of code now and I keep getting the error:

Challenge not yet complete in My Trailhead Playground 3
The 'challengeComplete' method in the 'UnitOfWorkTest' class has not successfully passed all tests. Ensure that you run the tests and it passes successfully before attempting this challenge again.


Here's the code I'm trying to run:

//Code attempt #2
public class UnitOfWorkTest {
    public static void challengeComplete() {
        fflib_SObjectUnitOfWork uow = new fflib_SObjectUnitOfWork(
            new Schema.SObjectType[]{
                Account.SObjectType,
                Contact.SObjectType, 
                Note.SObjectType});
        for (Integer i = 0; i < 100; i++) {
            Account acc = new Account(Name = 'TestAcc' + i);
            uow.registerNew(acc);
            for (Integer j = 0; j < 5; j++) {
                Contact c = new Contact(LastName = 'TestContact' + i + '_' + j);
                uow.registerNew(c, Contact.AccountId, acc);
                Note n = new Note(Title = 'TestNote' + i + '_' + j, Body = 'Test note body.');
                uow.registerNew(n, Note.ParentId, acc);
            }
        }
        uow.commitWork();
        System.assertEquals(100, [SELECT Id from Account].size());
        System.assertEquals(500, [SELECT Id from Contact].size());
        System.assertEquals(500, [SELECT Id From Note].size());
    }
}
/*My Original Code, attempt #1
public class UnitOfWorkTest {
    public static void challengeComplete(){
        //Create the unit of work
        fflib_SObjectUnitOfWork uow = new fflib_SObjectUnitOfWork(
            new Schema.SObjectType[]{
                Account.SObjectType,
                Contact.SObjectType,
                Note.SObjectType
            }
        );
        //Do Some Work!
        for(Integer a=[select id from account].size(); a<100; a++){
            Account acc = new Account();
            acc.Name = 'UoW Test Name' + a;
            uow.registerNew(acc);
            for(Integer c=0; c<5; c++){
                Contact cont = new Contact();
                cont.FirstName = 'James ';
                cont.LastName = acc.name + c;
                uow.registernew(cont, Contact.AccountId, acc);
                Note nt = new Note();
                nt.title = 'UoW Note #' + c;
                nt.Body = 'UoW Body ' + c;
                uow.registernew(nt, Note.ParentId, acc);
            }
        }
        //Commit the work to the database
        uow.commitWork();
        
        System.assertEquals(100, [select id from account].size());
        System.assertEquals(500, [select id from contact].size());
        System.assertEquals(500, [select id from note].size());
    }
}
*/