0

So requirements are as follows. I need to parse out this information from a JSON string and insert some detail records after the parents creation. I have the following -

Can anyone tell me what im doing wrong here? The detail records aren't inserting on the insert. The trigger is after insert.

public static void consumeObject(JSONParser parser) {

        Integer depth = 0;
        do {
            JSONToken curr = parser.getCurrentToken();
            if (curr == JSONToken.START_OBJECT || 
                curr == JSONToken.START_ARRAY) {
                depth++;
            } else if (curr == JSONToken.END_OBJECT ||
                curr == JSONToken.END_ARRAY) {
                depth--;
            }
        } while (depth > 0 && parser.nextToken() != null);
    }

    public String EntryType {get;set;} 
    public String ItemQuantity {get;set;} 
    public String ItemUnit {get;set;} 
    public String ItemCost {get;set;} 
    public String ItemDescription {get;set;} 
    public String ItemPercentComplete {get;set;} 

    public Class JsonTrigger {
    public JsonTrigger(JSONParser parser) {

        while (parser.nextToken() != JSONToken.END_OBJECT) {
            if (parser.getCurrentToken() == JSONToken.FIELD_NAME) {
                String text = parser.getText();
                if (parser.nextToken() != JSONToken.VALUE_NULL) {
                    if (text == 'DARS__Entry_Type__c') {
                        EntryType = parser.getText();
                    } else if (text == 'DARS__Item_Quantity__c') {
                        ItemQuantity = parser.getText();
                    } else if (text == 'DARS__Item_Unit__c') {
                        ItemUnit = parser.getText();
                    } else if (text == 'DARS__Item_Cost__c') {
                        ItemCost = parser.getText();
                    } else if (text == 'DARS__Item_Description__c') {
                        ItemDescription = parser.getText();
                    } else if (text == 'DARS__Item_Percent_Completed__c') {
                        ItemPercentComplete = parser.getText();
                    } else {
                        System.debug(LoggingLevel.WARN, 'Root consuming unrecognized property: '+text);
                        consumeObject(parser);
                        }
                    }
                }
            }
        }
    }


    public static Mobile_Details_Trigger_Handler parse(String json) {


              DSRDetails = [select  
                            id, 
                            name, 
                            Item_Description__c, 
                            Item_Unit__c, 
                            Item_Currency__c,
                            Item_Cost__c,
                            Item_Quantity__c,
                            Item_Percent_Completed__c
                            from DDIR_Detail__c 
                            where  Entry_Type__c = 'Emergency Repair' and DDIR_Detail__c = :ApexPages.currentPage().getParameters().get('iID')  
        ];
        SObject record = trigger.new[0];
        Map<String, Object> data = (Map<String, Object>)JSON.deserializeUntyped(record.get('JSON_Data__c'));


        DSRDetails.name = 'test';
        DSRDetails.Item_Description__c = EntryType;
        DSRDetails.Item_Unit__c = ItemUnit;
        DSRDetails.Item_Percent_Completed__c = decimal.valueOf(ItemPercentComplete);
        DSRDetails.Item_Quantity__c = decimal.valueOf(ItemQuantity); 


        insert DSRDetails;
    return new Mobile_Details_Trigger_Handler(System.JSON.createParser(json));
    }
BarCotter
  • 12,331
  • 4
  • 37
  • 58
Christian
  • 507
  • 1
  • 7
  • 18
  • 1
    Add some debug statements to find out what is wrong - see How do I start to debug my own Apex code?. I find the code you have posted hard to follow; the simplest may to parse JSON is to use the code generated by http://json2apex.herokuapp.com/. – Keith C Nov 24 '15 at 20:29
  • Actually this is pretty much straight out of the herokuapp. It's still not inserting the correct records – Christian Nov 24 '15 at 20:37
  • Is your deserialisation creating new sobjects ok ? Do you have a try catch around insertion with a debug statement in catch to see whats going on ? That would be very helpful to know... – CloudHugger Nov 24 '15 at 21:24
  • I added a couple system.debug's around the insert statement earlier but I couldn't get anything to pull up in the logs. The code is only working if the record is inserted from a mobile device. – Christian Nov 24 '15 at 22:22

0 Answers0