Skip to main content
April 29, 2010
Question

Simply not smart enough to write this

  • April 29, 2010
  • 2 replies
  • 1459 views

Hey All,

You know when you want to do something, and you realize you simply do not have the brain power to pull it off? I am there, but I think maybe a little bit of help might get me over the really hard part.

GOAL: Pull scheme data from our Salesforce.com instance (a massive CRM platform, basically just a fancy relational database system). Find all related objects, parse the data, and feed it into the JIT javascript visulization framework (http://thejit.org/). I want to use CF to pull the data, parse it, and feed it to JIT, however the format JIT requires is hard for me to write programatically.

Here is an example of the what the final result will look like, instead of numbers each node would be an object name.
http://thejit.org/Jit/Examples/Spacetree/example2.html

Here is the data that powers that thing.

http://pastebin.com/n2Sv64M6

It's just JSON, but the nesting get's really brutal.

So far I have the code in place to get the data I want, and break it into chunks, but I can't think of an eloquent way to create the required JSON. Here is what I have so far.

http://portal.fpitesters.com/sfmap.cfm

Here is the code for that tool.

<cfsetting requesttimeout="5000">

<cfif isdefined("form.Flush")>
     <cfcache action="flush" >
</cfif>

<form name="FlushForm" method="post">
     <input name="Flush" value="Recalculate Relationships (Will Take a Long Time)" type="submit" />
</form>

<cfcache action="optimal">

<cfoutput>
     These results where cached at #TimeFormat(Now())#.
     <cfset GlobalObject = server.OSF.describeGlobal()>
     <cfloop from="1" to="#arraylen(GlobalObject.Results)#" index="ObjectIndex">
          <cfset Object = server.OSF.describeObject(GlobalObject.Results[ObjectIndex])>
          <cfset ObjectInfo = Object.RawSoap>
          <cfset returnStruct = structnew()>
          
          <cfset RelationShips[GlobalObject.Results[ObjectIndex]] = arraynew(1)>
          <cfset numRefFields = 0>
          
          <cfloop
               From="1"
               To="#arraylen(ObjectInfo.Envelope.Body.describeSobjectResponse.result.fields)#"
               Index="i">
               
               <cfset ObjectFieldReference = ObjectInfo.Envelope.Body.describeSobjectResponse.result.fields>
               
               <cfif ObjectFieldReference.type.xmltext eq "reference">
                    <cfset numRefFields = numRefFields + 1>
                    <cfset RelationShips[GlobalObject.Results[ObjectIndex]][numRefFields] = structnew()>
                    <cfset RelationShips[GlobalObject.Results[ObjectIndex]][numRefFields].name=ObjectFieldReference.name.xmltext>
                    <cfset RelationShips[GlobalObject.Results[ObjectIndex]][numRefFields].label=ObjectFieldReference.label.xmltext>
                    <cfset RelationShips[GlobalObject.Results[ObjectIndex]][numRefFields].refto=ObjectFieldReference.referenceto.xmltext>
               </cfif>
               
          </cfloop>
          <a name="#GlobalObject.Results[ObjectIndex]#">RelationsShips in #GlobalObject.Results[ObjectIndex]#</a>
          <ol>
               <cfloop from="1" to="#arraylen(RelationShips[GlobalObject.Results[ObjectIndex]])#" index="relationshipFieldIndex">
                    <li>  #RelationShips[GlobalObject.Results[ObjectIndex]][relationshipFieldIndex].name# to <a href="###RelationShips[GlobalObject.Results[ObjectIndex]][relationshipFieldIndex].refto#">#RelationShips[GlobalObject.Results[ObjectIndex]][relationshipFieldIndex].refto#</a></li>
               </cfloop>
          </ol>
     </cfloop>
</cfoutput>

So instead of creating that simple dumb list, I ned to create the JSON, but how? I just can't wrap my brain around it for some reason. Any help, sample code, anything would be of use. Thank you.

This topic has been closed for replies.

2 replies

April 30, 2010

Well I am very close, my demo loads, but I'd just displays the same children over and over again. I'm sure its a structural problem in my JSON, but I can't exactly figure out. It seems I need to nest the children further, but I'm not sure why or how much further until it becomes infinitly recursive like I want.

I know this isn't really a CF problem anymore, but the support group for this framework blows, and you guys are the smartest coders I know

Any help is appreciated, thanks.

Here is a link to the demo

http://portal.fpitesters.com/sfmap/example2.html

Here is the JSON that mostly works.

{
    id: "account",
    data: {
       
    },
    children: [
        {
            id: "account",
            data: {
               
            },
            children: [
               
            ],
            name: "account"
        },
        {
            id: "recordtype",
            data: {
               
            },
            children: [
               
            ],
            name: "recordtype"
        },
        {
            id: "account",
            data: {
               
            },
            children: [
               
            ],
            name: "account"
        },
        {
            id: "user",
            data: {
               
            },
            children: [
               
            ],
            name: "user"
        },
        {
            id: "user",
            data: {
               
            },
            children: [
               
            ],
            name: "user"
        },
        {
            id: "user",
            data: {
               
            },
            children: [
               
            ],
            name: "user"
        },
        {
            id: "contact",
            data: {
               
            },
            children: [
               
            ],
            name: "contact"
        },
        {
            id: "contact",
            data: {
               
            },
            children: [
               
            ],
            name: "contact"
        },
        {
            id: "contact",
            data: {
               
            },
            children: [
               
            ],
            name: "contact"
        }
    ],
    name: "account"
},
{
    id: "accountcontactrole",
    data: {
       
    },
    children: [
        {
            id: "account",
            data: {
               
            },
            children: [
               
            ],
            name: "account"
        },
        {
            id: "contact",
            data: {
               
            },
            children: [
               
            ],
            name: "contact"
        },
        {
            id: "user",
            data: {
               
            },
            children: [
               
            ],
            name: "user"
        },
        {
            id: "user",
            data: {
               
            },
            children: [
               
            ],
            name: "user"
        }
    ],
    name: "accountcontactrole"
},
{
    id: "accounthistory",
    data: {
       
    },
    children: [
        {
            id: "account",
            data: {
               
            },
            children: [
               
            ],
            name: "account"
        },
        {
            id: "selfserviceuser",
            data: {
               
            },
            children: [
               
            ],
            name: "selfserviceuser"
        }
    ],
    name: "accounthistory"
},
{
    id: "accountpartner",
    data: {
       
    },
    children: [
        {
            id: "account",
            data: {
               
            },
            children: [
               
            ],
            name: "account"
        },
        {
            id: "account",
            data: {
               
            },
            children: [
               
            ],
            name: "account"
        },
        {
            id: "opportunity",
            data: {
               
            },
            children: [
               
            ],
            name: "opportunity"
        },
        {
            id: "user",
            data: {
               
            },
            children: [
               
            ],
            name: "user"
        },
        {
            id: "user",
            data: {
               
            },
            children: [
               
            ],
            name: "user"
        }
    ],
    name: "accountpartner"
},
{
    id: "accountshare",
    data: {
       
    },
    children: [
        {
            id: "account",
            data: {
               
            },
            children: [
               
            ],
            name: "account"
        },
        {
            id: "group",
            data: {
               
            },
            children: [
               
            ],
            name: "group"
        },
        {
            id: "user",
            data: {
               
            },
            children: [
               
            ],
            name: "user"
        }
    ],
    name: "accountshare"
},
{
    id: "activityhistory",
    data: {
       
    },
    children: [
        {
            id: "account",
            data: {
               
            },
            children: [
               
            ],
            name: "account"
        },
        {
            id: "contact",
            data: {
               
            },
            children: [
               
            ],
            name: "contact"
        },
        {
            id: "admin_projects__c",
            data: {
               
            },
            children: [
               
            ],
            name: "admin_projects__c"
        },
        {
            id: "user",
            data: {
               
            },
            children: [
               
            ],
            name: "user"
        },
        {
            id: "user",
            data: {
               
            },
            children: [
               
            ],
            name: "user"
        },
        {
            id: "user",
            data: {
               
            },
            children: [
               
            ],
            name: "user"
        }
    ],
    name: "activityhistory"
},
{
    id: "additionalnumber",
    data: {
       
    },
    children: [
        {
            id: "callcenter",
            data: {
               
            },
            children: [
               
            ],
            name: "callcenter"
        },
        {
            id: "user",
            data: {
               
            },
            children: [
               
            ],
            name: "user"
        },
        {
            id: "user",
            data: {
               
            },
            children: [
               
            ],
            name: "user"
        }
    ],
    name: "additionalnumber"
},
{
    id: "admin_projects__c",
    data: {
       
    },
    children: [
        {
            id: "group",
            data: {
               
            },
            children: [
               
            ],
            name: "group"
        },
        {
            id: "user",
            data: {
               
            },
            children: [
               
            ],
            name: "user"
        },
        {
            id: "user",
            data: {
               
            },
            children: [
               
            ],
            name: "user"
        },
        {
            id: "user",
            data: {
               
            },
            children: [
               
            ],
            name: "user"
        }
    ],
    name: "admin_projects__c"
},
{
    id: "apexclass",
    data: {
       
    },
    children: [
        {
            id: "user",
            data: {
               
            },
            children: [
               
            ],
            name: "user"
        },
        {
            id: "user",
            data: {
               
            },
            children: [
               
            ],
            name: "user"
        }
    ],
    name: "apexclass"
},
{
    id: "apextrigger",
    data: {
       
    },
    children: [
        {
            id: "user",
            data: {
               
            },
            children: [
               
            ],
            name: "user"
        },
        {
            id: "user",
            data: {
               
            },
            children: [
               
            ],
            name: "user"
        }
    ],
    name: "apextrigger"
},
{
    id: "approval",
    data: {
       
    },
    children: [
        {
            id: "contract",
            data: {
               
            },
            children: [
               
            ],
            name: "contract"
        },
        {
            id: "user",
            data: {
               
            },
            children: [
               
            ],
            name: "user"
        },
        {
            id: "user",
            data: {
               
            },
            children: [
               
            ],
            name: "user"
        },
        {
            id: "user",
            data: {
               
            },
            children: [
               
            ],
            name: "user"
        }
    ],
    name: "approval"
},
{
    id: "asset",
    data: {
       
    },
    children: [
        {
            id: "contact",
            data: {
               
            },
            children: [
               
            ],
            name: "contact"
        },
        {
            id: "account",
            data: {
               
            },
            children: [
               
            ],
            name: "account"
        },
        {
            id: "product2",
            data: {
               
            },
            children: [
               
            ],
            name: "product2"
        },
        {
            id: "user",
            data: {
               
            },
            children: [
               
            ],
            name: "user"
        },
        {
            id: "user",
            data: {
               
            },
            children: [
               
            ],
            name: "user"
        }
    ],
    name: "asset"
},
{
    id: "assignmentrule",
    data: {
       
    },
    children: [
        {
            id: "user",
            data: {
               
            },
            children: [
               
            ],
            name: "user"
        },
        {
            id: "user",
            data: {
               
            },
            children: [
               
            ],
            name: "user"
        }
    ],
    name: "assignmentrule"
},
{
    id: "attachment",
    data: {
       
    },
    children: [
        {
            id: "account",
            data: {
               
            },
            children: [
               
            ],
            name: "account"
        },
        {
            id: "user",
            data: {
               
            },
            children: [
               
            ],
            name: "user"
        },
        {
            id: "selfserviceuser",
            data: {
               
            },
            children: [
               
            ],
            name: "selfserviceuser"
        },
        {
            id: "selfserviceuser",
            data: {
               
            },
            children: [
               
            ],
            name: "selfserviceuser"
        }
    ],
    name: "attachment"
}

Inspiring
May 6, 2010

Right.  Busy w/end, sorry.

The problem is that the top level of the data structure they want is a struct, and you're creating an array.  In JSON notation {} denotes a struct, [] denotes an array.

Also their structure is hierarchical, however I see nothing in your code (like a recursive call) which would effect a hierarchy.  You're just creating a flat array.

--

Adam

May 6, 2010

Hey Adam,

Thanks for the help. Looking at this framework closer, I don't think it's going to do what I want it to to. I noticed when it hits the end of the heirachy, it just resets to the same root node that you started from. I needed it to load the root node that has the same name/id as the node you just clicked on, hence creating a recursive type hierarchy. I got the JSON pretty much figured out, but even then it doesn't perform as desired. I think my next best bet is to create some kind of simple AJAX based crawler. The visualizations in this are nice, but I don't think it works like I want it to. If you think I'm wrong, certainly let me know, but otherwise I'm going to start working on rolling my own basic visualizer system.

Inspiring
April 29, 2010

You know you can convert CF data structures to JSON with a single function call, right?

http://help.adobe.com/en_US/ColdFusion/9.0/CFMLRef/WSc3ff6d0ea77859461172e0811cbec22c24-79fa.html

--

Adam

April 29, 2010

I am familiar with that function. Maybe I'm overcomplicating things, but I don't think thats going to give the data in quite the structure I need. Worth a shot though. I'll try it out.

Inspiring
April 29, 2010

JSON is JSON: it's a notation used to represent the structure of data, not a data structure unto itself.  It's up to you to structure the source data appropriately.

You seem to have a lot of code already for something that's not working, and trying to get all that working will probably do your head in (a case in point: I'm not even prepared to look at that much code when someone is asking for help on these forums).  Can I recommend you ignore you actual data whilst you get this working, and just do a proof of concept work: use minimalist mock data to come up with a small working sample.  Once you get that formatted correctly, start thinking about expanding it to meet your needs; making sure the end result stays valid at intermediary stages.  Don't try to solve the whole thing at once.

--

Adam