You need to sign in to do that
Don't have an account?
pvande
Help with Apex Class Coverage (please)
I am new to Apex trigger/class coding. I was able to create a simple trigger that works, but I am having trouble with code coverage and can't roll it to the production environment. I would appreciate any help with Apex Class coding. (or could you point me toward a tutorial?) Thank you in advance.
My trigger is as follows:
trigger WorkstreamStamp on Project__c (before insert) {
For(Project__c p : Trigger.New)
{
If(p.Workstream__c == null)
{
List<Account> incomingprojinfo = [Select Workstream__c FROM Account WHERE Id =:p.Sponsor_Account_Ultimate_Parent__c LIMIT 1];
{
p.Workstream__c = incomingprojinfo[0].Workstream__c;
}
}
}
}
My trigger is as follows:
trigger WorkstreamStamp on Project__c (before insert) {
For(Project__c p : Trigger.New)
{
If(p.Workstream__c == null)
{
List<Account> incomingprojinfo = [Select Workstream__c FROM Account WHERE Id =:p.Sponsor_Account_Ultimate_Parent__c LIMIT 1];
{
p.Workstream__c = incomingprojinfo[0].Workstream__c;
}
}
}
}
To have "code coverage" means writing unit tests that exercise the production code. The code that is "exercised" is "covered" by the tests, whilst the code that isn't run by the tests is outside this coverage.
Unit tests are classes in your code base with the @IsTest annotation against them containing methods also marked as @IsTest (or using the deprecated "testMethod" keyword).
In this specific case, to exercise your trigger you need to write a test that causes the trigger to be invoked. That means, here, that your test must create and insert/upsert new Project__c instances. The tests do this by creating various Project__c instances with Workstream__c set to a value and and/or set as null. In both cases the tests should assert that the Workstream__c field is set appropriately after the insert/upsert call.
To get to grips with unit testing I recommend you start at https://developer.salesforce.com/page/How_to_Write_Good_Unit_Tests and go from there.
Phil
It should cover all your lines code.
Is there any work aroud for this situation?
Thank you again.
Formula field name Total_Amount__c : Amount * 2
Then you just need to insert Amount field on your test class:
Amount = 50000