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
cbrocbro 

Test Class Compile Errors (string to decimal) & (string to SOBJECT)

I am having errors with compiling in my Test Class. 

 

I have commented out the parts that the errors were occuring below (and put them in bold/underlined) - and now I have 92% code coverage, but I just want to know how I can fix this in the future.  

 

The two errors I am receiving are:

  • Error: Compile Error: Illegal assignment from String to SOBJECT:Product2 at line 61 column 9
  • Error: Compile Error: Illegal assignment from String to Decimal at line 63 column 9

Any help would be greatly appreciated!


Thanks!

Chris

 

 

Here is the code:

 

@isTest
private class AssetTest {
//Classes always have to be uppercase
    static testMethod void testContractLine()
    {      
        Profile pr = [select id from profile where name=: 'System Administrator'];
        User u = new User(alias = 'standt', email='standarduser@vauto.com',emailencodingkey='UTF-8', lastname='Testing', languagelocalekey='en_US',localesidkey='en_US', profileid = pr.Id,timezonesidkey='America/Los_Angeles', username='testemail1@vauto.com');
        insert u;
        System.runAs(u)
        {
            Account a = setupAccount();
            Contact co = setupContact();
            Opportunity o = setupOpportunity(a,co);
            Contract c = setupContract(o,a);
            Asset ae = setupAsset(a,c);
            
            Test.startTest();
                ae.Status = 'Active';
                update ae;
                
                List <Contract_Line__c> contractLines = [select Id from Contract_Line__c where Contract__c =: c.Id];
                
                //System.assertEquals(contractLines.size(),1); 
                
            Test.stopTest();
        }
    
    }
    //THE FOLLOWING IS OUR TEST DATA void means we're not returning anything
    static Account setupAccount(){ 
        Account a = new Account();
        a.Name = 'test';
        insert a;
        return a;
    }
    
    static Opportunity setupOpportunity(Account a, Contact c){
        Opportunity o = new Opportunity ();
        o.Name = 'test'; 
        o.CloseDate = System.today();
        o.StageName = 'Prospecting';
        o.AccountId = a.Id;
        o.Syndication_Primary_Contact_2__c = c.Id;
        insert o;
        return o;
    
    }
    
    static Contract setupContract(Opportunity o, Account a){
        Contract c = new Contract();
        c.Name = 'test';
        c.StartDate = System.today();
        c.Opportunity__c = o.Id;
        c.AccountId = a.Id;
        insert c;
        return c;
    }
    
    static Asset setupAsset(Account a, Contract c){
        Asset ae = new Asset();
        
//ae.Product2 = '30 Day Deposit';

 

        ae.Name = '30 Day Deposit';

        //ae.Price = '1400.00';

        ae.PurchaseDate = System.today();
        ae.InstallDate = System.today();
        ae.Contract__c = c.Id;
        ae.AccountId = a.Id;
        insert ae;
        return ae;
    
    }
    
    static Contact setupContact(){
        Contact co = new Contact();
        co.FirstName = 'Test';
        co.LastName = 'McGhee';
        insert co;
        return co;
    }



}

Best Answer chosen by Admin (Salesforce Developers) 
EnthEnth

Hi Chris,

 

        //ae.Product2 = '30 Day Deposit';

is an object reference not a string. You need to find the Product2 record Id for your 30 day deposit product and set the value of ae.Product2Id to the corresponding Product2.Id  

 

        //ae.Price = '1400.00';

 

should be 

        ae.Price =  1400.00;

 

 

 

All Answers

EnthEnth

Hi Chris,

 

        //ae.Product2 = '30 Day Deposit';

is an object reference not a string. You need to find the Product2 record Id for your 30 day deposit product and set the value of ae.Product2Id to the corresponding Product2.Id  

 

        //ae.Price = '1400.00';

 

should be 

        ae.Price =  1400.00;

 

 

 

This was selected as the best answer
cbrocbro

 

2nd question - can anyone tell me what the line below in bold/underlined is trying to do?  I have commented it out, as it was making the test fail... 

 

 

 

 

Thanks!

 

Here is the updated code, which now compiles:

 

