Wednesday, 7 March 2012

Crm2011 using rest end point in javascript

I have an entity Subject, and related entity Result. when marks are set in subject. Result entity is reterived and all subjects of Result entities are reterived where subject is not droped. and finally Result entity overall marks are updated.

Add follwoing javascript files to entity on load event.
jquery1.4.1vsdoc, json2, JQueryRESTDataOperationFunctions and finally here is javascript to update related entity value depending on current values.

statuscode on Subject is either selected or droped. On form load call Loaded function and on form save call UpdateResultDetails.
As ajax call are made in order to complete given task select  "pass execution context as first parameter "in order to manually control saving and closing of form.

/// <reference path="jquery1.4.1vsdoc.js" />
/// <reference path="JQueryRESTDataOperationFunctions.js" />

var initialSubjectStatus;
var saveMode;
//used to stop recursive saving
var saveFlag = false; 

//initialises application status
function Loaded() {
    initialSubjectStatus = Xrm.Page.getControl("statuscode").getAttribute().getValue();
}

function UpdateResultDetails(ExecutionObj) {    
    if (Xrm.Page.data.entity.getIsDirty() && !saveFlag) {

        var applicationstaus = Xrm.Page.getControl("statuscode").getAttribute().getValue();

        //checks whether Subject status initially was selected or is currently is selected
        if (initialApplicationStatus == 161820001 || applicationstaus == 161820001) {
            if (ExecutionObj) {
                ExecutionObj.getEventArgs().preventDefault();

                //Save = 1, Save and Close = 2 , Save and New = 59 ...
                saveMode = ExecutionObj.getEventArgs().getSaveMode();
            }
            retrieveResult(Xrm.Page.getControl("publisher_resultid").getAttribute().getValue());
        }
    } else {
        saveFlag = false;
    }
}

function retrieveResult(idObj) {
    if (!idObj) {
        return;
    }

    retrieveRecord(idObj[0].id, "publisher_resultSet", retrieveResultCompleted, null);
}

//reterive subjects for specified result id
function retrieveSubjects(resultId) {

    if (Xrm.Page.data.entity.getId() == null) {
        //subject is selected
        retrieveMultiple("publisher_subjectSet", "?$filter=publisher_resultId/Id eq (guid'" + resultId + "')  and (statuscode/Value eq 161820001)", retrieveSubjectsCompleted, null);
     } else {
        //subject status is selected
        retrieveMultiple("cnova_subjectSet", "?$filter=publisher_resultId/Id eq (guid'" + resultId + "')  and (statuscode/Value eq 161820001) and not (publisher_subjectId eq (guid'" + Xrm.Page.data.entity.getId() + "'))", retrieveSubjectsCompleted, null);
    }
}

var vacancy;

function updateResult(numberOfSubjectsSelected) {
    if (!subject.publisher_TotalNumberOfSelectedSubjects) {
        return;
    }
    var remainingsubjects = subject.publisher_TotalNumberOfSelectedSubjects - numberOfSelectedSubjects;

    var applicationstaus = Xrm.Page.getControl("statuscode").getAttribute().getValue();
    if (applicationstaus == 161820001) {
        remainingsubjects--;
    }

    if (remainingsubjects < 0) {
        remainingsubjects = 0;
    }

    var changes = new Object();
    changes.publisher_RemainingNumberOfSubjects = remainingpositions;

    if (remainingsubjects > subject.publisher_TotalNumberOfSelectedSubjects) {
     subject.publisher_TotalNumberOfSelectedSubjects = remainingpositions;
    }

    updateRecord(result.publisher_resultId, changes, "cnova_resultSet", ResultUpdated, null);
}

function retrieveSubjectsCompleted(data, textStatus, XmlHttpRequest) {
    updateResult(data.length);
}

function retrieveResultCompleted(data, textStatus, XmlHttpRequest) {
    result = data;
    retrieveSubjects(result.publisher_resultId);
}

function ResultUpdated() {
    saveFlag = true;

    if (saveMode == 1) {
        Xrm.Page.data.entity.save(null);
    }
    else if (saveMode == 2) {
        Xrm.Page.data.entity.save("saveandclose");
    }
    else if (saveMode == 59) {
        Xrm.Page.data.entity.save("saveandnew");
    }
}


No comments:

Post a Comment