简体   繁体   中英

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

我发现使用OData服务是在客户端javascript中返回所需信息的最佳方法: CRM 2011,OData入门

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM