• Eric Blaxton 11
  • NEWBIE
  • 10 Points
  • Member since 2018

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 3
    Questions
  • 1
    Replies
Hi and thanks in advance for any guidance on this.  
I want to take in-person classroom training to learn APEX and VF in the Dallas, Texas area. 
So far I have found:
  • virtual training with live teacher
  • online training study at my own pace
  • Travel to take classes offered by Salesforce. which is expensive.
Hi and thanks in Advance.  I was able to write a test code for my class, but need help.  This provides 5% coverage.

Class:
global with sharing class TMWSendOrderService 
{
    public class OrderResponse 
    {
        string OrderNumber;
        List<string> Comments = new List<string>();
        List<string> Errors = new List<string>();
        string CaseNumber;
    }
  
  public class FuelOrdered 
    {
    string Type;
    integer Amount;
  }
    
    public class TmwOrder 
    {
        string CaseNumber;
        Date DeliveryDate;
        string DeliveryTimeframe;
        string CompanyCode;
        string Origin;
    List<FuelOrdered> FuelList = new List<FuelOrdered>();
    List<string> ErrorsFound = new List<string>();
    }
  
    webservice static String SendToTmw(string cid) 
    {
    system.debug('cid: ' + cid);
        return SendOrderToTmw(cid);
    }
  
    private static string SendOrderToTmw(string cid)
    {

        if(String.isBlank(cid))
            return 'INVALID_PARAMETERS';
        
        // get list of cases that have not yet been sent to TMW and have a valid quantity
    List<Case> sfCases = [ SELECT Id, AccountId, CaseNumber, Origin, 
        RQ_Delivery_Date__c, RQ_Delivery_Timeframe__c, 
        Fuel1_Requested_Type__c, Fuel1_Requested_Amount__c, 
        Fuel2_Requested_Type__c, Fuel2_Requested_Amount__c, 
        Fuel3_Requested_Type__c, Fuel3_Requested_Amount__c, 
        Fuel4_Requested_Type__c, Fuel4_Requested_Amount__c
      FROM Case WHERE id = :cid 
      AND TMW_Order_Num__c = '' //AND SendtoTMW__c = false 
      AND ( 
        Fuel1_Requested_Amount__c > 0 OR Fuel2_Requested_Amount__c > 0 
                OR Fuel3_Requested_Amount__c > 0 OR Fuel4_Requested_Amount__c > 0 
            ) ]; 
        // Instantiate a new http object.  Create a list of cases and send to REST service
        if(sfCases.size() == 0)
            return 'This case requires: valid quantity, requested delivery time and timeframe, and cannot be a resubmission.';
        Http h = new Http();
        HttpRequest req = new HttpRequest();
        Case sfCase = sfCases[0];
    // get company  code from translation table
    string companyCode = 
        [  
          SELECT Surrogate_ID__c FROM Translation_Table__c 
          WHERE IntegratedSite__c = :sfCase.AccountId LIMIT 1
        ].Surrogate_ID__c.replace('TMW-','');
        // populate case
        TmwOrder tmwOrder = new TmwOrder();
        tmwOrder.CaseNumber = sfCase.casenumber;
        tmwOrder.DeliveryDate = sfCase.RQ_Delivery_Date__c;
        tmwOrder.DeliveryTimeframe = sfCase.RQ_Delivery_Timeframe__c;
        tmwOrder.CompanyCode = companyCode;
        tmwOrder.Origin = sfCase.Origin;
        FuelOrdered item = new FuelOrdered();
        // create FuelOrdered list from requested types/amounts
        item.Type = sfCase.Fuel1_Requested_Type__c;
        item.Amount = Integer.valueOf(sfCase.Fuel1_Requested_Amount__c);
        if (item.Amount > 0) tmwOrder.FuelList.Add(item);
        item = new FuelOrdered();
        item.Type = sfCase.Fuel2_Requested_Type__c;
        item.Amount = Integer.valueOf(sfCase.Fuel2_Requested_Amount__c);
        if (item.Amount > 0) tmwOrder.FuelList.Add(item);
        item = new FuelOrdered();
        item.Type = sfCase.Fuel3_Requested_Type__c;
        item.Amount = Integer.valueOf(sfCase.Fuel3_Requested_Amount__c);
        if (item.Amount > 0) tmwOrder.FuelList.Add(item);
        item = new FuelOrdered();
        item.Type = sfCase.Fuel4_Requested_Type__c;
        item.Amount = Integer.valueOf(sfCase.Fuel4_Requested_Amount__c);
        if (item.Amount > 0) tmwOrder.FuelList.Add(item);
        string postString = JSON.serialize(tmwOrder);
        req.setBody(postString);
        req.setEndpoint('http://dev-tmw.sunocolp.com/api/CreateTMWOrder');
        req.setHeader('content-type','application/json');
        req.setTimeout(60000);
        req.setMethod('POST');
        //req.setCompressed(true);
        // Send the request, and return a response
        HttpResponse res = h.send(req);
        string bod = res.getBody();
        system.debug(bod);
        OrderResponse rsp = (OrderResponse)JSON.deserialize(bod, OrderResponse.class);
        system.debug('order response: ' + rsp);
        Case newCase;
        if (rsp.OrderNumber != null && rsp.OrderNumber !='')
        {
            // If Order was created, add order # to the case
            newCase = 
      [  
        Select Id, TMW_Order_Num__c, SFtoTMWSent__c, SendtoTMW__c  
                FROM Case 
                WHERE CaseNumber = :rsp.CaseNumber
            ];
            newCase.TMW_Order_Num__c = rsp.OrderNumber;
            newCase.SendtoTMW__c = true;
            newCase.SFtoTMWSent__c = date.today();
            update newCase;
        }
        else
        {
            // No order # was returned.  Mark case as sent to TMW.
            newCase = 
            [  
                 SELECT Id, SFtoTMWSent__c, SendtoTMW__c  
                 FROM Case 
                 WHERE CaseNumber = :rsp.CaseNumber
            ];
            newCase.SendtoTMW__c = true;
            newCase.SFtoTMWSent__c = date.today();
            update newCase;
        }
        // add case comments (if any)
        for (Integer i = 0; i < rsp.Comments.size(); i++)
        {
            CaseComment com = new CaseComment();
            com.ParentId = newCase.id;
            com.CommentBody = rsp.Comments[i];
            insert com;
        }        
        return 'Order submitted successfully!';
    }
}
---------------------------
Test Class:
@isTest
 
private class TMWSendOrderService_Test{
 
  @testSetup
 
  static void setupTestData(){
 
    test.startTest();
 
    Translation_Table__c translation_table_Obj = new Translation_Table__c(Integration_Status__c = false);
 
    Insert translation_table_Obj; 
 
    test.stopTest();
 
  }
 
   static testMethod void test_TmwOrder(){
 
    List<Translation_Table__c> translation_table_Obj  =  [SELECT Integration_Status__c, Name from Translation_Table__c];
 
    System.assertEquals(true,translation_table_Obj.size()>0);
 
    TMWSendOrderService obj01 = new TMWSendOrderService();
 
    TMWSendOrderService.OrderResponse obj11 = new TMWSendOrderService.OrderResponse();
 
    TMWSendOrderService.FuelOrdered obj21 = new TMWSendOrderService.FuelOrdered();
 
    TMWSendOrderService.TmwOrder obj31 = new TMWSendOrderService.TmwOrder();
 
  }
 
}

 
Hi and thanks for any help or direction.

1. Please tell me if this is good Apex class.  By good I mean, up to SF best practices, bulkification etc.
2. I need help in writing a test class.  

@RestResource(urlMapping='/TmwCaseUpdates/v1/*')

global class TmwCaseUpdates 
{

    global class OrderResponse 
    {
        string OrderNumber;
        List<string> Comments = new List<string>();
        List<string> Errors = new List<string>();
        string CaseNumber;
    }
    
  global class FuelOrderItem
    {
        string FuelOrderNumber;
        string FuelType;
        string CaseNumber;
        long FuelOrderedAmount;
        long FuelDeliveredAmount;
    }
    
  global class SalesforceUpdateCase
    {
        string Status;
        string FuelOrderStatus;
        string TMWOrderNumber;
        DateTime DeliveredTime;
        DateTime CarrierConfirmDateTime;
        string[] CaseComments;
        List<FuelOrderItem> FuelOrderItems;
        string NotifiedDate;
        string CreditAlertType;
        Date RequestDeliveryDate;
        string RequestDeliveryTimeframe;
        string Description;
        string Subject;
        string CompanyCode;
        string Origin;
    long QuantityDelivered;
    long QuantityRequested;
    }
    
    global static string GetAccountID(string companyId)
    {
        string cmpID = 'TMW-' + companyId;
        string accountID = [SELECT IntegratedSite__c FROM Translation_Table__c where Surrogate_ID__c='TMW-JMJHOR01' LIMIT 1].Id;
        return accountID;
    }
    
    @HttpPost
    global static List<OrderResponse> UpdateOrders()
    {
        string requestBody = RestContext.request.requestBody.tostring();
        System.debug('input data is ' + requestBody);
        List<SalesforceUpdateCase> updOrders = (List<SalesforceUpdateCase>)JSON.deserialize(requestBody, List<SalesforceUpdateCase>.class);
        System.debug(updOrders);
        // create OrderResponse list to be returned to the caller
        List<OrderResponse> orderList = new List<OrderResponse>();
        // loop through SalesforceUpdateCase objects.  Create new cases for valid orders.  Return any case comments
        for (SalesforceUpdateCase theOrder : updOrders)        
        {
            OrderResponse theResponse = new OrderResponse();
            List<Case> theCases = new List<Case>();
            theCases =   [ select id, status, AccountId, TMW_Order_Num__c, CaseNumber, Fuel_Order_Status__c 
                         FROM case WHERE TMW_Order_Num__c = :theOrder.TMWOrderNumber ];
            System.debug('order # is ' + theOrder.TMWOrderNumber);
            if (theCases.size() > 1)
            {
                theResponse.Errors.Add('Case ' + theCases[0].CaseNumber + 'for TMW Order ' + theOrder.TMWOrderNumber + ' already exists');
                System.debug('Case ' + theCases[0].CaseNumber + 'for TMW Order ' + theOrder.TMWOrderNumber + ' already exists');
                theResponse.OrderNumber = theOrder.TMWOrderNumber;
                theResponse.CaseNumber = theCases[0].CaseNumber;
                orderList.Add(theResponse);
            } else if (theCases.size() == 0)
            {
                // Create new SF case
                Case newCase = new Case();
        newCase.Status = theOrder.Status;
        newCase.Fuel_Order_Status__c = theOrder.FuelOrderStatus;
        newCase.TMW_Order_Num__c = theOrder.TMWOrderNumber;
        newCase.TMWSentDt__c = Date.today();
        newCase.Credit_Alert_Type__c = theOrder.CreditAlerttype;
                newCase.Delivered_Time__c = theOrder.DeliveredTime;
                // Get the company code from the translation table. Company codes are stored as TMW-xxxxx
                string translatedCompanyCode = 'TMW-' + theOrder.CompanyCode;
                System.debug(translatedCompanyCode);
                newCase.AccountId = [SELECT IntegratedSite__c 
                                     FROM Translation_Table__c 
                                     WHERE Surrogate_ID__c = :translatedCompanyCode].IntegratedSite__c;
                newCase.RQ_Delivery_Timeframe__c = theOrder.RequestDeliveryTimeframe;
                newCase.RQ_Delivery_Date__c = theOrder.RequestDeliveryDate;
                newCase.Origin = theOrder.Origin;
                integer i = 0;
                for (FuelOrderItem item : theOrder.FuelOrderItems)
                {
                    i++;
                    switch on i
                    {
                        when 1 
                        {
                            newCase.Fuel1_Requested_Type__c = item.FuelType;
                            if (theOrder.FuelOrderStatus == 'Delivered')
              {
                newCase.Fuel1_Delivered_Type__c = item.FuelType;
                                newCase.Fuel1_Delivered_Amount__c = item.FuelDeliveredAmount;
                        }
                        }
                        when 2 
                        {
                            newCase.Fuel2_Requested_Type__c = item.FuelType;
                            if (theOrder.FuelOrderStatus == 'Delivered')
              {
                newCase.Fuel2_Delivered_Type__c = item.FuelType;
                                newCase.Fuel2_Delivered_Amount__c = item.FuelDeliveredAmount;
                        }                            
                        }
                        when 3 
                        {
                            newCase.Fuel3_Requested_Type__c = item.FuelType;
                            if (theOrder.FuelOrderStatus == 'Delivered')
              {
                newCase.Fuel3_Delivered_Type__c = item.FuelType;
                                newCase.Fuel3_Delivered_Amount__c = item.FuelDeliveredAmount;
                        }                                            
                        }
                        when 4 
                        {
                            newCase.Fuel4_Requested_Type__c = item.FuelType;
              if (theOrder.FuelOrderStatus == 'Delivered')
              {
                newCase.Fuel4_Delivered_Type__c = item.FuelType;
                                newCase.Fuel4_Delivered_Amount__c = item.FuelDeliveredAmount;
                        }                
                        }
                    }
                }
                System.debug('about to insert case');
        Insert newCase;
        theResponse.OrderNumber = theOrder.TMWOrderNumber;
        theResponse.CaseNumber = newCase.CaseNumber;
        orderList.Add(theResponse);
            }
      else
            {
                // Update existing case
                Case updCase = theCases[0];
                if (updCase.Fuel_Order_Status__c != theOrder.FuelOrderStatus)
                {
                    updCase.Status = theOrder.Status;
                    updCase.Fuel_Order_Status__c = theOrder.FuelOrderStatus;
                    updCase.TMW_Order_Num__c = theOrder.TMWOrderNumber;
                    updCase.TMWSentDt__c = Date.today();
                    updCase.Credit_Alert_Type__c = theOrder.CreditAlerttype;
                    // update Delivered fields for completed orders
                    if (theOrder.FuelOrderStatus == 'Delivered')
                    {
                        for (FuelOrderItem item : theOrder.FuelOrderItems)
            {
                            if (updCase.Fuel1_Requested_Type__c == item.FuelType)
                            { 
                                updCase.Fuel1_Delivered_Amount__c = item.FuelDeliveredAmount; 
                            }
                            else if (updCase.Fuel2_Requested_Type__c == item.FuelType)
                            { 
                                updCase.Fuel2_Delivered_Amount__c = item.FuelDeliveredAmount; 
                            }
                            else if (updCase.Fuel3_Requested_Type__c == item.FuelType)
                            { 
                                updCase.Fuel3_Delivered_Amount__c = item.FuelDeliveredAmount; 
                            }
                            else if (updCase.Fuel4_Requested_Type__c == item.FuelType)
                            { 
                                updCase.Fuel4_Delivered_Amount__c = item.FuelDeliveredAmount; 
                            }
            }
                        updCase.Delivered_Time__c = theOrder.DeliveredTime;
                        updCase.Carrier_Confirm_Date_Time__c = theOrder.CarrierConfirmDateTime;
                    }
                    System.debug('about to update case');
            Update updCase;
                    theResponse.OrderNumber = theOrder.TMWOrderNumber;
                    theResponse.CaseNumber = updCase.CaseNumber;
                    orderList.Add(theResponse);
                }
            }            
        }
        return orderList;
    }
}
 
Hi and thanks for any help or direction.

1. Please tell me if this is good Apex class.  By good I mean, up to SF best practices, bulkification etc.
2. I need help in writing a test class.  

@RestResource(urlMapping='/TmwCaseUpdates/v1/*')

global class TmwCaseUpdates 
{

    global class OrderResponse 
    {
        string OrderNumber;
        List<string> Comments = new List<string>();
        List<string> Errors = new List<string>();
        string CaseNumber;
    }
    
  global class FuelOrderItem
    {
        string FuelOrderNumber;
        string FuelType;
        string CaseNumber;
        long FuelOrderedAmount;
        long FuelDeliveredAmount;
    }
    
  global class SalesforceUpdateCase
    {
        string Status;
        string FuelOrderStatus;
        string TMWOrderNumber;
        DateTime DeliveredTime;
        DateTime CarrierConfirmDateTime;
        string[] CaseComments;
        List<FuelOrderItem> FuelOrderItems;
        string NotifiedDate;
        string CreditAlertType;
        Date RequestDeliveryDate;
        string RequestDeliveryTimeframe;
        string Description;
        string Subject;
        string CompanyCode;
        string Origin;
    long QuantityDelivered;
    long QuantityRequested;
    }
    
    global static string GetAccountID(string companyId)
    {
        string cmpID = 'TMW-' + companyId;
        string accountID = [SELECT IntegratedSite__c FROM Translation_Table__c where Surrogate_ID__c='TMW-JMJHOR01' LIMIT 1].Id;
        return accountID;
    }
    
    @HttpPost
    global static List<OrderResponse> UpdateOrders()
    {
        string requestBody = RestContext.request.requestBody.tostring();
        System.debug('input data is ' + requestBody);
        List<SalesforceUpdateCase> updOrders = (List<SalesforceUpdateCase>)JSON.deserialize(requestBody, List<SalesforceUpdateCase>.class);
        System.debug(updOrders);
        // create OrderResponse list to be returned to the caller
        List<OrderResponse> orderList = new List<OrderResponse>();
        // loop through SalesforceUpdateCase objects.  Create new cases for valid orders.  Return any case comments
        for (SalesforceUpdateCase theOrder : updOrders)        
        {
            OrderResponse theResponse = new OrderResponse();
            List<Case> theCases = new List<Case>();
            theCases =   [ select id, status, AccountId, TMW_Order_Num__c, CaseNumber, Fuel_Order_Status__c 
                         FROM case WHERE TMW_Order_Num__c = :theOrder.TMWOrderNumber ];
            System.debug('order # is ' + theOrder.TMWOrderNumber);
            if (theCases.size() > 1)
            {
                theResponse.Errors.Add('Case ' + theCases[0].CaseNumber + 'for TMW Order ' + theOrder.TMWOrderNumber + ' already exists');
                System.debug('Case ' + theCases[0].CaseNumber + 'for TMW Order ' + theOrder.TMWOrderNumber + ' already exists');
                theResponse.OrderNumber = theOrder.TMWOrderNumber;
                theResponse.CaseNumber = theCases[0].CaseNumber;
                orderList.Add(theResponse);
            } else if (theCases.size() == 0)
            {
                // Create new SF case
                Case newCase = new Case();
        newCase.Status = theOrder.Status;
        newCase.Fuel_Order_Status__c = theOrder.FuelOrderStatus;
        newCase.TMW_Order_Num__c = theOrder.TMWOrderNumber;
        newCase.TMWSentDt__c = Date.today();
        newCase.Credit_Alert_Type__c = theOrder.CreditAlerttype;
                newCase.Delivered_Time__c = theOrder.DeliveredTime;
                // Get the company code from the translation table. Company codes are stored as TMW-xxxxx
                string translatedCompanyCode = 'TMW-' + theOrder.CompanyCode;
                System.debug(translatedCompanyCode);
                newCase.AccountId = [SELECT IntegratedSite__c 
                                     FROM Translation_Table__c 
                                     WHERE Surrogate_ID__c = :translatedCompanyCode].IntegratedSite__c;
                newCase.RQ_Delivery_Timeframe__c = theOrder.RequestDeliveryTimeframe;
                newCase.RQ_Delivery_Date__c = theOrder.RequestDeliveryDate;
                newCase.Origin = theOrder.Origin;
                integer i = 0;
                for (FuelOrderItem item : theOrder.FuelOrderItems)
                {
                    i++;
                    switch on i
                    {
                        when 1 
                        {
                            newCase.Fuel1_Requested_Type__c = item.FuelType;
                            if (theOrder.FuelOrderStatus == 'Delivered')
              {
                newCase.Fuel1_Delivered_Type__c = item.FuelType;
                                newCase.Fuel1_Delivered_Amount__c = item.FuelDeliveredAmount;
                        }
                        }
                        when 2 
                        {
                            newCase.Fuel2_Requested_Type__c = item.FuelType;
                            if (theOrder.FuelOrderStatus == 'Delivered')
              {
                newCase.Fuel2_Delivered_Type__c = item.FuelType;
                                newCase.Fuel2_Delivered_Amount__c = item.FuelDeliveredAmount;
                        }                            
                        }
                        when 3 
                        {
                            newCase.Fuel3_Requested_Type__c = item.FuelType;
                            if (theOrder.FuelOrderStatus == 'Delivered')
              {
                newCase.Fuel3_Delivered_Type__c = item.FuelType;
                                newCase.Fuel3_Delivered_Amount__c = item.FuelDeliveredAmount;
                        }                                            
                        }
                        when 4 
                        {
                            newCase.Fuel4_Requested_Type__c = item.FuelType;
              if (theOrder.FuelOrderStatus == 'Delivered')
              {
                newCase.Fuel4_Delivered_Type__c = item.FuelType;
                                newCase.Fuel4_Delivered_Amount__c = item.FuelDeliveredAmount;
                        }                
                        }
                    }
                }
                System.debug('about to insert case');
        Insert newCase;
        theResponse.OrderNumber = theOrder.TMWOrderNumber;
        theResponse.CaseNumber = newCase.CaseNumber;
        orderList.Add(theResponse);
            }
      else
            {
                // Update existing case
                Case updCase = theCases[0];
                if (updCase.Fuel_Order_Status__c != theOrder.FuelOrderStatus)
                {
                    updCase.Status = theOrder.Status;
                    updCase.Fuel_Order_Status__c = theOrder.FuelOrderStatus;
                    updCase.TMW_Order_Num__c = theOrder.TMWOrderNumber;
                    updCase.TMWSentDt__c = Date.today();
                    updCase.Credit_Alert_Type__c = theOrder.CreditAlerttype;
                    // update Delivered fields for completed orders
                    if (theOrder.FuelOrderStatus == 'Delivered')
                    {
                        for (FuelOrderItem item : theOrder.FuelOrderItems)
            {
                            if (updCase.Fuel1_Requested_Type__c == item.FuelType)
                            { 
                                updCase.Fuel1_Delivered_Amount__c = item.FuelDeliveredAmount; 
                            }
                            else if (updCase.Fuel2_Requested_Type__c == item.FuelType)
                            { 
                                updCase.Fuel2_Delivered_Amount__c = item.FuelDeliveredAmount; 
                            }
                            else if (updCase.Fuel3_Requested_Type__c == item.FuelType)
                            { 
                                updCase.Fuel3_Delivered_Amount__c = item.FuelDeliveredAmount; 
                            }
                            else if (updCase.Fuel4_Requested_Type__c == item.FuelType)
                            { 
                                updCase.Fuel4_Delivered_Amount__c = item.FuelDeliveredAmount; 
                            }
            }
                        updCase.Delivered_Time__c = theOrder.DeliveredTime;
                        updCase.Carrier_Confirm_Date_Time__c = theOrder.CarrierConfirmDateTime;
                    }
                    System.debug('about to update case');
            Update updCase;
                    theResponse.OrderNumber = theOrder.TMWOrderNumber;
                    theResponse.CaseNumber = updCase.CaseNumber;
                    orderList.Add(theResponse);
                }
            }            
        }
        return orderList;
    }
}