+ Start a Discussion
cribiscribis 

Error: Compile Error: Illegal assignment from SOBJECT:Customer_Product__c to Id at line 11 column 9

I am writing a test class and am running into this error:

Error: Compile Error: Illegal assignment from SOBJECT:Customer_Product__c to Id at line 11 column 9

 Here is my test class:

@isTest
private class BulkDensityTest{
    testmethod private static void TestTrigger() {
        Customer_Product__c cp = new Customer_Product__c
            (name = 'apple');
        insert cp;
        
        Customer_Bulk_Density__c  c = new Customer_Bulk_Density__c ();
        c.Name = '4';
        c.Metric_Label__c = 'kg/m3';
        c.Customer_Product__c = cp;
        insert c;
        
        System.debug('c:' + c);
        c = [SELECT ID, Name FROM Customer_Bulk_Density__c  WHERE id = :c.id];
        System.debug('c:' + c);
        System.assertEquals(c.Customer_Product__c, c.Name);
        Profile p = [select id from profile where name='Standard User'];

    User u = new User(alias = 'standt', email='standarduser@testorg.com',
emailencodingkey='UTF-8', lastname='Testing', languagelocalekey='en_US',
localesidkey='en_US', profileid = p.Id,
timezonesidkey='America/Los_Angeles', username='standarduser@testorg.com');
System.runAs(u) {
// The following code runs as user 'u'
System.debug('Current User: ' + UserInfo.getUserName());
System.debug('Current Profile: ' + UserInfo.getProfileId()); }
}
     

}

 

Here is the trigger:

 

trigger BulkDensityConversion on Customer_Bulk_Density__c (before insert) {
    Set<Id> bIds = new Set<Id>();

list<Customer_Bulk_Density__c> customerBulkDensityList = new List<Customer_Bulk_Density__c>();

if(StaticClass.doNotExecute ==true)
{
system.debug('Inserting'+StaticClass.doNotExecute);
    for(Customer_Bulk_Density__c c:trigger.new)
    {
     bIds.add(c.id);
     
     if(c.Metric_Label__c =='Lbs./ft3')
     {
     Double conversion = Double.valueof(c.Name) * 16.018;
         Long L1 = conversion.round();
        
      Customer_Bulk_Density__c s = new Customer_Bulk_Density__c(
        Metric_Label__c = 'Kg/m3',
        Name = String.Valueof(L1) + String.ValueOf(' ') + String.Valueof('Kg/m3'),
        Customer_Product__c=c.Customer_Product__c,
        Customer_Product_2__c=c.Customer_Product_2__c,
        Customer_Product_3__c=c.Customer_Product_3__c
        );
        customerBulkDensityList.add(s);   
        }
         else
        {
         Double conversion1 = Double.valueof(c.Name) * 1 / 16.018;
              Long L2 = conversion1.round();        
              
         Customer_Bulk_Density__c s = new Customer_Bulk_Density__c(
         
        Metric_Label__c = 'Lbs/ft3',
        Name = String.Valueof(L2) + String.ValueOf(' ') + String.Valueof('Lbs./ft3'),
        Customer_Product__c=c.Customer_Product__c,
        Customer_Product_2__c=c.Customer_Product_2__c,
        Customer_Product_3__c=c.Customer_Product_3__c
        
        );
        customerBulkDensityList.add(s);   
        } 
    }
    StaticClass.doNotExecute =false;
    if(!customerBulkDensityList.isEmpty())
    {
        insert customerBulkDensityList;
    }
    
    System.debug('****1 : B Id size '+ bIds.size());
 
  }
  else
  {
  StaticClass.doNotExecute =true;
  }

  }

 

How to I solve the error?

 

Thank you again

Best Answer chosen by Admin (Salesforce Developers) 
rscottrscott

c.Customer_Product__c = cp; is the issue.

 

You are trying to set an Id (the c.Customer_Product__c field) to a full sObject (cp).

 

Try

 

c.Customer_Product__c = cp.Id;