@isTest
private class AssetTest {
//Classes always have to be uppercase
    static testMethod void testContractLine()
    {      
        Profile pr = [select id from profile where name=: 'System Administrator'];
        User u = new User(alias = 'standt', email='standarduser@vauto.com',emailencodingkey='UTF-8', lastname='Testing', languagelocalekey='en_US',localesidkey='en_US', profileid = pr.Id,timezonesidkey='America/Los_Angeles', username='testemail1@vauto.com');
        insert u;
        System.runAs(u)
        {
            Account a = setupAccount();
            Contact co = setupContact();
            Opportunity o = setupOpportunity(a,co);
            Contract c = setupContract(o,a);
            Asset ae = setupAsset(a,c);
            
            Test.startTest();
                ae.Status = 'Active';
                update ae;
                
                List <Contract_Line__c> contractLines = [select Id from Contract_Line__c where Contract__c =: c.Id];
                
                //System.assertEquals(contractLines.size(),1); 
                
            Test.stopTest();
        }
    
    }
    //THE FOLLOWING IS OUR TEST DATA void means we're not returning anything
    static Account setupAccount(){ 
        Account a = new Account();
        a.Name = 'test';
        insert a;
        return a;
    }
    
    static Opportunity setupOpportunity(Account a, Contact c){
        Opportunity o = new Opportunity ();
        o.Name = 'test'; 
        o.CloseDate = System.today();
        o.StageName = 'Prospecting';
        o.AccountId = a.Id;
        o.Syndication_Primary_Contact_2__c = c.Id;
        insert o;
        return o;
    
    }
    
    static Contract setupContract(Opportunity o, Account a){
        Contract c = new Contract();
        c.Name = 'test';
        c.StartDate = System.today();
        c.Opportunity__c = o.Id;
        c.AccountId = a.Id;
        insert c;
        return c;
    }
    
    static Asset setupAsset(Account a, Contract c){
        Asset ae = new Asset();
        ae.Product2Id = '01tV0000000gY2n';
        ae.Name = '30 Day Deposit';
        ae.Price = 1400.00;
        ae.PurchaseDate = System.today();
        ae.InstallDate = System.today();
        ae.Contract__c = c.Id;
        ae.AccountId = a.Id;
        insert ae;
        return ae;
    
    }
    
    static Contact setupContact(){
        Contact co = new Contact();
        co.FirstName = 'Test';
        co.LastName = 'McGhee';
        insert co;
        return co;
    }


}

Vinit_KumarVinit_Kumar

Agreed with Enth,

 

//ae.Price = '1400.00'; should be

 

ae.Price = 1400.00; //without the single quotes as Price field is of Double Datatype

 

//ae.Product2 = '30 Day Deposit'; should be 

 

As you are assigning a value it should be assigned to a Field and there is no field as Product2(Infact it is an object),you should be checking on what field of that object you want to populate it.

 

You need to check the API documentation for Asset object to get on which field you want to assign that value,please go through the below link for the same:-

 

http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_objects_asset.htm

Vinit_KumarVinit_Kumar

//System.assertEquals(contractLines.size(),1); 

 

is checking whether contractLines.size() is equal to ! or not,if it then it would return True else false.Basically,it is comparing the 2 parameters.

 

You can go through the below link for the same :-

 

http://th3silverlining.com/2010/09/07/salesforce-system-assert-vs-system-assertequals/

cbrocbro
thanks for the info, Vinit!
llisallisa
Hello, 
i am getting
" Error: Compile Error: Illegal assignment from String to Decimal ."  while importing data from a csv file in Apex Class.
To avoid this error i am using "decimal.valueof(csvRecordData[1]);"
it gives the reverse error " Error: Compile Error: Illegal assignment from Decimal to String ".

public void importCSVFile(){
       try{
           csvAsString = csvFileBody.toString();
           csvFileLines = csvAsString.split('\n'); 
            
           for(Integer i=1;i<csvFileLines.size();i++){
               Distributor_Prior_Sales__c prisale = new Distributor_Prior_Sales__c() ;
               string[] csvRecordData = csvFileLines[i].split(',');
               prisale.SMS_Year__c= csvRecordData[0] ;             
               prisale.SMS_sales_amount__c= csvRecordData[1];
               prisale.Exhibit_A_Customer__c= csvRecordData[2];
               prisale.Calculation_Type__c= csvRecordData[3];                                                                        
               accon.add(prisale);   
           }
        //insert acclist;
        }
        catch (Exception e)
        {
            ApexPages.Message errorMessage = new ApexPages.Message(ApexPages.severity.ERROR,'An error has occured while importing data Please make sure input csv file is correct');
            ApexPages.addMessage(errorMessage);
        }  
  }