You need to sign in to do that
Don't have an account?
faris ramadhan 12
entitlement implementation test class only cover 50% of its util class
Dear Developer Community,
I have a case where i need to create an auto complete milestone on case. I took example from this documentation http://resources.docs.salesforce.com/latest/latest/en-us/sfdc/pdf/salesforce_entitlements_implementation_guide.pdf page 29 - 33. Everything is fine but then problem occured when the test unit provided in the guide only cover 50% of its util class.
I found this exact same question on the stackexchange https://salesforce.stackexchange.com/questions/141607/57-code-coverage-on-milestone-complete/141624 but still I'm confused on how to implement it in code
I'm breaking my head to solve this, please help. Any kind will be very appreciated
I have a case where i need to create an auto complete milestone on case. I took example from this documentation http://resources.docs.salesforce.com/latest/latest/en-us/sfdc/pdf/salesforce_entitlements_implementation_guide.pdf page 29 - 33. Everything is fine but then problem occured when the test unit provided in the guide only cover 50% of its util class.
I found this exact same question on the stackexchange https://salesforce.stackexchange.com/questions/141607/57-code-coverage-on-milestone-complete/141624 but still I'm confused on how to implement it in code
I'm breaking my head to solve this, please help. Any kind will be very appreciated
<pre>
public class MilestoneUtils
{
public static void completeMilestone( List caseIds, String milestoneName, DateTime complDate )
{
List<CaseMilestone> cmsToUpdate =
[ SELECT Id, completionDate
FROM CaseMilestone
WHERE ( CaseId in :caseIds
AND MilestoneType.Name = :milestoneName
AND CompletionDate = null
)
LIMIT 1
];
for ( CaseMilestone cm : cmsToUpdate ) cm.completionDate = complDate;
update cmsToUpdate; // no error or penalty to DML empty list
}
}
</pre>
If you want to be hard core, this can be done in a single line. Don't stress about the update in a for loop, the query is LIMIT 1, so you'll never have more than one update.
<pre>
public class MilestoneUtils
{
public static void completeMilestone( List caseIds, String milestoneName, DateTime complDate )
{
for ( CaseMilestone cm :
[ SELECT Id, completionDate
FROM CaseMilestone
WHERE ( CaseId in :caseIds
AND MilestoneType.Name = :milestoneName
AND CompletionDate = null
)
LIMIT 1
]
) update new CaseMilestone( Id = cm.Id, CompletionDate = complDate );
}
}
</pre>
All Answers
<pre>
public class MilestoneUtils
{
public static void completeMilestone( List caseIds, String milestoneName, DateTime complDate )
{
List<CaseMilestone> cmsToUpdate =
[ SELECT Id, completionDate
FROM CaseMilestone
WHERE ( CaseId in :caseIds
AND MilestoneType.Name = :milestoneName
AND CompletionDate = null
)
LIMIT 1
];
for ( CaseMilestone cm : cmsToUpdate ) cm.completionDate = complDate;
update cmsToUpdate; // no error or penalty to DML empty list
}
}
</pre>
If you want to be hard core, this can be done in a single line. Don't stress about the update in a for loop, the query is LIMIT 1, so you'll never have more than one update.
<pre>
public class MilestoneUtils
{
public static void completeMilestone( List caseIds, String milestoneName, DateTime complDate )
{
for ( CaseMilestone cm :
[ SELECT Id, completionDate
FROM CaseMilestone
WHERE ( CaseId in :caseIds
AND MilestoneType.Name = :milestoneName
AND CompletionDate = null
)
LIMIT 1
]
) update new CaseMilestone( Id = cm.Id, CompletionDate = complDate );
}
}
</pre>
Thank you for your response. let me test it
just out of curiousity though, why this considered to be cheating? its valid codewise
The more I look at it, the more I like the one-line version better. It only executes the update statement if there is a CaseMilestone. The three-line version always calls update, even with an empty list.
I'm glad it's working for you!