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
kathybbkathybb 

Illegal Assignment from SObject: Sales_Target_c to SObject Account

I am trying to write a Controller Extension for a custom object (Sales_Targets__c) that is parented by the Standard Account Object.  I am getting the error message above, but I don't understand what is causing it.  I can't see where I am assigning a Sales_Target to Account except where I am trying to assign the Account ID to the relationship field.  Here's the test code:

 

@isTest
private class TestSalesTargetsController {
    static  testMethod void TestSalesTargets() {
    
    Account myAcct = new Account();
        
    ApexPages.StandardController con = new ApexPages.Standardcontroller(myAcct);
    SalesTargetsController ext = new SalesTargetsController(con);
   
    Account testAcctWithTargets= new Account();
      Account testAcctWithNoTargets = new Account();
      Sales_Targets__c testTargetwithData = new Sales_Targets__c();
      Sales_Targets__c testTargetNoData= new Sales_Targets__c();
      List<Account> AcctsToInsert = new List<Account>();
      List<Sales_Targets__c> TargetsToInsert= new List<Sales_Targets__c>();
   
// create Accounts for Testing
    for(integer x = 0;x<25;x++){
        system.debug('Account = ' + testAcctWithTargets);
        testAcctWithTargets = new Account(Name='TestAcctWithTargets' + x);
        AcctsToInsert.add(testAcctWithTargets);
        testAcctWithNoTargets = new Account(Name='TestAcctWithNoTargets' + x);
        AcctsToInsert.add(testAcctWithNoTargets);
    }
        try{
            database.insert(AcctsToInsert);
        }catch (DMLException e) {
            system.debug('Account Insert Failed with exception ' + e);
        }
    
        // query Accounts for SalesTargets
        String accountQuery = 'select id from account where name contains TestAcct LIMIT 25';
        Map<id, Account> MapAcc = new Map<id, Account>((List<Account>)Database.query(accountQuery));
        
        system.debug('Size of Account List = ' + mapAcc.size());
        // Create SalesTargets for Testing  
        for (sObject a : MapAcc){
            a = (Account)a;
        	string aID = a.id;
        	system.debug('aID = '+ aID);
            if(a.name.contains('TestAcctWithTargets')){
               TestAcctWithTargets = new Sales_Targets__c(Target_Name__c='MediaStar',Account__c =aID,
               		Probability__c=10, value__c=1000.00,Year__c='2013');
               TargetsToInsert.add(TestAcctWithTargets);
               TestAcctWithTargets = new Sales_Targets__c(Target_Name__c='CME', Account__c = aID,
                     Probability__c=20, value__c=100.00,Year__c='2013');
               TargetsToInsert.add(TestAcctWithTargets);
               TestAcctWithTargets = new Sales_Targets__c(Target_Name__c='Guide', Account__c = aID,
                   Probability__c=10, value__c=1500.00,Year__c='2013');
               TargetsToInsert.add(TestAcctWithTargets);
               TestAcctWithTargets = new Sales_Targets__c(Target_Name__c='CMS', Account__c = aID,
                    Probability__c=50, value__c = 2000.00, Year__c = '2013');
               TargetsToInsert.add(TestAcctWithTargets);
				TestAcctWithTargets = new Sales_Targets__c(Target_Name__c='PSIP', Account__c = aID,
                    Probability__c=30, value__c = 2000.00, Year__c = '2013');
               TargetsToInsert.add(TestAcctWithTargets);
                     
         }
         try{
            database.insert(TargetsToInsert);
        }catch (DMLException e) {
            system.debug('Target Insert Failed with exception ' + e);
        }    
           
    }
}
}

 and here is the Controller extension:

 

public class SalesTargetsController {
    private ApexPages.StandardController con {get;set;}
    private Account a;
    public List<Sales_Targets__c> targets {get; set;}
    
    public SalesTargetsController(ApexPages.StandardController con){
        this.con = con;
        this.a =(Account)con.getRecord();
    }
    public PageReference display() {
        if(targets == null){
            targets = new List<Sales_Targets__c>();
        } else {
            targets.clear();
        }
 
        string qry = 'Select t.id, t.target_name_c, t.probability__c, t.value__c, t.Contacted__c, t.no_sale_reason__c from Sales_Targets__c t where AccountID = a.Id Order by t.id';
        targets=database.query(qry);
        return null;
    }
}

If someone can tell me what I'm doing wrong, I'd be eternally grateful.  The purpose is to create a page that can be inserted into the account detail section.

 

Thanks in advance,

 

Naidu PothiniNaidu Pothini
@isTest
private class TestSalesTargetsController {
    static  testMethod void TestSalesTargets() {
    
    Account myAcct = new Account();
        
    ApexPages.StandardController con = new ApexPages.Standardcontroller(myAcct);
    SalesTargetsController ext = new SalesTargetsController(con);
   
    Account testAcctWithTargets= new Account();
      Account testAcctWithNoTargets = new Account();
      Sales_Targets__c testTargetwithData = new Sales_Targets__c();
      Sales_Targets__c testTargetNoData= new Sales_Targets__c();
      List<Account> AcctsToInsert = new List<Account>();
      List<Sales_Targets__c> TargetsToInsert= new List<Sales_Targets__c>();
   
// create Accounts for Testing
    for(integer x = 0;x<25;x++){
        system.debug('Account = ' + testAcctWithTargets);
        testAcctWithTargets = new Account(Name='TestAcctWithTargets' + x);
        AcctsToInsert.add(testAcctWithTargets);
        testAcctWithNoTargets = new Account(Name='TestAcctWithNoTargets' + x);
        AcctsToInsert.add(testAcctWithNoTargets);
    }
        try{
            database.insert(AcctsToInsert);
        }catch (DMLException e) {
            system.debug('Account Insert Failed with exception ' + e);
        }
    
        // query Accounts for SalesTargets
        String accountQuery = 'select id from account where name contains TestAcct LIMIT 25';
        Map<id, Account> MapAcc = new Map<id, Account>((List<Account>)Database.query(accountQuery));
        
        system.debug('Size of Account List = ' + mapAcc.size());
        // Create SalesTargets for Testing  
        for (sObject a : MapAcc){
            a = (Account)a;
        	string aID = a.id;
        	system.debug('aID = '+ aID);
            if(a.name.contains('TestAcctWithTargets'))
            {
               Sales_Targets__c st1 = new Sales_Targets__c(Target_Name__c='MediaStar',Account__c =aID, Probability__c=10, value__c=1000.00,Year__c='2013');
               Sales_Targets__c st2 = new Sales_Targets__c(Target_Name__c='CME', Account__c = aID, Probability__c=20, value__c=100.00,Year__c='2013');
               Sales_Targets__c st3 = new Sales_Targets__c(Target_Name__c='Guide', Account__c = aID, Probability__c=10, value__c=1500.00,Year__c='2013');
               Sales_Targets__c st4 = new Sales_Targets__c(Target_Name__c='CMS', Account__c = aID, Probability__c=50, value__c = 2000.00, Year__c = '2013');
               Sales_Targets__c st5 = new Sales_Targets__c(Target_Name__c='PSIP', Account__c = aID,Probability__c=30, value__c = 2000.00, Year__c = '2013');

               TargetsToInsert.add(st1);
               TargetsToInsert.add(st2);
               TargetsToInsert.add(st3);
               TargetsToInsert.add(st4);
               TargetsToInsert.add(st5);
                     
         }
         try{
            database.insert(TargetsToInsert);
        }catch (DMLException e) {
            system.debug('Target Insert Failed with exception ' + e);
        }    
           
    }
}
}