• Janet Putnam 2
  • NEWBIE
  • 10 Points
  • Member since 2017

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 3
    Questions
  • 3
    Replies
I am trying to display json data in a visual force page. In the process of that I'm using Execute Anonymous to pinpoint my errors. I have code sample from a trail that works:
 
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('https://th-apex-http-callout.herokuapp.com/animals');
request.setMethod('GET');
HttpResponse response = http.send(request);
// If the request is successful, parse the JSON response.
if (response.getStatusCode() == 200) {
    // Deserialize the JSON string into collections of primitive data types.
    Map<String, Object> results = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());
    // Cast the values in the 'animals' key as a list
    List<Object> animals = (List<Object>) results.get('animals');
    System.debug('Received the following animals:');
    for (Object animal: animals) {
        System.debug(animal);
    }
}

The above url produces json that looks like this:
{"animals":["majestic badger","fluffy bunny","scary bear","chicken"]}

So, my code is here:
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('http://12.15..13/GPDataAPI/api/values');
request.setMethod('GET');
HttpResponse response = http.send(request);
system.debug('response: ' + response.getBody());
// If the request is successful, parse the JSON response.
if (response.getStatusCode() == 200) {
    // Deserialize the JSON string into collections of primitive data types.
    Map<String, Object> results = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());
   // Cast the values in the 'animals' key as a list
   // This is where my issue is because my array doesn't have a 'named' element
    List<Object> payments = (List<Object>) results.get('payments');
    System.debug('Received the following animals:');
    for (Object payment: payments) {
        System.debug(payment);
    }
}

If I highlight and run only to the system.debug the code works and I can see in the logs that the body is correct. 
The source json for my call is:
[{"CustNmbr":"F592611 ","DocNmbr":"EBM01006295 ","DocDate":"2017-11-21T00:00:00","CheckNmbr":" ","Amt":100.00000,"Paid":"Paid","Posted":"2017-11-22T00:00:00","UnPaid":"100.00000"}]

I believe my problem is here:
List<Object> payments = (List<Object>) results.get('payments');
    System.debug('Received the following animals:');
    for (Object payment: payments) {
        System.debug(payment);
    }
and is because my source json is just an array of an object without a name (payments) where the SF code that does work has a key ('animals'). 

Any suggestions would be greatly appreciated.
 
The module is Improve Data Quality for a Cleaning supply App, the step is Create a Validation Rule. I get the typical unexpected error while verifying. I created a new playground, got the same error. I opened the developer console to look at the log created when clicking the verify button and no log shows up. I opened an old trusty playground and without completing the step I just clicked the button, same error ( I would have expected a this field doesn't exist error) and no log created. 

It's almost like the verify step button is broke.
I'm trying to return an Order with an OrderLine as a JSON string.
I create two classes Order and OrderLines. Order contains a list of OrderLines.
public class Order
    {
        public string CustNmbr;
        public string ShipMthd;
        public string Refrence;
        public double FrtAmnt;
        public double TaxAmnt;
        public string Cntcprsn;
        public string ShipToName;
        public string Address1;
        public string Address3;
        public string City;
        public string State;
        public string ZipCode;
        public string PhNumbr1;
        public List<OrderLines> SOP10200;
    }
    public class OrderLines
    {
        public string itemnmbr;
        public decimal quantity;
        public string unitprce;
        public string uofm;
    }
Then I populate the fields from a query.
public static string getOrderFromContract(string contractID){
        sObject myOrder = [select id, name, contact_city__c, contact_state__c, contact_street_address__c, contact_zip_code__c, contact_email_address__c, contact_phone_number__c, contact_name_export__c, fns_number__c, number_of_device_sets_needed__c, processor_id__c, raps2_id__c, saturdaydelivery__c, store_name__c, terminal_id__c, terminal_type__c from cde_contract__c where id = :contractID];
        
        Order newOrder = new Order();
        OrderLines newLines = new OrderLines();
        
        string fns = (String)myOrder.get('fns_number__c');
        
        newOrder.custnmbr = ('f' + fns);
        newOrder.state = (String)myOrder.get('contact_state__c');
        newOrder.shipmthd = 'FedEX';
        newOrder.refrence = 'SalesForce Contract';
        newOrder.frtamnt = 0.00;
        newOrder.taxamnt = 0.00;
        newOrder.cntcprsn = (String)myOrder.get('contact_name_export__c');
        newOrder.shiptoname = (String)myOrder.get('store_name__c');
        newOrder.address1 = (String)myOrder.get('contact_street_address__c');
        newOrder.address3 = (String)myOrder.get('contact_email_address__c');
        newOrder.city = (String)myOrder.get('contact_city__c');
        newOrder.state = (String)myOrder.get('contact_state__c');
        newOrder.zipcode = (String)myOrder.get('contact_zip_code__c');
        newOrder.phnumbr1 = (String)myOrder.get('contact_phone_number__c');
        
        newLines.itemnmbr = '120604';
        newLines.quantity = (decimal)myOrder.get('number_of_device_sets_needed__c');
        newLines.uofm = 'roll';
                 
        return JSON.serialize(newOrder);
    }
This works to create the order header but I can't get the order lines part to work.
newOrder.SOP10200 = newLines;
I need something to assign the newLines item to the SOP10200 but I don't know how to get it done. The above results in the following error.
Illegal assignment from SalesOrderFromContract.OrderLines to List<SalesOrderFromContract.OrderLines>
Thanks for any help.




 
The module is Improve Data Quality for a Cleaning supply App, the step is Create a Validation Rule. I get the typical unexpected error while verifying. I created a new playground, got the same error. I opened the developer console to look at the log created when clicking the verify button and no log shows up. I opened an old trusty playground and without completing the step I just clicked the button, same error ( I would have expected a this field doesn't exist error) and no log created. 

It's almost like the verify step button is broke.
I'm trying to return an Order with an OrderLine as a JSON string.
I create two classes Order and OrderLines. Order contains a list of OrderLines.
public class Order
    {
        public string CustNmbr;
        public string ShipMthd;
        public string Refrence;
        public double FrtAmnt;
        public double TaxAmnt;
        public string Cntcprsn;
        public string ShipToName;
        public string Address1;
        public string Address3;
        public string City;
        public string State;
        public string ZipCode;
        public string PhNumbr1;
        public List<OrderLines> SOP10200;
    }
    public class OrderLines
    {
        public string itemnmbr;
        public decimal quantity;
        public string unitprce;
        public string uofm;
    }
Then I populate the fields from a query.
public static string getOrderFromContract(string contractID){
        sObject myOrder = [select id, name, contact_city__c, contact_state__c, contact_street_address__c, contact_zip_code__c, contact_email_address__c, contact_phone_number__c, contact_name_export__c, fns_number__c, number_of_device_sets_needed__c, processor_id__c, raps2_id__c, saturdaydelivery__c, store_name__c, terminal_id__c, terminal_type__c from cde_contract__c where id = :contractID];
        
        Order newOrder = new Order();
        OrderLines newLines = new OrderLines();
        
        string fns = (String)myOrder.get('fns_number__c');
        
        newOrder.custnmbr = ('f' + fns);
        newOrder.state = (String)myOrder.get('contact_state__c');
        newOrder.shipmthd = 'FedEX';
        newOrder.refrence = 'SalesForce Contract';
        newOrder.frtamnt = 0.00;
        newOrder.taxamnt = 0.00;
        newOrder.cntcprsn = (String)myOrder.get('contact_name_export__c');
        newOrder.shiptoname = (String)myOrder.get('store_name__c');
        newOrder.address1 = (String)myOrder.get('contact_street_address__c');
        newOrder.address3 = (String)myOrder.get('contact_email_address__c');
        newOrder.city = (String)myOrder.get('contact_city__c');
        newOrder.state = (String)myOrder.get('contact_state__c');
        newOrder.zipcode = (String)myOrder.get('contact_zip_code__c');
        newOrder.phnumbr1 = (String)myOrder.get('contact_phone_number__c');
        
        newLines.itemnmbr = '120604';
        newLines.quantity = (decimal)myOrder.get('number_of_device_sets_needed__c');
        newLines.uofm = 'roll';
                 
        return JSON.serialize(newOrder);
    }
This works to create the order header but I can't get the order lines part to work.
newOrder.SOP10200 = newLines;
I need something to assign the newLines item to the SOP10200 but I don't know how to get it done. The above results in the following error.
Illegal assignment from SalesOrderFromContract.OrderLines to List<SalesOrderFromContract.OrderLines>
Thanks for any help.