locked
how to access a jquery script file on save in mscrm? RRS feed

  • Question

  • I have a jquery script file as below...I need to call the "SavePolicyDetails" function on save of form in mscrm. I tried doing it but the function name is undefined.  (Remember this is a $ jquery function)

     

    layoutVMList={};
    sessionStorage.isEdit = true;
    var PolicyInfoViewModel;
    $(function () {


       // makeActive_tabnavigation('PolicyInfo');
        function onload() {
      var offerid = window.parent.Xrm.Page.getAttribute("biz_offerid").getValue();
    //   var offerid = 1113;
    if(offerid !=null)
    {
    PolicyInfoDelegate.GetPolicyFieldsCompleted = GetPolicyFieldsCompleted;
      PolicyInfoDelegate.GetPolicyFields(offerid);
    }
            
        }

        if (layoutVMList['PolicyInfoViewModel'] == undefined) {

             PolicyInfoViewModel = kendo.observable({
                policyInfoValidations: {},
                policyInfoFields: {},
                issaveforlater: false,
                errorMessage: '',
                isErrorVisible: false,
                policyInfo: new PolicyInfo(),            
                ddlOnChanged: function (e) {
                    if (e.sender.element.attr('data-rulecheck') == "true") {
                        if (e.sender._selectedValue == 'Endorsement') {
                            for (var i = 0; i < PolicyInfoViewModel.policyInfoFields.sections[0].fields.length; i++) {
                                if (PolicyInfoViewModel.policyInfoFields.sections[0].fields[i].name === 'EndorsementEffectiveDate') {
                                    PolicyInfoViewModel.policyInfoFields.sections[0].fields[i].set('isVisible', true);
                                }
                            }
                        }
                        else {
                            for (var i = 0; i < PolicyInfoViewModel.policyInfoFields.sections[0].fields.length; i++) {
                                if (PolicyInfoViewModel.policyInfoFields.sections[0].fields[i].name === 'EndorsementEffectiveDate') {
                                    PolicyInfoViewModel.policyInfoFields.sections[0].fields[i].set('isVisible', false);
                                }
                            }
                        }
                    }
                    kendo.bind($("#policyInfoContent"), layoutVMList["PolicyInfoViewModel"]);
                },
                setExpiryDate: function (e) {
                    if (e.sender.element.attr('name') == "Effective Date") {
                        setExpiryDate();
                    }
                },
                SavePolicyDetails: function () {
    alert("save");


    var type = window.parent.Xrm.Page.ui.getFormType();
    if(type ==2 )
    {
                    if (layoutVMList["PolicyInfoViewModel"].policyInfoValidations.validate()) {
                        layoutVMList["PolicyInfoViewModel"].set('issaveforlater', true);
                        PolicyInfoDelegate.ManagePolicyInfoCompleted = ManagePolicyInfoCompleted;
                        PolicyInfoDelegate.ManagePolicyInfo(PolicyInfoViewModel.policyInfoFields.sections);

                    }
    }
                },
                Next: function () {

                    if (layoutVMList["PolicyInfoViewModel"].policyInfoValidations.validate()) {
                        layoutVMList["PolicyInfoViewModel"].set('issaveforlater', false);
                        PolicyInfoDelegate.ManagePolicyInfoCompleted = ManagePolicyInfoCompleted;
                        PolicyInfoDelegate.ManagePolicyInfo(PolicyInfoViewModel.policyInfoFields.sections);

                    }
                },
                onChanged: function () {

                }
            });
            PolicyInfoViewModel.Commands = [
             {
                 text: 'Save for Later',
                 cssClass: 'btn btn-success',
                 IsEnable: function () {
                     if (sessionStorage.isEdit === 'true')
                         return true;
                     else
                         return false;
                 },
                 IsVisible: true,
                 onClick: PolicyInfoViewModel.Save
             },
             {
                 text: 'Next',
                 cssClass: 'btn btn-info',
                 IsEnable: function () {
                     if (sessionStorage.isEdit === 'true')
                         return true;
                     else
                         return false;
                 },
                 IsVisible: true,
                 onClick: PolicyInfoViewModel.Next
             }
            ];
            layoutVMList['PolicyInfoViewModel'] = PolicyInfoViewModel;
            onload();
        }
        //layoutVMList['RightNavigationVM'].previouscommand.set('IsEnable', sessionStorage.isEdit);
      //  layoutVMList['RightNavigationVM'].set('pagecommands', layoutVMList['PolicyInfoViewModel'].Commands);
        PolicyInfoBinder.bind();


        //service response
        //gets the policy info fields from xml
        function GetPolicyFieldsCompleted(l_policyFieldData) {
            debugger;
            layoutVMList['PolicyInfoViewModel'].set('policyInfoFields', l_policyFieldData);
            var l_transactionType = $.grep(layoutVMList['PolicyInfoViewModel'].policyInfoFields.sections[0].fields, function (e) { return e.name === 'TransactionType'; });
            if (l_transactionType != null && l_transactionType.length > 0 && l_transactionType[0].defaultvalue === 'Endorsement') {
                layoutVMList['PolicyInfoViewModel'].policyInfoFields.sections[0].fields[8].set('isVisible', true);
            }
            else
                layoutVMList['PolicyInfoViewModel'].policyInfoFields.sections[0].fields[8].set('isVisible', false);
            var l_EffDate = $.grep(layoutVMList['PolicyInfoViewModel'].policyInfoFields.sections[0].fields, function (e) { return e.name === 'EffDate'; });
            if (l_EffDate != null && l_EffDate.defaultvalue === '') {
                l_EffDate.set('defaultvalue', "12/12/2014");
            }
            for (var i = 0; i < layoutVMList['PolicyInfoViewModel'].policyInfoFields.sections[0].fields.length; i++) {
                if (layoutVMList['PolicyInfoViewModel'].policyInfoFields.sections[0].fields[i].name === 'EffDate') {
                    if (layoutVMList['PolicyInfoViewModel'].policyInfoFields.sections[0].fields[i].defaultvalue === '')
                        layoutVMList['PolicyInfoViewModel'].policyInfoFields.sections[0].fields[i].set('defaultvalue', kendo.toString(kendo.parseDate(new Date()), 'MM/dd/yyyy'));
                }
                if (layoutVMList['PolicyInfoViewModel'].policyInfoFields.sections[0].fields[i].name === 'ExpDate') {
                    if (layoutVMList['PolicyInfoViewModel'].policyInfoFields.sections[0].fields[i].defaultvalue === '' && l_EffDate.defaultvalue !== '') {
                        debugger;
                        var d = new Date();
                        var expdate = new Date();
                        expdate.setFullYear(d.getFullYear() + 1, d.getMonth(), d.getDate());
                        layoutVMList['PolicyInfoViewModel'].policyInfoFields.sections[0].fields[i].set('defaultvalue', kendo.toString(kendo.parseDate(expdate), 'MM/dd/yyyy'));
                    }

                }
            }
        }

        function ManagePolicyInfoCompleted(l_response) {
            if (Offer.status < 2 || Offer.status == 3) {
                Offer.status = 2;
            }
            if (layoutVMList['PolicyInfoViewModel'].issaveforlater) {
                window.location = "home.html";
            }
            else
                router.navigate('/LocationInfo');
        }

        function setExpiryDate() {
            var effDate = layoutVMList['PolicyInfoViewModel'].policyInfoFields.sections[0].fields.filter(function (obj) {
                return obj.name === "EffDate";
            });
            if (effDate.length > 0 && effDate[0].defaultvalue != '') {
                var _effDate = new Date(effDate[0].defaultvalue);
                var expDate = new Date();
                expDate.setFullYear(_effDate.getFullYear() + 1, _effDate.getMonth(), _effDate.getDate());
                layoutVMList['PolicyInfoViewModel'].policyInfoFields.sections[0].fields.filter(function (obj) {
                    return obj.name === "ExpDate";
                })[0].set('defaultvalue', kendo.toString(kendo.parseDate(expDate), 'MM/dd/yyyy'));
            }
        }

    })

    Thursday, November 13, 2014 6:50 AM

