You need to sign in to do that
Don't have an account?

Synchronize Salesforce data with an external system
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.
here is my code
public with sharing class WarehouseCalloutService {
private static final String WAREHOUSE_URL = 'https://th-superbadge-apex.herokuapp.com/equipment';
@future(callout=true)
// complete this method to make the callout (using @future) to the
// REST endpoint and update equipment on hand.
public static void runWarehouseEquipmentSync(){
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint(WAREHOUSE_URL);
request.setMethod('GET');
HttpResponse response = http.send(request);
// If the request is successful, parse the JSON response.
String ret = '';
if (response.getStatusCode() == 200) {
// Deserialize the JSON string into collections of primitive data types.
List<Object> results = (List<Object>) JSON.deserializeUntyped(response.getBody());
List<Product2> LstProduct = new List<Product2>();
for(Object obj : results)
{
Map<String, Object> mapobj = (Map<String, Object>)obj;
Product2 product = new Product2();
Integer maintenanceperiod = (Integer)mapobj.get('maintenanceperiod');
Integer Lifespan = (Integer)mapobj.get('lifespan');
Integer Cost = (Integer)mapobj.get('cost');
Boolean replacement = (Boolean)mapobj.get('replacement');
Integer quantity = ((Integer)mapobj.get('qIntegerntity'));
product.Name = (String)mapobj.get('name');
product.Maintenance_Cycle__c = Integer.valueof(maintenanceperiod);
product.Cost__c = Cost;
product.Current_Inventory__c = quantity;
product.Lifespan_Months__c = Lifespan;
product.Replacement_Part__c = replacement;
product.Warehouse_SKU__c = (String) mapobj.get('sku');
product.ProductCode = (String)mapobj.get('_id');
LstProduct.add(product);
}
System.debug(LstProduct);
upsert LstProduct Warehouse_SKU__c;
}
}
}
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.
here is my code
public with sharing class WarehouseCalloutService {
private static final String WAREHOUSE_URL = 'https://th-superbadge-apex.herokuapp.com/equipment';
@future(callout=true)
// complete this method to make the callout (using @future) to the
// REST endpoint and update equipment on hand.
public static void runWarehouseEquipmentSync(){
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint(WAREHOUSE_URL);
request.setMethod('GET');
HttpResponse response = http.send(request);
// If the request is successful, parse the JSON response.
String ret = '';
if (response.getStatusCode() == 200) {
// Deserialize the JSON string into collections of primitive data types.
List<Object> results = (List<Object>) JSON.deserializeUntyped(response.getBody());
List<Product2> LstProduct = new List<Product2>();
for(Object obj : results)
{
Map<String, Object> mapobj = (Map<String, Object>)obj;
Product2 product = new Product2();
Integer maintenanceperiod = (Integer)mapobj.get('maintenanceperiod');
Integer Lifespan = (Integer)mapobj.get('lifespan');
Integer Cost = (Integer)mapobj.get('cost');
Boolean replacement = (Boolean)mapobj.get('replacement');
Integer quantity = ((Integer)mapobj.get('qIntegerntity'));
product.Name = (String)mapobj.get('name');
product.Maintenance_Cycle__c = Integer.valueof(maintenanceperiod);
product.Cost__c = Cost;
product.Current_Inventory__c = quantity;
product.Lifespan_Months__c = Lifespan;
product.Replacement_Part__c = replacement;
product.Warehouse_SKU__c = (String) mapobj.get('sku');
product.ProductCode = (String)mapobj.get('_id');
LstProduct.add(product);
}
System.debug(LstProduct);
upsert LstProduct Warehouse_SKU__c;
}
}
}
Sincerely regret the inconvenience for the delayed reply.
Please find the below code snippet which helps you surpass the challenge successfully.
Please mark this post as solved if it helps.
Best Regards,
Nagendra.P
DevConsole=> Execute Anonymous and then make sure that the Apex Job for your future method was created and completed before you check the challenge.
Class: WarehouseCalloutService.
Class: WarehouseSyncSchedule
Next You have to schedule it daily:
Run this code in Anonymous WIndow:
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.
I have gone through the above code and added URL to the remote site settings, but getting the 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.
Setup > Remote Site Settings
and click 'Execute' then check your challenge
The above examples in which we see String sch = '0 0 8 * * ? *'; is a cron expression, its correct but to complete our challenge its not as it schedules the job into future time and we would need to wait.
Keep it simple 1. Add the url in the remote site settings with any name and run the above one liner code in the anonymous window.
private static final String WAREHOUSE_URL = 'https://th-superbadge-apex.herokuapp.com/equipment';
// Please note to add the Endpoint URL in Remote Site Settings.
@future(callout=true)
public static void runWarehouseEquipmentSync() {
// create new http request
Http http = new Http();
HttpRequest req = new HttpRequest();
req.setEndpoint(WAREHOUSE_URL); //Setting Endpoint URL for the request.
req.setMethod('GET');
HttpResponse res = http.send(req);
List<product2> equipmentList = new List<product2>();
if(res.getStatusCode()==200){
//Deserialize the JSON String
List< Object> result =(List<object>)JSON.deserializeUntyped(res.getBody());
// For each equipment we create a new product with all the new feild values.
for(Object productMap: result){
Map<String,Object> prod = (Map<String,Object>)productMap;
Product2 newprod = new Product2();
newprod.ExternalId=(String)prod.get('_id');
newprod.Replacement_Part__c=true;
newprod.Cost__c=(Decimal)prod.get('cost');
newprod.Current_Inventory__c=(Decimal)prod.get('quantity');
newprod.Lifespan_Months__c=(Double)prod.get('lifespan');
newprod.Name=(String)prod.get('name');
newprod.Maintenance_Cycle__c=(Integer)prod.get('maintenanceperiod');
newprod.ExternalId=(String)prod.get('sku');
equipmentList.add(newprod);
}
// we upsert the product to the database when the equipment list is greater than zero.
if(equipmentList.size()>0){
upsert equipmentList;}
}
}
}
Hi, if anyone is yet having trouble use the below code. Tested and verified.
public with sharing class WarehouseCalloutService {
private static final String WAREHOUSE_URL = 'https://th-superbadge-apex.herokuapp.com/equipment';
@future(callout=true)
public static void runWarehouseEquipmentSync() {
//ToDo: complete this method to make the callout (using @future) to the
// REST endpoint and update equipment on hand.
List<Product2> prods2Update = new List<Product2>();
HttpRequest req = new HttpRequest();
req.setEndpoint(WAREHOUSE_URL);
req.setMethod('GET');
req.setHeader('Content-Type', 'application/json;charset=UTF-8');
http http = new http();
HttpResponse res = new HttpResponse();
res = http.send(req);
if(res.getStatusCode() == 200)
{
string respStr = res.getBody().replace('replacement','Replacement_Part__c').replace('maintenanceperiod','Maintenance_Cycle__c');
respStr = respStr.replace('Cost','Cost__c').replace('lifespan','Lifespan_Months__c');
respStr = respStr.replace('sku','Warehouse_SKU__c').replace('quantity','Current_Inventory__c');
List<Product2> results = (List<Product2>) JSON.deserialize(respStr, List<Product2>.class);
system.debug('response key set---> '+results);
for(Product2 item : results)
{
//system.debug('id---> '+ item.Current_Inventory__c);
prods2Update.add(item);
}
}
else
{
system.debug('error code '+res.getStatusCode()+'error status '+res.getStatus());
}
if(!prods2Update.isEmpty())
{
upsert prods2Update Warehouse_SKU__c;
system.debug('list got updated. Size: '+prods2Update.size());
}
else
system.debug('List is empty.Size: '+prods2Update.size());
}
}
If worked for you mark it the best answer.
Implement Queable interface for class 'WarehouseCalloutService'.
This could have not been the issue for others as mentioned above if you have tried executing your method once in Anonymous window.
What Parag Devghare said is correct.
private static final String WAREHOUSE_URL = 'https://th-superbadge-apex.herokuapp.com/equipment';
public static void runWarehouseEquipmentSync(){
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setMethod('GET');
request.setEndpoint(WAREHOUSE_URL);
HttpResponse response = http.send(request);
if(response.getStatusCode() == 200) {
List<Object> jsonResponse = (List<Object>)JSON.deserializeUntyped(response.getBody());
system.debug('~~ '+jsonResponse);
List<Product2> productList = new List<Product2>();
for(Object ob : jsonResponse) {
Map<String,Object> mapJson = (Map<String,Object>)ob;
Product2 pr = new Product2();
pr.Replacement_Part__c = (Boolean)mapJson.get('replacement');
pr.Name = (String)mapJson.get('name');
pr.Maintenance_Cycle__c = (Integer)mapJson.get('maintenanceperiod');
pr.Lifespan_Months__c = (Integer)mapJson.get('lifespan');
pr.Cost__c = (Decimal) mapJson.get('lifespan');
pr.Warehouse_SKU__c = (String)mapJson.get('sku');
pr.Current_Inventory__c = (Double) mapJson.get('quantity');
productList.add(pr);
}
if(productList.size()>0)
upsert productList;
}
}
public static void execute(QueueableContext context){
runWarehouseEquipmentSync();
}
}
In Execute Anonymous Window, use enqueueJob.
System.enqueueJob(New WarehouseCalloutService());
@DHIRAJ SABALE.......his code works for me thanks a lot yaar
---------------------
public with sharing class WarehouseCalloutService implements Queueable {
private static final String WAREHOUSE_URL = 'https://th-superbadge-apex.herokuapp.com/equipment';
@future(callout=true)
public static void runWarehouseCalloutSync(){
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setMethod('GET');
request.setEndpoint(WAREHOUSE_URL);
HttpResponse response = http.send(request);
if(response.getStatusCode() == 200){
List<Object> jsonResponse = (List<Object>)JSON.deserializeUntyped(response.getBody());
if(jsonResponse.size() > 0){
List<Product2> products = new List<Product2>();
for(Object o : jsonResponse){
Product2 pr = new Product2();
Map<String,Object> pro = (Map<String,Object>)o;
pr.Replacement_Part__c = (Boolean)pro.get('replacement');
pr.Cost__c = (Integer)pro.get('cost');
pr.Current_Inventory__c = (Integer)pro.get('quantity');
pr.Lifespan_Months__c = (Integer)pro.get('lifespan');
pr.Maintenance_Cycle__c = (Integer)pro.get('maintenanceperiod');
pr.Warehouse_SKU__c = (String)pro.get('sku');
pr.ProductCode = (String)pro.get('_id');
pr.Name = (String)pro.get('name');
products.add(pr);
}
if(products.size() > 0){
System.debug(products);
upsert products;
}
}
}
}
public static void execute(QueueableContext context){
runWarehouseCalloutSync();
}
}
-------------------
this code is works.
The WarehouseCalloutService class does not appear to have run successfully as the equipment records are not found in the database. The REST endpoint returns 22 records so your org should have at least 22 records in the Product2 table. Make sure that you successfully run this class at least once before attempting this challenge. Since this class is implementing the queueable interface, you may want to wait to ensure that it has processed successfully
Please find the below code that resolve this error!!
Then run the below code in anonymous window.
Hope, It is working for you.
Regards,
Pankaj