I am very new to both programming and Apex. I have written one Trigger on account to update contact SLA fields whenever the SLA is updated as 'Bronze' on account. My code coverage is 100%. Please advise if this code can be improved further and optimised as I have written nested for loops.
trigger MySLA on Account (after Update) { if(Trigger.isAfter) { for(account a:Trigger.new) { for(account ao:Trigger.old) { if(a.SLA__c == 'Bronze' && ao.SLA__c != 'Bronze') { list<contact> conlist = new list<contact>(); list<Contact> cons = [select SLA_Serial_Number__c,SLA__c,SLA_Expiration_Date__c from contact where accountid IN :Trigger.new]; for(contact c:cons) { c.SLA__c = 'Bronze'; c.SLA_Expiration_Date__c = a.SLAExpirationDate__c; c.SLA_Serial_Number__c = a.SLASerialNumber__c; conlist.add(c); } update conlist; } } } } } @isTest public class testSLAupdate { private static testmethod void testSLAContactUpdate() { account a = new account(name='abc', SLA__c='Gold'); insert a; contact c = new contact(lastname='Smith',firstname='John',accountid=a.id); insert c; a.SLA__c = 'Bronze'; update a; system.assertEquals('Bronze', c.SLA__c); System.assertEquals(a.SLAExpirationDate__c, c.SLA_Expiration_Date__c); System.assertEquals(a.SLASerialNumber__c, c.SLA_Serial_Number__c); } }