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
Dipil JainDipil Jain 

Getting Save error: Illegal assignment from LIST<Asset> to SOBJECT:Order_Line_Item__c

Hi All,

i'm new to apex code. I'm trying to update the fields Pending_Activation_Date__c and Pending_Deactivation_Date__c on the Order__c object using a trigger. my Asset object has a reference to Order_Line_Item__c object which in-turn has a reference to Order__c object. the trigger is firing on update of Asset Object. below is the trigger code from eclipse IDE.

trigger UpdateOrder on Asset (Before Update) {
    Order_Line_Item__c tempOrderLine;
    Order__c tempOrder, OrderToUpdate;
   
if(trigger.isBefore)
    {
     System.debug('Inside UpdateOrder');
        //It will update the pending Activation and pending deactivation picklists on order object
        for(Asset astObj : trigger.new)
        {
         System.debug('Processing Asset Id '+astObj.id);
         tempOrderLine = [select order_line_item__r.id from Asset where Asset.Id =: astObj.Id limit 1];
         System.debug('Corresponding Order Line Item '+tempOrderLine.Name);
         tempOrder= [select order__r.id from Order_Line_Item__c where id =: tempOrderLine.id limit 1];
         System.debug('Corresponding Order '+tempOrder.Name);
         OrderToUpdate = [select Pending_Activation_Dates__c, Pending_Deactivation_Dates__c from Order__c where order__c.id=:tempOrder.Id limit 1];
         if(astObj.ActivationDate__c!=null){
          tempOrder.Pending_Activation_Dates__c='M6 Effective From Dates';
         }
         if(astObj.UsageEndDate!=null){
          tempOrder.Pending_Deactivation_Dates__c='M6 Effective To Dates';
         }
        }
        if(OrderToUpdate.Id!=null){
         update OrderToUpdate;
        }
       
    }
}

but Eclipse is keep showing error of Save error: Illegal assignment from LIST<Asset> to SOBJECT:Order_Line_Item__c
on this line.
tempOrderLine = [select order_line_item__r.id from Asset where Asset.Id =: astObj.Id limit 1];

now Since this line is inside the for loop, the asset object should not be a list, it can always be a single object from the list of Asset objects from Trigger. not sure why this error is coming. can you please help me... also this trigger is firing but no lines are executing. below is the debug log for its firing sequence.

01:07:41.140 (140215573)|EXECUTION_STARTED
01:07:41.140 (140248778)|CODE_UNIT_STARTED|[EXTERNAL]|TRIGGERS
01:07:41.140 (140279864)|CODE_UNIT_STARTED|[EXTERNAL]|01qL00000004Y3K|UpdateOrder on Asset trigger event BeforeUpdate for [02iL00000003pZF]
01:07:41.010 (141930232)|CUMULATIVE_LIMIT_USAGE
01:07:41.010|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 0 out of 100
  Number of query rows: 0 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 0 out of 150
  Number of DML rows: 0 out of 10000
  Maximum CPU time: 0 out of 10000
  Maximum heap size: 0 out of 6000000
  Number of callouts: 0 out of 10
  Number of Email Invocations: 0 out of 10
  Number of fields describes: 0 out of 100
  Number of record type describes: 0 out of 100
  Number of child relationships describes: 0 out of 100
  Number of picklist describes: 0 out of 100
  Number of future calls: 0 out of 10

01:07:41.010|CUMULATIVE_LIMIT_USAGE_END

01:07:41.142 (142006686)|CODE_UNIT_FINISHED|UpdateOrder on Asset trigger event BeforeUpdate for [02iL00000003pZF]

Your help is greatly appreciated. Thanks!

Regards,
Dipil Jain
Cloud_forceCloud_force
you are trying to put list type of data in a object type of data. 
folowig line would not work although you are using limit 1:
tempOrderLine = [select order_line_item__r.id from Asset where Asset.Id =: astObj.Id limit 1];
this will return you list and not object.

thanks,
http://www.forcexplore.com
Bhushan.AdhikariBhushan.Adhikari
Hi dilip,

You have declare tempOrderLine of type Order_Line_Item__c and you are querying on Assets that will return assets records. 
More over looking at your code, you are at a danger of hitting govener limits easily. Avoid use of  SOQL in side for loop, try mofdifying your code.
Dipil JainDipil Jain
Thanks SFDC_WAVE and BHUSHAN for quickly replying to my query. I agree with you Bhushan.. but like I said, i am new to SFDC Apex code and not sure how else this code can be written? should i just hardcode the objects... like instead of for lopp on trigger... should i just user Trigger.new.get(0) to get the corresponding asset object? thank you for your kind help.
Dipil JainDipil Jain
here's the updated code...

trigger UpdateOrder on Asset (Before Update) {
list<Asset> tempAssets;
    list<Order_Line_Item__c> tempOrderLines;
    list<Order__c> OrdersToUpdate;
   
if(trigger.isBefore)
    {
     System.debug('Inside UpdateOrder');
        //It will update the pending Activation and pending deactivation picklists on order object
        for(Asset astObj : trigger.new)
        {
        
         tempAssets= [select order_line_item__r.id, Name from Asset where Asset.Id =: astObj.Id limit 1];
         System.debug('Processing Asset Id '+tempAssets.get(0).Name);
        
         tempOrderLines = [select order__r.id, Name from Order_Line_Item__c where id =: tempAssets.get(0).order_line_item__r.id limit 1];
         System.debug('Corresponding Order Line Item '+tempOrderLines.get(0).Name);
         OrdersToUpdate = [select Pending_Activation_Dates__c, Pending_Deactivation_Dates__c, Name from Order__c where order__c.id=:tempOrderLines.get(0).order__r.id limit 1];
         System.debug('Corresponding Order '+OrdersToUpdate.get(0).Name);
         if(astObj.ActivationDate__c!=null){
          OrdersToUpdate.get(0).Pending_Activation_Dates__c='M6 Effective From Dates';
         }
         if(astObj.UsageEndDate!=null){
          OrdersToUpdate.get(0).Pending_Deactivation_Dates__c='M6 Effective To Dates';
         }
        }
        if(OrdersToUpdate.get(0)!=null && OrdersToUpdate.get(0).Id!=null){
         update OrdersToUpdate;
        }
       
    }
}
Vinit_KumarVinit_Kumar
I see many issues here in your code which I am highlighting below :-

1.) You are running your SOQL inside FOR loop which is not recommended and against the best practices of salesforce.Move your SOQL outside FOR loop.

2.) You are performing your DML inside FOR loop which is again not recommended and against the best practices of salesforce.Move your DML outside FOR loop.

3.) Your code is not bulkified as you are using tempAssets.get(0).order_line_item__r.id which would only for one record if you are doing bulk operations instead you should be populating the records in the list and iterate over list.

Go ahead and fix these errors.