Kanini
Kanini

Reputation: 1993

Javascript in CRM 2011

Statutory Disclaimer: New to JavaScript

Form: MyForm

Fields:

ntt_name
ntt_lookup1
ntt_lookup2

Requirement:

Read the value of ntt_lookup1 and ntt_lookup2 and get the name (not the id) and concatenate them and populate it on the name field (Primary Attribute).

I upload this as a Web Resource, associate it with the Form MyForm and add it to the onsave event.

I get the following error.

enter image description here

Question: What am I doing wrong?

What I have done so far:

  1. Did a google search for the error and found this SO question. So, double checked for any missing brackets using both Notepad++ and by manually tracing it with hand.

Code:

if (typeof(CRMDynamics2011) == "undefined")
{ CRMDynamics2011 = {}; }

CRMDynamics2011.MyForm = {
    FormSave: function() {
        alert("Within Function FormSave");
        var lookup1 = Xrm.Page.getAttribute("ntt_lookup1").getValue();
        var lookup2 = Xrm.Page.getAttribute("ntt_lookup2").getValue();
        if (lookup1 != null && lookup1[0] && lookup1[0].name != null) {
            alert("lookup1 is NOT NULL");
            var value1 = lookup1[0].name;
        }
        if (lookup2 != null && lookup2[0] && lookup2[0].name != null) {
            alert("lookup2 is NOT NULL");
            var value2 = lookup2[0].name;
        }
        var fieldName = Xrm.Page.getAttribute("ntt_name");
        fieldName.setValue(value1 + ' - ' + value2);

    }
};

Upvotes: 4

Views: 718

Answers (1)

glosrob
glosrob

Reputation: 6715

As you are using namespacing, you must fully qualify the method you want to call.

So in the form properties for CRM, for the OnSave event use

CRMDynamics2011.MyForm.FormSave();

CRM should then call your method when you click save.

Upvotes: 3

Related Questions