You need to sign in to do that
Don't have an account?
Lil_Ranger1111
Required Field Missing Test Class?
I created a trigger to prevent the deletion of a parent record if a child record exists. I'm having problems with the test class. It says required field missing "Dept_Code__c". I have it in the test class, so I'm not sure what I'm missing.
Parent Object - Order_Request__c (Quantity, Cost Center (lookup), Order Type (lookup) are required fields.
Child Object - Order_Assignment__c (Order Request, Order Center (lookup) are required fields)
Any help is greatly appreciated.
Parent Object - Order_Request__c (Quantity, Cost Center (lookup), Order Type (lookup) are required fields.
Child Object - Order_Assignment__c (Order Request, Order Center (lookup) are required fields)
Any help is greatly appreciated.
trigger PreventOrderDeletion on Order_Request__c (before delete) { for(Order_Request__c lq : [Select Id, Name, (Select Id from Order_Assignments__r) from Order_Request__c where Id IN :trigger.oldMap.KeySet()]){ if(lq.Order_Assignments__r != null && lq.Order_Assignments__r.size() > 0){ lq.addError('The order request cannot be deleted because there is an associated order assignment record'); } } @IsTest(SeeAllData=true) public class PreventOrderDeletionTest{ static testMethod void PreventDeleteTest() { //Create Parent Object Order_Request__c lic = new Order_Request__c(Name='Salesforce Testing'); lic.Quantity__c = Decimal.valueOf('1.0'); lic.Order_Type__c = 'a6xxxxxxxxxxxx'; lic.Dep_Code__c = 'a6Oxxxxxxxxxxx'; insert lic; //Start Test & create child object test.startTest(); Order_Assignment__c asgn = new Order_Assignment__c(); //Relate Child to the Parent from the inserted record above asgn.Order_Request__c = lic.Id; asgn.Dep_Code__c = lic.Default_Code__c; //Insert Child Record insert asgn; try { delete lic; } catch (Exception e) { system.assertEquals('You cannot delete this record!', e.getMessage()); } //Stop Test test.stopTest(); } }
Where does the values for the lic.Default_Code__c comes from?
Can you check to see if that value is not empty?
Regards,
Ana Catarina Brites
avoid hardcoding k
i.e entering id values
I had an error in my posting. The parent record should have (Quantity__c, Dept_code__c (lookup), and order_type__c(lookup) which are all required on the parent object creation.
How do I avoid using IDs, but populate those required lookup values?
Thank you.
I did some updates and recieved 100% code coverage.
When I tested the code in the developer console, I received this error: (I'm assuming this is expected)
System.AssertException: Assertion Failed: Expected: You cannot delete this record!, Actual: Delete failed. First exception on row 0 with id a6Q110000008OLyEAM; first error: DELETE_FAILED, Your attempt to delete Testing could not be completed because it is associated with the following order assignments.: LA-000026