CompanyDroneFromSector7G
CompanyDroneFromSector7G

Reputation: 4527

Dynamics CRM 2011 retrieve all subjects

I'm currently learning CRM 2011 while working with it, and I thought this could be a helpful example. In a javascript function I need to retrieve all Subject entity records and cache them on the client. I also need them to be cached within their heirarchy. I would think the best way to do this is to return each subject record with its id and parent id, so I can build a client-side structure in javascript.

Does anyone with CRM experience have any suggestions as to how this query should be coded? I'm ok with processing the data, just no idea how to return the results I need!

Thanks

Upvotes: 0

Views: 1819

Answers (2)

CompanyDroneFromSector7G
CompanyDroneFromSector7G

Reputation: 4527

Your answer probably works, but tbh there were too many clicks for me.

This is how I did it in the end. cacheSubjects is the main function:

var sgc_subjectCache = [];
var sgc_subjectCacheCount = 0;

function cacheSubjectsCallback(data) {
    // update subjects
    // loop through retrieved subjects and add to cache
    for( i=0; i < data.length; i++ )
    {
        var subject = data[i];
        subject.Root = subject.Title;
        // var subjectid = subject.SubjectId;
        sgc_subjectCache.push( subject );
        sgc_subjectCacheCount += 1;
    }
}    

function cacheSubjectsComplete() {
    // now update title with ancestors
    var done = false;
    while(done==false)
    {
        done = true;
        // outer loop
        var len = sgc_subjectCache.length;
        for( var i=0; i < len-1; i++ )
        {
            subject = sgc_subjectCache[ i ];
            // inner loop
            for( var j=0; j < len-1; j++ )
            {
                subject2 = sgc_subjectCache[ j ];
                if( subject.ParentSubject.Id === subject2.SubjectId )
                {
                    // found the parent
                    var newTitle = subject2.Title + ' : ' + subject.Title;
                    sgc_subjectCache[ i ].Title = newTitle;
                    sgc_subjectCache[ i ].Root = subject2.Root;
                    sgc_subjectCache[ i ].ParentSubject.Id = subject2.ParentSubject.Id;
                    done = false; // more to do
                }
            }
        }
    }

}

function cacheSubjects() {
    sgc_subjectCache = [];
    var options = "$select=Title,SubjectId,ParentSubject";
    SDK.REST.retrieveMultipleRecords("Subject", options, cacheSubjectsCallback, function(error) {
        alert( error );
    }, cacheSubjectsComplete);
}

Upvotes: 1

Daryl
Daryl

Reputation: 18895

I've found using the OData service is the best way to return information you need in the client side javascript: CRM 2011, Getting started with OData.

Upvotes: 2

Related Questions