mattaddy / SObjectFabricator

An SObject fabrication API to reduce database interactions and dependencies on triggers in Apex unit tests.
Other
89 stars 14 forks source link

Master Detail relationship between two sObjects #11

Closed alquesadilla closed 4 years ago

alquesadilla commented 4 years ago

I have two Sobjects Routing_Rulec and Routing_Rule_Parameterc that have a parent-child/master-detail relationship. That is there is a Routing_Rulec field on the Routing_Rule_Parameterc object that goes back to the Routing_Rule__c object.

Following the documentation I have the following:

//Parent Object
 sfab_FabricatedSObject fabricatedrr1 = new sfab_FabricatedSObject(Routing_Rule__c.class);
    fabricatedrr1.setField(Routing_Rule__c.Id, getFakeId(Schema.Routing_Rule__c.SObjectType));

//Child Object
    sfab_FabricatedSObject fabricatedrrp1 = new sfab_FabricatedSObject(Routing_Rule_Parameter__c.class);  
    fabricatedrrp1.setField(Routing_Rule_Parameter__c.Id, getFakeId(Schema.Routing_Rule_Parameter__c.SObjectType));

//I set the name to 'Routing_Rule_Parameters' because that is what it says on Child Relationship Name
    fabricatedrr1.setChildren('Routing_Rule_Parameters', new List<sfab_FabricatedSObject> {fabricatedrrp1});

    //======================problematic line======================
    Routing_Rule__c rr1 = (Routing_Rule__c)fabricatedrr1.toSObject();

I am getting: System.JSONException: No field name specified on column for sobject of type Routing_Rule__c

The stack trace:

Class.System.JSON.deserialize: line 15, column 1
Class.sfab_FabricatedSObject.toSObject: line 39, column 1

Looking further the code on Class.sfab_FabricatedSObject.toSObject is :

public SObject toSObject() {
        system.debug('serialize() is (later to be deserialized): '+serialize());
        return (SObject)JSON.deserialize(JSON.serialize(serialize()), sType);
    }

What am I doing wrong? I have been looking at this for a couple of hours now. Any suggestions?