You need to sign in to do that
Don't have an account?
t j 5
Apex Specialist Superbadge : Solution of Step1,2,3,4
Hi
Here is the complete solution. Hit like if this solution helps you.
trigger MaintenanceRequest on Case ( after update) {
// List<case>ClosedCaseList = [SELECT Id, subject, Vehicle__c, vehicle__r.Name, equipment__c, type FROM Case WHERE status = 'closed'];
List<case>ClosedCaseList = new List<case>();
for(case c : Trigger.New){
if((c.type == 'Repair' || c.type =='Routine Maintenance') && c.status =='closed'){
ClosedCaseList.add(c);
}
}
MaintenanceRequestHelper.updateWorkOrders(ClosedCaseList);
}
public class MaintenanceRequestHelper {
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){
Database.insert(insertCaseList,false);
}
}
}
public with sharing class WarehouseCalloutService {
public static integer size;
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(){
HttpRequest req = new HttpRequest();
req.setMethod('GET');
req.setEndpoint(WAREHOUSE_URL);
req.setHeader('Content-Type', 'application/json');
Http http= new Http();
HTTPResponse res = http.send(req);
list<object> equipments = (list<Object>) JSON.deserializeUntyped(res.getBody());
//List<Product2__c> p2 = new List<Product2__c>();
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');
product.Cost__c = (integer)mapProduct.get('cost');
product.Current_Inventory__c = (integer)mapProduct.get('quantity');
product.ProductCode = (string)mapProduct.get('_id');
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);
}
size=p2.size();
system.debug('p2'+p2.size());
Database.upsert(p2,false);
System.debug('upsert');
}
}
@isTest
Public class WarehouseCalloutServiceTest {
// implement your mock callout test here
public static String CRON_EXP = '0 0 0 15 3 ? 2022';
@isTest
public static void test1(){
Test.startTest();
Vehicle__c vec = new Vehicle__c();
vec.name= 't1';
insert vec;
product2 prod= new product2 ();
prod.name= 't1';
insert prod;
Vehicle__c vecD= [SELECT Id FROM Vehicle__c WHERE name='t1'];
product2 prodD= [SELECT Id FROM product2 WHERE name='t1'];
list<case> l1 = new list<case>();
for(integer i = 0; i<=300;i++)
{
case newCase= new case();
newCase.Type = 'Routine Maintenance';
newCase.Status = 'New';
newCase.Vehicle__c = vecD.id;
newCase.Subject = 'testt'+i;
newCase.Date_Reported__c = Date.today();
newCase.Date_Due__c = Date.today();
newCase.Equipment__c = prodD.id;
l1.add(newCase);
}
insert l1;
list<case> l2=[select id from case limit 200];
list<case> l3= new list<case>();
for(case ob : l2){
ob.status='closed';
l3.add(ob);
}
update l3;
list<case> l4=[select id from case where Type = 'Routine Maintenance' And status='closed'];
//System.AssertEquals(10,l4.size());
Test.stopTest();
}
@isTest
public static void test2(){
Test.startTest();
if(Test.isRunningTest())
Test.setMock(HttpCalloutMock.class, new WarehouseCalloutServiceMock());
WarehouseCalloutService.runWarehouseEquipmentSync();
Test.stopTest();
System.AssertEquals(22,WarehouseCalloutService.size);
}
}
Here is the complete solution. Hit like if this solution helps you.
trigger MaintenanceRequest on Case ( after update) {
// List<case>ClosedCaseList = [SELECT Id, subject, Vehicle__c, vehicle__r.Name, equipment__c, type FROM Case WHERE status = 'closed'];
List<case>ClosedCaseList = new List<case>();
for(case c : Trigger.New){
if((c.type == 'Repair' || c.type =='Routine Maintenance') && c.status =='closed'){
ClosedCaseList.add(c);
}
}
MaintenanceRequestHelper.updateWorkOrders(ClosedCaseList);
}
public class MaintenanceRequestHelper {
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){
Database.insert(insertCaseList,false);
}
}
}
public with sharing class WarehouseCalloutService {
public static integer size;
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(){
HttpRequest req = new HttpRequest();
req.setMethod('GET');
req.setEndpoint(WAREHOUSE_URL);
req.setHeader('Content-Type', 'application/json');
Http http= new Http();
HTTPResponse res = http.send(req);
list<object> equipments = (list<Object>) JSON.deserializeUntyped(res.getBody());
//List<Product2__c> p2 = new List<Product2__c>();
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');
product.Cost__c = (integer)mapProduct.get('cost');
product.Current_Inventory__c = (integer)mapProduct.get('quantity');
product.ProductCode = (string)mapProduct.get('_id');
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);
}
size=p2.size();
system.debug('p2'+p2.size());
Database.upsert(p2,false);
System.debug('upsert');
}
}
@isTest
Public class WarehouseCalloutServiceTest {
// implement your mock callout test here
public static String CRON_EXP = '0 0 0 15 3 ? 2022';
@isTest
public static void test1(){
Test.startTest();
Vehicle__c vec = new Vehicle__c();
vec.name= 't1';
insert vec;
product2 prod= new product2 ();
prod.name= 't1';
insert prod;
Vehicle__c vecD= [SELECT Id FROM Vehicle__c WHERE name='t1'];
product2 prodD= [SELECT Id FROM product2 WHERE name='t1'];
list<case> l1 = new list<case>();
for(integer i = 0; i<=300;i++)
{
case newCase= new case();
newCase.Type = 'Routine Maintenance';
newCase.Status = 'New';
newCase.Vehicle__c = vecD.id;
newCase.Subject = 'testt'+i;
newCase.Date_Reported__c = Date.today();
newCase.Date_Due__c = Date.today();
newCase.Equipment__c = prodD.id;
l1.add(newCase);
}
insert l1;
list<case> l2=[select id from case limit 200];
list<case> l3= new list<case>();
for(case ob : l2){
ob.status='closed';
l3.add(ob);
}
update l3;
list<case> l4=[select id from case where Type = 'Routine Maintenance' And status='closed'];
//System.AssertEquals(10,l4.size());
Test.stopTest();
}
@isTest
public static void test2(){
Test.startTest();
if(Test.isRunningTest())
Test.setMock(HttpCalloutMock.class, new WarehouseCalloutServiceMock());
WarehouseCalloutService.runWarehouseEquipmentSync();
Test.stopTest();
System.AssertEquals(22,WarehouseCalloutService.size);
}
}