1

I'm running into a very odd issue and hoping someone else has also experienced this.

I'm trying to build an object to send to an application called Processmaker and it requires very a specific object definition. I'll leave an example from their documentation below. So my idea was to build a class so that I could easily create the structure required and send the request. The example they gave is not in Apex, but I figured I should be able translate it to Apex fairly easy. I seem to be wrong, unless I'm missing something.

The problem I'm having, is that when I run json.serialize(buildCase()); it only serializes the Map variable and not the entire object.

When I do a System.debug(cs1) it returns the correct object structure, but when I do System.debug(json.serialize(cs1) it only returns the Map<String,String> variable.

Has anyone else experienced something similar?

Object Structure Needed The aVars variable is the structure I need for my object

$aCaseVars = array(
   "client"            => 'Acme Inc.',                        //textbox with string variable
   "amount"            => 23456.99,                           //textbox with float variable
   "address"           => "245 Stars Av.\nHollywood CA 12345",//textarea with string variable
   "dateDue"           => '2015-12-31',                       //datetime
   "dateDue_label"     => '12/31/2015',                       //datetime label in MM/DD/YYYY format
   "deliveryTime"      => '2015-11-25 17:55:38',              //datetime
   "serviceType"       => 'accounting',                       //dropdown with string variable
   "serviceType_label" => 'Accounting Review',                //dropdown label
   "hasContract"       => array('1'),                         //checkbox with boolean variable
   "hasContract_label" => 'yes',                              //checkbox label
   "howContact"        => array('fax', 'email', 'telephone'), //checkbox group with string variable
   "howContact_label"  => '["Fax","Send Email","Telephone"]', //checkbox group label in JSON string
));
$aVars = array(
   'pro_uid'   => '325089587550b34ab5471f8086074839',
   'tas_uid'   => '491406639550b34b27f0b34088369199',
   'variables' => $aCaseVars
);

This is what I put together.

Case Structure Class

public class CaseStructure {

    public String pro_uid;
    public String tas_uid;
    public Map<String, String> variables = new Map<String, String>();

}

buildCase()

    public static CaseStructure buildCase() {

        Processmaker_Create_Case p0 = new Processmaker_Create_Case();
        CaseStructure cs1 = new CaseStructure();
        cs1.pro_uid = '1234567890';
        cs1.tas_uid = '0987654321';
        cs1.variables.put('ServiceRep', 'Bob Barker');
        cs1.variables.put('SubmitDate', '10-12-2022');
        cs1.variables.put('CustomerName', 'Big Guy');
        cs1.variables.put('Company', 'CompanyRKHL');
        cs1.variables.put('ContactName', 'Steve Harvey');
        cs1.variables.put('CustomerContactNumber', '8031234567');
        cs1.variables.put('CustomerEmailAddress', '[email protected]');
        cs1.variables.put('CustomerAddress', '1234 Magic Lane');
        cs1.variables.put('ApartmentSelection', 'AptSelectionApartment');
        cs1.variables.put('City', 'Tega Cay');
        cs1.variables.put('State', 'StateSC');
        cs1.variables.put('Zipcode', '29708');
        cs1.variables.put('SubdivisionSelection', 'SubSelectionNA');
        cs1.variables.put('RequestTypeMain', 'RequestTypeNewConstruction');
        cs1.variables.put('ServiceTypeRequested', 'ServiceTypeRequestedTelephone');

        System.debug('PMCase Built: '+cs1);
        System.debug(LoggingLevel.ERROR, json.serialize(cs1));
        System.debug('PMCase Built Serialized: '+json.serialize(cs1));
        return cs1;
    }
2
  • 1
    Have you tried deserializing the serialized object and confirming the values aren't there by debugging them from the deserialized object? Oct 15, 2022 at 15:33
  • I have not, thanks for that! I'll give it a try!
    – nigeldendy
    Oct 16, 2022 at 14:48

1 Answer 1

2

Your code looks fine, and running it as anonymous apex backs that up.

Given the code, modified slightly to work with anon apex (the changes shouldn't make any difference in execution)

public class CaseStructure {

    public String pro_uid;
    public String tas_uid;
    public Map<String, String> variables = new Map<String, String>();

}

CaseStructure cs1 = new CaseStructure();
cs1.pro_uid = '1234567890';
cs1.tas_uid = '0987654321';
cs1.variables.put('ServiceRep', 'Bob Barker');
cs1.variables.put('SubmitDate', '10-12-2022');
cs1.variables.put('CustomerName', 'Big Guy');
cs1.variables.put('Company', 'CompanyRKHL');
cs1.variables.put('ContactName', 'Steve Harvey');
cs1.variables.put('CustomerContactNumber', '8031234567');
cs1.variables.put('CustomerEmailAddress', '[email protected]');
cs1.variables.put('CustomerAddress', '1234 Magic Lane');
cs1.variables.put('ApartmentSelection', 'AptSelectionApartment');
cs1.variables.put('City', 'Tega Cay');
cs1.variables.put('State', 'StateSC');
cs1.variables.put('Zipcode', '29708');
cs1.variables.put('SubdivisionSelection', 'SubSelectionNA');
cs1.variables.put('RequestTypeMain', 'RequestTypeNewConstruction');
cs1.variables.put('ServiceTypeRequested', 'ServiceTypeRequestedTelephone');

System.debug('PMCase Built: '+cs1);
System.debug(LoggingLevel.ERROR, json.serialize(cs1));
System.debug('PMCase Built Serialized: '+json.serialize(cs1));

I get the following result (which I have formatted in 'pretty-print' style)

{
    "variables":
        {
            "ServiceTypeRequested":"ServiceTypeRequestedTelephone",
            "RequestTypeMain":"RequestTypeNewConstruction",
            "SubdivisionSelection":"SubSelectionNA",
            "Zipcode":"29708",
            "State":"StateSC",
            "City":"Tega Cay",
            "ApartmentSelection":"AptSelectionApartment",
            "CustomerAddress":"1234 Magic Lane",
            "CustomerEmailAddress":"[email protected]",
            "CustomerContactNumber":"8031234567",
            "ContactName":"Steve Harvey",
            "Company":"CompanyRKHL",
            "CustomerName":"Big Guy",
            "SubmitDate":"10-12-2022",
            "ServiceRep":"Bob Barker"
        },
    "tas_uid":"0987654321",
    "pro_uid":"1234567890"
}

Serialization doesn't guarantee any order, but as long as you don't add the transient keyword to a variable it should get serialized somewhere.

Your tas_uid and pro_uid should exist if you examine your output, just not necessarily before variables.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .