• Sudhir Panda
  • NEWBIE
  • 15 Points
  • Member since 2018
  • Salesforce Developer

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 1
    Replies
for(String line:lines){
                if(cnt!=0 && Util.IfNull(line)!=''){
                    try{
                        fields = line.split(',', -1);
                        message = fields[1];
                        temp = fields[3].normalizeSpace();// remove white space
                        temp = temp.replace('\\n\\r', '');// remove CRLF
                        system.debug('temp:'+temp);
                        
                        giverId = [Select id from User where Alias =: Util.IfNull(fields[0]) limit 1].Id;
                        receiverId = [Select id from User where Alias =: Util.IfNull(fields[2]) limit 1].Id;
                        badgeId = [SELECT Id FROM WorkBadgeDefinition where Name =: temp limit 1].Id;

Hi,
Is there any way we could avoid those DML's and use before the for loop for the giverId, recieverId, badgeId. As it leads to :- Too many SOQL queries: 101
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;
        
        }
        
       
    }
}