You need to sign in to do that
Don't have an account?

do my code covered governor limit
I have a parent object and child object and am updating the sum of child records to a field in parent
do my below code cover governor limit please advise.
do my below code cover governor limit please advise.
trigger quantityleft on Sample_Transaction__c (after insert,after update,after delete,after undelete) { List<Sample_Lot__c> list_Account= new List<Sample_Lot__c>(); set<Id> set_Opportunity = new set<Id>(); if(Trigger.isInsert || Trigger.isUndelete){ for(Sample_Transaction__c objOpp: trigger.new){ if(objOpp.Completed_Transaction__c == true){ set_Opportunity.add(objOpp.Parent_id__c); } } } if(Trigger.isUpdate){ for(Sample_Transaction__c objOpp: trigger.new){ if(objOpp.Completed_Transaction__c == true){ set_Opportunity.add(objOpp.Parent_id__c); } } } if(Trigger.isdelete){ for(Sample_Transaction__c objOpp: trigger.old){ if(objOpp.Completed_Transaction__c == true){ set_Opportunity.add(objOpp.Parent_id__c); } } } if(!set_Opportunity.isEmpty()){ Decimal Sum; Sum=0; for(Sample_Transaction__c sample_transaction : [SELECT id,Sample_Lot_ID__c, Transaction_Quantity__c FROM Sample_Transaction__c WHERE Parent_id__c = : set_Opportunity ]){ Sum+= sample_transaction.Transaction_Quantity__c; for(Sample_Lot__c objAccount : [SELECT Id, Quantity_Left__c FROM Sample_Lot__c WHERE Id = : sample_transaction.Sample_Lot_ID__c]){ objAccount.Quantity_left__c = Sum ; list_Account.add(objAccount); update objAccount ; } } } }
Here is the corrected code -
Thanks for modify the code i got below error in line no 75 , any idea for this error, my previous code didnt
through this error
Apex trigger quantityleft1 caused an unexpected exception, contact your administrator: quantityleft1: execution of AfterUpdate caused by: System.ListException: Duplicate id in list: a0h0E0000004ZVlQAM: Trigger.quantityleft1: line 75, column 1
I solved the issue , i have only one sample lot and modfied the code to get only one sample lot it solved the issue thanks a lot for your pateint and help am new to code , please advise me how to start test class .
I have one more interesting in test class i will post it after you look this below coce
I may be not mentioned the requirement clearly to you my apologies.
Could you please get me the screenshot from the Schema Builder after selecting these 2 objects?
Apart from checking the code , could you please advise me about test case i wrote the below test class and when i run it shows it covered 82% but am not convinced, the trigger code has after update,after insert, after delete and after undelte , in all i just getting the id of child and parent then i am collectiing child record then getting sum and then updateing the parent field.
in below test class i just inserted what are the record need for the code but it shows 82% code coverage , for prod move this is fine but my query is how come it covered 82& i didnt do anything for after update after delete undelete and also didnt get sum and update
could you please advise me
thanks
soma
- Insert assert statements
- You have to test for both positive as well as negative cases
- All the lines in the Apex code should be coverd in proper manner
- Create a common Test Factory method to create records in the test class
Also please refer this link to know more about writing test classes - https://developer.salesforce.com/page/How_to_Write_Good_Unit_Tests