You need to sign in to do that
Don't have an account?
bobnunny1
Superbadge APEX Error
Performing the first Challenge of this Superbadge for APEX, I am receiving the below error. But it is not true because the Maintenance Request does indeed have all of the attributes cited when I try via the GUI and via a Test Class. Can someone please tell me what is wrong?
Challenge Not yet complete... here's what's wrong:
Inserting a new Maintenance Request of type 'Routine Maintenance' and then closing it did not create of a new Maintenance Request based upon the original record correctly. The challenge is expecting to find the closed Maintenance Request plus an 'New' Maintenance Request of type 'Routine Maintenance' with the same Vehicle as the closed one.
(Also, the challenge didn't specifically mention that the Case and Product2 objects should be relabeled)
Challenge Not yet complete... here's what's wrong:
Inserting a new Maintenance Request of type 'Routine Maintenance' and then closing it did not create of a new Maintenance Request based upon the original record correctly. The challenge is expecting to find the closed Maintenance Request plus an 'New' Maintenance Request of type 'Routine Maintenance' with the same Vehicle as the closed one.
(Also, the challenge didn't specifically mention that the Case and Product2 objects should be relabeled)
All Answers
Error here states that the new case that you are creating should have the same value in the field Vehicle__c as the old case that you have closed. And the type of the new case that you areting should be set to 'Routine Maintenance'.
I guess you are missing this statement somewhere... (newCase.Vehicle__c = oldCase.Vehicle__c; before inserting the newCase)
caseInsLIST.add(new Case(
Subject = caseItem.ID + ' Scheduled Checkup',
Description = caseItem.ID + ' Scheduled Checkup for related Work Part',
Vehicle__c = caseItem.Vehicle__c,
Type = 'Routine Maintenance',
Date_Reported__c = system.Today(),
Date_Due__c = system.Today() + (wpItem.Equipment__r.Maintenance_Cycle__c == null ? 0 : (integer) wpItem.Equipment__r.Maintenance_Cycle__c)));
can you please help me to earn super badge actually am trying to learn apex and now finally am just started the super badges am beginer in salesforce but for "Automate record creation" part am not getting the requirement please explain me and also send me code please if you can.
Jeff Douglas
Trailhead Developer Advocate
Jeff Douglas
Trailhead Developer Advocate
On eproblem that I see in your code is this code...
you are creating new case for each associated work part which is wrong. you just need to create one case and then change the association on all the work part records from old case to new case.... check my last reply here on this link https://developer.salesforce.com/forums/ForumsMain?id=906F0000000kEG5IAM
If you look closely there is a field Equipment__c on Case object which is a lookup to Product
I am using this relation and creating one ormula field on csae object itself. Something ;like this,
Once done this .... I am populating due date by doing this... Even I did the code earlier the hard way and tried to complete the challenge but I guess here they are expecting much simpler and straight forward customization. Don't overthink or overwork it just keep it simple that's what I will suggest.
version 1 of my code... (I am not using this anymore and completed challenge by commenting it out)
Challenge Not yet complete... here's what's wrong:
The 'MaintenanceRequest' trigger does not appear to be handling bulk operations correctly. For the positive use case of inserting and updating more than 200 records, it did not produce the expected outcome.
Thx.
Hello, here is my code for the test but i have this error:Challenge Not yet complete... here's what's wrong:
The 'MaintenanceRequest' trigger does not appear to be handling bulk operations correctly. For the positive use case of inserting and updating more than 200 records, it did not produce the expected outcome.
I will put all my codes bellow thanks for the help.
I am try complete Apex superbadge Synchronize Salesforce data with an external system. my code show this error "Challenge Not yet complete... here's what's wrong:
The runWarehouseEquipmentSync method does not appear to have run successfully. Could not find a successfully completed @future job for this method. Make sure that you run this method at least one before attempting this challenge. Since this method is annotated with the @future method, you may want to wait for a few seconds to ensure that it has processed successfully.
my code is
global with sharing class WarehouseCalloutService {
private static final String WAREHOUSE_URL = 'https://th-superbadge-apex.herokuapp.com/equipment';
// complete this method to make the callout (using @future) to the
// REST endpoint and update equipment on hand.
@future(callout=true)
public static void runWarehouseEquipmentSync()
{
System.debug('entered method of warehouse callout services');
Http http=new http();
HttpRequest call=new HttpRequest();
call.setEndpoint('https://th-superbadge-apex.herokuapp.com/equipment');
call.setMethod('GET');
HttpResponse res=http.send(call);
List<Product2> p2 = new List<Product2>();
if (res.getStatusCode() == 200)
{
List<Object> equipments = (List<Object>) JSON.deserializeUntyped(res.getBody());
// List<Product2> p2 = new List<Product2>();
for(Object obj : equipments){
map<String, Object> mapProduct = (Map<String, Object>)obj;
Product2 product = new Product2();
product.Name = (string)mapProduct.get('name');
System.debug('product name='+product.Name);
product.Cost__c = (integer)mapProduct.get('cost');
product.Current_Inventory__c = (integer)mapProduct.get('quantity');
product.Maintenance_Cycle__c = (Decimal)mapProduct.get('maintenanceperiod');
product.Replacement_Part__c = true;
product.Lifespan_Months__c = (integer)mapProduct.get('lifespan');
product.Warehouse_SKU__c = (string)mapProduct.get('sku');
product.ProductCode = (string)mapProduct.get('_id');
p2.add(product);
}
System.debug(p2);
system.debug('function');
try
{
upsert p2 Warehouse_SKU__c;
System.debug('upsert');
}
Catch(DmlException e)
{
System.debug(e.getMessage());
}
}
}
}
Trailhead Developer Advocate
I am working on first challenge of apex superbadge and according to the requirement I created new case with Routine Maintenance status and also attached an old vehicle but I don't know why I am getting this error.
Inserting a new Maintenance Request of type 'Routine Maintenance' and then closing it did not create of a new Maintenance Request based upon the original record correctly. The challenge is expecting to find the closed Maintenance Request plus an 'New' Maintenance Request of type 'Routine Maintenance' with the same Vehicle as the closed one.
Below is my code:
Trigger: MaintenanceRequest
trigger MaintenanceRequest on Case (after update)
{
MaintenanceRequestHelper.updateWorkOrders(Trigger.new);
}
MaintenanceRequestHelper Class:
public with sharing class MaintenanceRequestHelper
{
public static void updateWorkOrders(List<case> newcaes)
{
list<case> newcases = new list<case>();
for(case newc:newcaes)
{
integer datevalue = getdate(newc.id);
if(newc.Status == 'Closed' && newc.Type == 'Repair' || newc.Type == 'ROUTINE_MAINTENANCE')
{
case creatingcase = new case();
creatingcase.Subject = 'New case created by Trigger';
creatingcase.Vehicle__c = newc.Vehicle__c;
creatingcase.Equipment__c = newc.Equipment__c;
creatingcase.Type = 'Routine Maintenance';
creatingcase.Date_Reported__c = date.today();
creatingcase.Date_Due__c = Date.today().addDays(datevalue);
creatingcase.status= 'new';
newcases.add(creatingcase);
}
}
insert newcases;
}
public static Integer getdate(id caseids)
{
List<Work_Part__c> work = new List<Work_Part__c>();
list<Integer> duedates = new list<Integer>();
work =[select id, Equipment__r.Maintenance_Cycle__c, Maintenance_Request__c from Work_Part__c where Maintenance_Request__r.id =: caseids];
system.debug('size of work list....'+work.size());
integer duedate = 0;
for(Work_Part__c w:work)
{
duedates.add(Integer.valueOf(w.Equipment__r.Maintenance_Cycle__c));
duedates.sort();
duedate = duedates[0];
}
return duedate;
}
Anyone still having any problem in completing step 1 of apex specialist superbadge, refer this link..
https://salesforceranger.blogspot.com/2020/07/apex-specialist-challenge-1.html
Closing a Maintenance Request of type 'Routine Maintenance' or 'Repair' did not create of a new Maintenance Request with the correct due date. The challenge is expecting the due date to be calculated using the maintenance cycle defined on the related equipment records. If multiple equipments are used in the maintenance request, choose the shortest maintenance cycle to define the service date.
My trigger code.
trigger MaintenanceRequest on Case (after update) {
// This is an after update trigger, but you can use the same structure for other trigger events.
// Remember: One trigger per object.
//Create a Map to store the Maintenance Requests to evaluate:
Map<Id, Case> casesToEvaluate = new Map<Id, Case>();
if(Trigger.isAfter && Trigger.isUpdate){
for(Case maintenance : Trigger.new){
// Check if this Maintenance Type and Status follow the requirements:
if((maintenance.Type.contains('Repair') || maintenance.Type.contains('Routine Maintenance')) && maintenance.Status == 'Closed'){
casesToEvaluate.put(maintenance.Id,maintenance);
}
}
}
// Simple! With the collection of Cases in a Map, we can invoke the method from the handler class:
Helper 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.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;
}
}
}
MaintenanceRequestHelper.updateWorkOrders(casesToEvaluate);
}
Hi team,
I facing issues in the first challenge in APEX Specialist superBadge.
what I have done in this challenge is
step-1>> I created New trailhead
Step-2>> Install the unmanaged package(How We Roll Maintenance)
step-3>> Renamed the Objects "Case" TO "Maintenance Request"
"Products" TO "Equipment"
Note; I also changed "Products" TO "Vehicle"
Step-4 >>Here My Code :
MY TRIGGER:
trigger MaintenanceRequest on Case (before update, after update) {
if ( Trigger.isAfter && Trigger.isUpdate ) {
MaintenanceRequestHelper.updateWorkOrders ( Trigger.New, Trigger.OldMap );
}
}
MY APEX CLASS:
public class MaintenanceRequestHelper {
// Retrieve the Cases from the database, in order to retrieve the related Equipments.
public static List<Case> updatedMaintenanceList( List<Case> maintenanceRequests ) {
return [ SELECT ID, Equipment__r.Maintenance_Cycle__c, Type, Status, Vehicle__c, Subject, Equipment__c FROM CASE WHERE ID =: maintenanceRequests ];
}
// Create a new Routine and Update Work Orders
public static void updateWorkOrders( List<Case> maintenanceRequests, Map<Id, Case> mapOldRecords ) {
List<Case> maintenanceRequestsThatMetTheCriteria = new List<Case>();
// New maintenances.
List<Case> newMaintenanceListForRoutineCheckup = new List<Case>();
// Iterating to check for those that meet the criteria
for ( Case maintenanceRequest : updatedMaintenanceList( maintenanceRequests ) ) {
Case oldMaintenance = mapOldRecords.get( maintenanceRequest.Id );
if ( meetCriteria( maintenanceRequest, oldMaintenance ) && maintenanceRequest.Type == 'Repair' || maintenanceRequest.Type == 'Routine Maintenance' ) {
maintenanceRequestsThatMetTheCriteria.add( maintenanceRequest );
}
}
// Retrieve the mimimun number of cycle days required to close the newest case.
Map<Id, Decimal> mapMaintenanceIdWithCycleDays = retrieveMinimunCycleDays( maintenanceRequestsThatMetTheCriteria );
for ( Case newMaintenaceRequest : maintenanceRequestsThatMetTheCriteria ) {
// Create a new routine once the first one is closed, with same vehicle and equipment
Case routineCheckupMaintenance = new Case();
routineCheckupMaintenance.Vehicle__c = newMaintenaceRequest.Vehicle__c;
routineCheckupMaintenance.Equipment__c = newMaintenaceRequest.Equipment__c;
routineCheckupMaintenance.Type = 'Routine Maintenance';
routineCheckupMaintenance.Subject = String.isBlank( newMaintenaceRequest.Subject ) ? 'New Routine ' : newMaintenaceRequest.Subject;
routineCheckupMaintenance.Status = 'New';
routineCheckupMaintenance.Date_Reported__c = System.today();
if ( mapMaintenanceIdWithCycleDays.containsKey( newMaintenaceRequest.Id ) ) {
routineCheckupMaintenance.Date_Due__c = System.today().addDays( ( Integer ) mapMaintenanceIdWithCycleDays.get( newMaintenaceRequest.Id ) );
} //else {
// routineCheckupMaintenance.Date_Due__c = System.today().addDays( ( Integer ) newMaintenaceRequest.Equipment__r.Maintenance_Cycle__c );
//}
routineCheckupMaintenance.Old_Case__c = newMaintenaceRequest.Id;
newMaintenanceListForRoutineCheckup.add( routineCheckupMaintenance );
}
insert newMaintenanceListForRoutineCheckup;
updateRelatedWorkParts( newMaintenanceListForRoutineCheckup );
}
// Once the new maintenance records are created, update the work parts
public static void updateRelatedWorkParts( List<Case> createdMaintenances) {
Map<Id, Id> mapNewMaintenancesWithClosedMaintenances = new Map<Id, Id>();
for ( Case newMaintenances : createdMaintenances ) {
mapNewMaintenancesWithClosedMaintenances.put( newMaintenances.Old_Case__c , newMaintenances.Id );
}
List<Work_Part__c> workParkList = new List<Work_Part__c>();
// Query work parts to be updated
for ( Work_Part__c workPart : [ SELECT Id, Maintenance_Request__c, Equipment__c, Quantity__c FROM Work_Part__c
WHERE Maintenance_Request__c =: mapNewMaintenancesWithClosedMaintenances.keySet() ] ) {
workParkList.add(new Work_Part__c(Maintenance_Request__c = mapNewMaintenancesWithClosedMaintenances.get( workPart.Maintenance_Request__c ),
Equipment__c = workPart.Equipment__c, Quantity__c = workPart.Quantity__c));
}
insert workParkList;
}
// Method to check if the cases being updated meet the criteria (They must be closed)
public static boolean meetCriteria( Case newcase, Case oldMaintenance ) {
Boolean falg = false;
if ( newCase.Status == 'Closed' && oldMaintenance.Status != newCase.Status ) {
falg = true;
}
return falg;
}
// Use an aggregate function to return the minimum number of cycle days related to the Case
public static Map<Id, Decimal> retrieveMinimunCycleDays( List<Case> maintenanceRequests ) {
AggregateResult[] listWorkParts = [ SELECT Maintenance_Request__c, MIN( Equipment__r.Maintenance_Cycle__c )
FROM Work_Part__c WHERE Maintenance_Request__c =: maintenanceRequests
Group By Maintenance_Request__c ];
Map<Id, Decimal> mapWorkPartaAndMaintenanceRecords = new Map<Id, Decimal>();
for ( AggregateResult ar : listWorkParts ) {
mapWorkPartaAndMaintenanceRecords.put( ( Id ) ar.get( 'Maintenance_Request__c' ), ( Decimal ) ar.get( 'expr0' ) );
}
return mapWorkPartaAndMaintenanceRecords;
}
}
MY APEX TEST
@isTest
public class MaintenanceRequestTest {
@testSetup
static void setup(){
Product2 prod = new Product2();
prod.Cost__c = 50;
prod.Name = 'Ball Valve 10 cm';
prod.Lifespan_Months__c = 12;
prod.Maintenance_Cycle__c = 365;
prod.Current_Inventory__c = 50;
prod.Replacement_Part__c = true;
prod.Warehouse_SKU__c = '100009';
insert prod;
Product2 prod2 = new Product2();
prod2.Cost__c = 50;
prod2.Name = 'Ball Valve 10 cm';
prod2.Lifespan_Months__c = 12;
prod2.Maintenance_Cycle__c = 240;
prod2.Current_Inventory__c = 50;
prod2.Replacement_Part__c = true;
prod2.Warehouse_SKU__c = '100009';
insert prod2;
List<Case> caseList = new List<Case>();
for(Integer i=0; i<300; i++) {
Case caseNew = new Case();
caseNew.Subject = 'Maintenance ' + i;
caseNew.Type = 'Other';
caseNew.Status = 'New';
caseNew.Equipment__c = prod.Id;
caseNew.SuppliedName = 'Test';
caseList.add(caseNew);
if(i==10){
caseNew.Subject = 'Maintenance test 10';
}
}
insert case list;
List<Work_Part__c> workPartList = new List<Work_Part__c>();
for(Case caseHere : [select Id, Subject from Case where SuppliedName = 'Test']) {
Work_Part__c workPart = new Work_Part__c();
workPart.Maintenance_Request__c = caseHere.Id;
workPart.Equipment__c = prod.Id;
workPartList.add(workPart);
if(caseHere.Subject == 'Maintenance test 10'){
Work_Part__c workPart2 = new Work_Part__c();
workPart2.Maintenance_Request__c = caseHere.Id;
workPart2.Equipment__c = prod2.Id;
workPartList.add(workPart2);
}
}
insert workPartList;
}
@isTest
static void testMaintenanceRequest(){
List<Case> caseList = new List<Case>();
for(Case caseHere : [select Id from Case where SuppliedName = 'Test']) {
caseHere.Type = 'Repair';
caseHere.Status = 'Closed';
caseList.add(caseHere);
}
Test.startTest();
update caseList;
System.assertEquals(300, [SELECT count() FROM Case WHERE Type = 'Routine Maintenance' and Date_Reported__c = :Date.today()]);
Test.stopTest();
}
}
MY ERROR:
Challenge Not yet complete... here's what's wrong:
Closing a Maintenance Request of type 'Routine Maintenance' or 'Repair' did not create of a new Maintenance Request with the correct due date. The challenge is expecting the due date to be calculated using the maintenance cycle defined on the related equipment records. If multiple equipments are used in the maintenance request, choose the shortest maintenance cycle to define the service date.
Close errors
Thanks in advance
Further Information:
My Email Id:anagarajasfdc@gmail.com
Please check below code. If you find it helpful then Mark the answer as best so that it will help other trailblazers as well. CHEERS!
If anyone is still stuck, below solution worked for me.
Please Like my answer if helped you solve the challenge.
Equipment_Maintenance_Item__c.Maintenance_Request__c to allow reparenting. Basically enable the checkbox -
Reparentable Master Detail
I am facing issue while creating SOQL query. As i can see object "Equipment Maintenance Item" is child object of "case" but somehow my query is not returning expected result. Can someone explain why this is happening.
Challenge Not yet complete... here's what's wrong:
There was an unexpected error in your org which is preventing this assessment check from completing: System.DmlException: Update failed. First exception on row 0 with id 5005g000009okFrAAI; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, MaintenanceRequest: execution of AfterUpdate caused by: System.SObjectException: Field is not writeable: Equipment_Maintenance_Item__c.Maintenance_Request__c Class.MaintenanceRequestHelper.updateEquipments: line 33, column 1 Class.MaintenanceRequestHelper.updateWorkOrders: line 23, column 1 Trigger.MaintenanceRequest: line 4, column 1: []
For Apex Superbadge Challenge 4 Use following solution. As I have completed this challenge after number of R&D...
For this challenge need to create follwing things:
1. MaintenanceRequestHelper Class
2. MaintenanceRequest Trigger on Case object
3. MaintenanceRequestHelperTest for testing purpose as name given in challenge
Use following code for following things:
1. MaintenanceRequestHelper Class
-------------------------------------------------------------------------------------------------------------------------------------------------------
public class MaintenanceRequestHelper {
public static void updateWorkOrders(Map<Id, Case> applicableCases){
Map<Id, Integer> mapProduct = new Map<Id, Integer>();
List<Case> newCases = 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());
newCases.add(newCase);
}
if(newCases.size() > 0){
insert newCases;
}
}
}
========================================================================================================
2. MaintenanceRequest Trigger on Case object
--------------------------------------------------------------------------------------------------------------------------------------
trigger MaintenanceRequest on Case (before update, after update) {
// call MaintenanceRequestHelper.updateWorkOrders
Map<Id,Case> applicableCases = new Map<Id,Case>();
if(Trigger.isUpdate){
if(Trigger.isAfter){
for(Case a: Trigger.new){
if (a.IsClosed && (a.Type.equals('Repair') || a.Type.equals('Routine Maintenance'))){
applicableCases.put(a.Id, a);
}
}
MaintenanceRequestHelper.updateWorkOrders(applicableCases);
}
}
}
=================================================================================================
3. MaintenanceRequestHelperTest
-----------------------------------------------------------------------------------------------------------------------------------------------
@isTest
public class MaintenanceRequestHelperTest {
@isTest
static void testMaintenanceRequest(){
List<Case> caseList = new List<Case>();
Product2 prod = new Product2();
prod.Cost__c = 50;
prod.Name = 'Ball Valve 10 cm';
prod.Lifespan_Months__c = 12;
prod.Maintenance_Cycle__c = 365;
prod.Current_Inventory__c = 50;
prod.Replacement_Part__c = true;
prod.Warehouse_SKU__c = '100009';
insert prod;
System.assertEquals(1, [SELECT count() FROM Product2 WHERE Name = 'Ball Valve 10 cm']);
for(Integer i=1;i<=300;i++) {
Case caseNew = new Case();
caseNew.Subject = 'Maintenance';
caseNew.Type = 'Other';
caseNew.Status = 'New';
caseNew.Equipment__c = prod.Id;
caseList.add(caseNew);
}
Test.startTest();
insert caseList;
System.assertEquals(300, [SELECT count() FROM Case WHERE Type = 'Other']);
for(Case a : caseList){
a.Type = 'Repair';
a.Status = 'Closed';
}
update caseList;
System.assertEquals(300, [SELECT count() FROM Case WHERE Type = 'Repair']);
Test.stopTest();
}
}
======================================================================================================
If you like and used this code then please mark my solution as Best Answer
Closing a Maintenance Request of type 'Routine Maintenance' did not create of a new Maintenance Request related to the same Vehicle.