Exemple #1
0
        /**
         * This is taken from https://gist.github.com/afawcett/8dbfc0e1d8c43c982881.
         *
         * This method works on the principle that serializing and deserialising child records is supported
         *
         *   System.assertEquals(1, ((List<Master__c>)
         *    JSON.deserialize(
         *	    JSON.serialize(
         *         [select Id, Name,
         *            (select Id, Name from Children__r) from Master__c]), List<Master__c>.class))
         *               [0].Children__r.size());
         *
         * This method results internally in constructing this JSON, before deserialising it back into SObject's
         *
         *		[
         *		    {
         *		        "attributes": {
         *		            "type": "Master__c",
         *		            "url": "/services/data/v32.0/sobjects/Master__c/a0YG0000005Jn5uMAC"
         *		        },
         *		        "Name": "Fred",
         *		        "Id": "a0YG0000005Jn5uMAC",
         *		        "Children__r": {
         *		            "totalSize": 1,
         *		            "done": true,
         *		            "records": [
         *		                {
         *		                    "attributes": {
         *		                        "type": "Child__c",
         *		                        "url": "/services/data/v32.0/sobjects/Child__c/a0ZG0000006JGPAMA4"
         *		                    },
         *		                    "Name": "Bob",
         *		                    "Id": "a0ZG0000006JGPAMA4",
         *		                    "Master__c": "a0YG0000005Jn5uMAC"
         *		                }
         *		            ]
         *		        }
         *      ]
         */
        public static object makeRelationship(Type parentsType, List <SObject> parents, SObjectField relationshipField, List <List <SObject> > children)
        {
            // Find out more about this relationship...
            string relationshipFieldName = relationshipField.getDescribe().getName();
            DescribeSObjectResult           parentDescribe     = parents.getSObjectType().getDescribe();
            List <Schema.ChildRelationship> childRelationships = parentDescribe.getChildRelationships();
            string relationshipName = null;

            foreach (Schema.ChildRelationship childRelationship in childRelationships)
            {
                if (childRelationship.getField() == relationshipField)
                {
                    relationshipName = childRelationship.getRelationshipName();
                    break;
                }
            }

            // Stream the parsed JSON representation of the parent objects back out, injecting children as it goes
            JSONParser    parentsParser  = JSON.createParser(JSON.serialize(parents));
            JSONParser    childrenParser = JSON.createParser(JSON.serialize(children));
            JSONGenerator combinedOutput = JSON.createGenerator(false);

            streamTokens(parentsParser, combinedOutput, new InjectChildrenEventHandler(childrenParser, relationshipName, children));

            // Derserialise back into SObject list complete with children
            return(JSON.deserialize(combinedOutput.getAsString(), parentsType));
        }