3

My mock body looks like :

'{"totalSize": 1, "done":true,
"nextRecordsUrl":"WWW.testurl.com","records":[{"Name":"Fund1", "aduro__Date_of_Inception__c":"2011-10-09 00:00:00", "Accept_ACH_Payments__c":true,"ACH_Public_Key__c":"pk_sandbox_MTU4MmJhMDgtMTU4OC00YmVh","ACH_Secret_Key__c"
:"sk_sandbox_OTc0ZTVkMzQtYmM5Mi00MWU5"}]}'

And my wrapper looks like this:

public class QueryResponse {
        public Integer totalSize {get; set;}
        public Boolean done {get; set;}
        public String nextRecordsUrl { get; set; }
        public List<SObject> records {get; set;}
    }

The SObject in my case is fund__c. When i use i am getting the error as System.JSONException: Nested object for polymorphic foreign key must have an attributes field before any other fields.

2 Answers 2

5

The deserialization needs to know what concrete type to use and that is signalled by an attributes field:

"records":[{"attributes": {"type": "fund__c"}, "Name":"Fund1", ...

so if the system that generates the JSON can provide that, get it added.

Or if the records are always fund__c:

public List<fund__c> records {get; set;}

If the system that generates the JSON can't provide that, you will have to write more involved code that walks through the JSON and infers the type that you can then hard-code in the logic.

1
  • I wish the error response had a reference to this. I keep ending up on this post :) Nov 17, 2021 at 17:17
-1

For me, i was working with Wistia and the payload is like:

{
  "hook": {
    "uuid": "bb812395-8d23-4285-9278-9ca014209ae7"
  },
  "events": [{
    "uuid": "09e67473ecb3a000",
    "type": "viewing_session.percent_watched",
    "payload": {
      "visitor": {
        "id": "v20150227_bee7121c-04bb-42ae-abc9-5327f536df0f"
      },
      "viewing_session": {
        "id": "v20150225_52967e29-8196-42d8-9dc6-952c1d9ec87f"
      },
      "percent_watched": 25,
      "media": {
        "url": "http://dave.wistia.com/medias/mmgzi8h6yh",
        "thumbnail": {
          "url": "http://embed.wistia.com/deliveries/23c4846eecd462beab57a508dff7f052a5faf065.jpg?image_crop_resized=200x120"
        },
        "name": "Lenny Eating Peanuts",
        "id": "mmgzi8h6yh",
        "duration": 71.912
      }
    },
    "metadata": {
      "account_id": "8lq25o0p9c"
    },
    "generated_at": "2016-03-30T15:14:01Z"
  }]
}

When i was trying to access the element String, sObject type variable it was showing me the defined error. Finally i modified the class and it worked.

global class hookWrapper {
        String uuid;
    }

    global class ThumbnailWrapper {
        String url;
    }

    global class VisitorWrapper {
        String id;
    }

    global class ViewingSessionWrapper {
        String id;
    }

    global class MetadataWrapper {
        String account_id;
    }

    global class MediaWrapper {
        String url;
        String name;
        String id;
        Double duration;
        ThumbnailWrapper thumbnail;
    }

    global class payloadWrapper {
        Integer percent_watched;
        MediaWrapper media;
        VisitorWrapper visitor;
        ViewingSessionWrapper viewing_session;
    }

    global class eventWrapper {
        String uuid;
        String type;
        payloadWrapper payload;
        MetadataWrapper metadata;
        String generated_at;
    }

    @HttpPost
    global static void check(hookWrapper hook, eventWrapper[] events) {
        eventWrapper event = events[0];
        String type = event.type;
        Integer percentWatched = event.payload.percent_watched;
        String contentUrl = event.payload.media.url;
        String name = event.payload.media.name;
        String mediaId = event.payload.media.id;
        String accountId = event.metadata.account_id;
        String content = 'content : ' + contentUrl + ' name: ' + name + ' type: ' + type + ' media_Id: ' + mediaId + ' account_id: ' + accountId + ' percent watched: ' + percentWatched;

    }
2
  • Are you sure you were getting the same error? This particular error message seems like it should be exclusive to SObjects, not Apex classes (because the "polymorphic relationship" bit of the error is referring to things like the OwnerId field on Case (which can be a User or a Group) or the WhatId field on Task.
    – Derek F
    Dec 28, 2023 at 19:32
  • @DerekF Yes I'm sure about this error. I copied the error message, searched, and reached this point. I hoped someone may get help from this answer.
    – sh6210
    Dec 28, 2023 at 21:12

You must log in to answer this question.

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