+ Start a Discussion
chikpeachikpea 

Change in Apex coding standard

Hi,

 

I worked on Apex code more than 1 year before, developed some triggers and packages. Now I am finding that all those coding standards have been changed completely.

 

I can't compile my old trigger code, and Apex doesn't provide any correction hints. I am providing the code here, if someone can just validate, would be very helpful otherwise I am bit lost.

 

trigger getOrderLines on Service_Order__c (before insert, after insert) {
Double discount = 0;

if (Trigger.new.oss__quote__c != null) {
  if (Trigger.isBefore) {
     Trigger.new.total_plan_rate__c = [select oss__quote__c.oss__total_plan_rate__c from oss__quote__c where id = :Trigger.new.oss__quote__c].oss__total_plan_rate__c;

       try {              
                  discount = [select oss__quote__c.oss__charge_discount__c from oss__quote__c where id = :Trigger.new.oss__quote__c].oss__charge_discount__c;
        } catch (Exception e) { discount = 0; }

      Trigger.new.oss__total_charges__c = [select oss__quote__c.oss__total_charges__c from oss__quote__c where id = :Trigger.new.oss__quote__c].oss__total_charges__c - discount;

      Trigger.new.oss__total_equipment_rental__c = [select oss__quote__c.oss__total_equipment_rental__c from oss__quote__c where id = :Trigger.new.oss__quote__c].oss__total_equipment_rental__c;
}

  if (Trigger.isAfter)
       util.copyLineItemsFromQuote(Trigger.new.clone());
}
}

This code was compiled before, now I cant. If there is any document, which can show, what are the things have been changed, that would be helpful.

 

Thanks in advance!

Bhaskar

www.chikpea.com

Scott.MScott.M

I think the problem your having is that trigger.new is a list so you have to put in a loop like the following

 

 

for(Service_Order__c so: trigger.new) { }

 

I believe the reason is that there's no gaurentee that only one object will be passed to the trigger. It could be a batch that needs to be processed all at the same time

 

 

 

mikefmikef

Yes the coding standard did change, it looks like your trigger was created when the key word "bulk" was being used.

Now every trigger is run in bulk and there are no non-bulk triggers.


Please see this post for an example of bulk operations.

 

Bottom line is a bulk trigger doesn't understand things like,

 

trigger.new.fieldName = someValue;

 

You will need to update your trigger to run in bulk fashion.