All replies

  • Hi,

    Clear the cache and try once. If you are still not able to call the required method, try the same thing on different machine.

    Thursday, November 13, 2014 7:13 AM
  • Have you load the jquery in the page? If no, please include it in page.

    If yes, it could be caused by async load by the page. Your onload function is triggered before the jquery js been loaded.

    Some sample code for you to reference to fix the async load issue:

    function loadScript(url)  

    {  

        var x ;  

     if ( window.XMLHttpRequest ) // Gecko

            x=new XMLHttpRequest() ;

        else if ( window.ActiveXObject ) // IE

            x=new ActiveXObject("MsXml2.XmlHttp") ;

        x.open('GET', url, false); 

        x.send(null);  

        eval(x.responseText);  

        var s = x.responseText.split(/\n/);  

        var r = /^(?:function|var)\s*([a-zA-Z_0-9]+)/i; 

        for (var i = 0; i < s.length; i++)  

        {  

            var m = r.exec(s[i]);  

            if (m != null)  

                window[m[1]] = eval(m[1]);  

        }  

    } 

    function onload()

    {

    loadScript("/WebResources/new_jquery");

    loadScript("/WebResources/new_jquery_mask");

      Mask("mobilephone", "9999 9999");

    }

    function Mask(field, format)

    {

      $("#"+field).mask(format);

    Thursday, November 13, 2014 8:15 AM