You need to sign in to do that
Don't have an account?
Himanshu Verma 23
Trailhead super badge problem?
Automate record creation
Install the unmanaged package for the schema and stubs for Apex classes and triggers. Rename cases and products to match the HowWeRoll schema, and assign all profiles to the custom HowWeRoll page layouts for those objects. Use the included package content to automatically create a Routine Maintenance request every time a maintenance request of type Repair or Routine Maintenance is updated to Closed. Follow the specifications and naming conventions outlined in the business requirements.
how to achieved ?
Install the unmanaged package for the schema and stubs for Apex classes and triggers. Rename cases and products to match the HowWeRoll schema, and assign all profiles to the custom HowWeRoll page layouts for those objects. Use the included package content to automatically create a Routine Maintenance request every time a maintenance request of type Repair or Routine Maintenance is updated to Closed. Follow the specifications and naming conventions outlined in the business requirements.
how to achieved ?
Thanks for trying superbadges!
Jeff Douglas
Trailhead Developer Advocate
1.MaintenanceRequest Trigger:
public static void updateWorkOrders(List<case>ClosedCaseList){
// update workorders
list<case> insertCaseList = new list<case>();
for(Case c : ClosedCaseList)
{
Case newCase = new Case();
newCase.Type = 'Routine Maintenance';
newCase.Status = 'New';
newCase.Vehicle__c = c.Vehicle__c;
newCase.Subject = c.Subject;
newCase.Date_Reported__c = Date.today();
newCase.Date_Due__c = Date.today();
newCase.Equipment__c = c.Equipment__c;
insertCaseList.add(newCase);
}
if(insertCaseList.size()>0){
insert insertCaseList;
}
}
}
https://login.salesforce.com/packaging/installPackage.apexp?p0=04t36000000i5UM
Then, rename things from
1) Setup -> Rename Tabs and Labels -> Edit (Case) ==> Rename to 'Maintenance Request'
2) Setup -> Rename Tabs and Labels -> Edit (Product) ==> Rename to 'Equipment'
The installed package has already had the required trigger 'MaintenanceRequest' and class 'MaintenanceRequestHelper', you need to work on them.
1.Create New Trailhead Playground Org (recommended) to complete Superbadges and get your login Credentials.
2.Install Unmanaged package(Id-04tB0000000Q3Cq) to avoid errors like duplicate object, field, etc. as this package contains all resources mentioned in the Use case or Requirements.
3.Rename Case & Product as given in the Use Case(Standard Objects section)-
a) Setup -> Rename Tabs and Labels -> Edit (Case) ==> Rename to 'Maintenance Request'
b) Setup -> Rename Tabs and Labels -> Edit (Product) ==> Rename to 'Equipment'
4.Trigger named 'MaintenanceRequest' & Class named 'MaintenanceRequestHelper' is already provided in above package so you just need to search for it and modify as per below :
Trigger -
trigger MaintenanceRequest on Case (before update, after update) {
// ToDo: Call MaintenanceRequestHelper.updateWorkOrders
Map<Id,Case> caseLst = new Map<Id,Case>();
if(Trigger.isUpdate && Trigger.isAfter){
for(Case oCase: Trigger.new){
if (oCase.IsClosed && (oCase.Type.equals('Repair') || oCase.Type.equals('Routine Maintenance'))){
caseLst.put(oCase.Id, oCase);
}
}
if(caseLst.size() > 0){
System.debug('****Calling updateWorkOrders from MaintenanceRequestHelper Class****');
MaintenanceRequestHelper.updateWorkOrders(caseLst);
}
}
}
Class-
public class MaintenanceRequestHelper {
public static void updateWorkOrders(Map<Id, Case> applicableCases){
System.debug('****Inside MaintenanceRequestHelper Class****');
Map<Id, Integer> mapProduct = new Map<Id, Integer>();
List<Case> newCaseList = new List<Case>();
List<Product2> listProduct = [select Id, Maintenance_Cycle__c from Product2];
for (Product2 p : listProduct) {
if (p != null) {
if(p.Maintenance_Cycle__c != null){
mapProduct.put(p.Id, Integer.valueOf(p.Maintenance_Cycle__c));
}
}
}
for(Case a: applicableCases.values()){
Case newCase = new Case();
newCase.Vehicle__c = a.Vehicle__c;
newCase.Equipment__c = a.Equipment__c;
newCase.Type = 'Routine Maintenance';
newCase.Subject = String.isBlank(a.Subject) ? 'Routine Maintenance Request' : a.Subject;
newCase.Date_Reported__c = Date.today();
newCase.Status = 'New';
newCase.Product__c = a.Product__c;
newCase.AccountId = a.AccountId;
newCase.ContactId = a.ContactId;
newCase.AssetId = a.AssetId;
newCase.Origin = a.Origin;
newCase.Reason = a.Reason;
newCase.Date_Due__c = (mapProduct.get(a.Id) != null) ? (Date.today().addDays(Integer.valueOf(mapProduct.get(a.Id)))) : (Date.today());
newCaseList.add(newCase);
}
if(newCaseList.size() > 0){
insert newCaseList;
}
}
}
5. Click on save & Check the Challenge.
Well, I have read many above answers and applied them too. Some of them work fine and will allow you to pass the first Challenge but if the correct solution to this is :
MaintenanceRequest.apxt
trigger MaintenanceRequest on Case (before update, after update) {
// ToDo: Call MaintenanceRequestHelper.updateWorkOrders
Map<Id,Case> mapofMntRque = new Map<Id,Case>();
if(Trigger.isUpdate && Trigger.isAfter)
{
for(Case ca: Trigger.New)
{
if(ca.Type.equalsIgnoreCase('Repair') || ca.Type.equalsIgnoreCase('Routine Maintenance') && ca.IsClosed == true)
{
mapofMntRque.put(ca.Id, ca);
}
}
}
if(!mapofMntRque.isEmpty())
{
system.debug('Calling MaintenanceRequestHelper.updateWorkOrders');
MaintenanceRequestHelper.updateWorkOrders(mapofMntRque);
}
}
MaintenanceRequestHelper.apxc
public with sharing class MaintenanceRequestHelper {
public static void updateWorkOrders(Map<Id,Case> mapofMntRque) {
// TODO: Complete the method to update workorders
/*
This section gives a solution to this bellow statement :
"Another aspect about parts is they all have different lifespans. Therefore, you need to calculate and set the next due date using the maintenance cycle defined on the related work part records. If multiple work parts are used in the maintenance request, choose the shortest maintenance cycle to define the service date."
The above statements are skipped by many of the developers and this also gets skipped by the salesforce validations but I think this is important. It is for knowledge purpose.
You can comment on this below this till the next comment if errors occur.
/*
String query2 = 'select id,(select equipment__r.maintenance_cycle__c from work_parts__r) from case';
List<Case> CaselistToEquip = database.query(query2);
Map<Id,Decimal> case2MaintenanceCycle = new Map<Id,Decimal>();
for(case ce : CaselistToEquip)
{
Decimal maintenanceCycle = 0.0;
for(work_part__c wp : ce.work_parts__r)
{
maintenanceCycle = (maintenanceCycle > wp.equipment__r.maintenance_cycle__c) ? wp.equipment__r.maintenance_cycle__c : maintenanceCycle;
}
case2MaintenanceCycle.put(ce.id,maintenanceCycle);
}
// comment above if errors occur.
List<Case> caseList = new List<Case>();
if(!mapofMntRque.isEmpty())
{
for(Case ca : mapofMntRque.values())
{
system.debug('------'+ca.Id);
Case newCa = new Case();
newCa.Vehicle__c = ca.Vehicle__c;
newCa.Equipment__c = ca.Equipment__c;
newCa.Type = 'Routine Maintenance';
newCa.Subject = (string.isBlank(ca.Subject)) ? ('Routine Maintenance Request') : (ca.Subject) ;
newCa.Date_Reported__c = date.today();
// comment next line if error occur.
newCa.Date_Due__c = (case2MaintenanceCycle.containsKey(ca.Id)) ? Date.today().addDays(Integer.valueOf(case2MaintenanceCycle.get(ca.Id))) : date.today();
/*
uncomment this section and comment the just the above whole line.
newCa.Date_Due__c = (ca.Equipment__r.Maintenance_Cycle__c != null) ? Date.today().addDays(Integer.valueOf(ca.Equipment__r.Maintenance_Cycle__c)) : date.today();
*/
newCa.Origin = 'Phone';
newCa.Status = 'New';
caseList.add(newCa);
}
insert caseList;
}
}
}
If works for you. Mark this the best answer.