1

As shown in the image below, I can provide my little method with a lot of stuff. The thing is that I want to send in e.g. first name of the contact I'm currently on. The field doesn't seem to be able to read the contents of the boxes on the same form that the calling control is on.

The approach now is to execute the method without parameters and let it fetch it's own data as it pleases. However, I'd like it better if I could provide it with some junk. How can I send in dynamical parameters into it?

Should I go for dependencies? I've always thought of them as "stuff the method will see if you query for them" and not "parameters that will be sent in"...

enter image description here

4
  • What exactly does this mean: The field doesn't seem to be able to read the contents of the boxes on the same form that the calling control is on.? Why not just call it in form Javascript with with the parameters you want to pass executeSigning(param1, param2, ...)? Javascript supports optional parameters so have executeSigning accept every prefetched value it might use, but only pass in parameters the form "knows" about. Commented Nov 16, 2014 at 14:11
  • @JasonFaulkner Perhaps it's a matter of calling things different names but I didn't get the part "call it in form JavaScript". Which script are you referring to? I've added a web resource that contains the script to be executed when e.g. the contents of a box change (the onChange event). But I have no control over the actual call being made - we can only point to a web resource's method and say that this one is to be called when that one changes its value. It's not like I can determine how the call is being performed by implementing an onChange method... Commented Nov 16, 2014 at 19:38
  • 1
    Might be personal preference, but what I typically do for each form that runs JS is create a JS file for each one (e.g. account.js, contact.js, etc.) and each of those files would define all the handlers for the respective form. So what I was saying is in your "form Javascript" (i.e. account.js) define a method for field_onChange which you would map the respective handler to. Then in your field_onChange method make a call to executeSigning. I prefer this way because it makes it easier to view/debug JS as everything is in one place and you don't have to view each handler in CRM. Commented Nov 16, 2014 at 20:00
  • Hey there, mate - that was one cool approach. Sadly, most of my customers turn inside out as I start implying the "black magic" of programming. But for a more advanced and profession approach, that's actually quite a neat idea, architecture-wise. Please post a reply summarizing what you suggested. The correct answer is still that by @MarioZG and it stays accepted but I'll be glad to do +1 on you since the suggestion makes a lot of sense. Commented Nov 16, 2014 at 20:12

3 Answers 3

2

You can pass there any valid javascript statement - call to function etc.. For example Xrm.Page.getAttribute('name').getValue() will pass current record name value as argument.

CRM takes whatever you type in the parameters field and puts as parameter to function call.

Sign up to request clarification or add additional context in comments.

4 Comments

Oh, you don't say... I've always thought it'd sent in the string or fixed value or something. So if I sent in a parameter like "2 + 3", it's actually going to send in "5"?! I've tried that once, years ago when I was new to CRM. It failed then and I've never tried again, believing that it's just not a correct approach. But it might have been due to my ignorance at that time! Please confirm or deny the verifying follow-up question stated above and I'll accept the reply as the right answer.
If you send it as 2+3 (without quotes) it will work. When you pass with quotes, it assumes it's just text that says "2+3"...
Sorry, my bad. I meant, of course - to send 2+3 and not "2+3". If it actually works the way you're telling me, that's awesome...
I confirm that passing a function call works like a charm. Example : "Param1",Xrm.Page.getAttribute("telephone1").getValue()
1

(From my comment above) As an alternate/architectural suggestion, maybe consider creating a Javascript resource for each form (e.g. account.js, contact.js, etc.) and each of those files would define all the handlers for the respective form.

For example, your account.js file might look like this:

function form_onLoad() {
    // Do stuff on load.
}

function form_onSave() {
    // Do stuff on save.
}

function field_onChange() {
    executeSigning(param1, param2);
}

Now in the CRM handlers, you bind each event to its respective method. I have found this works well because it makes it easier to view/debug Javascript as everything is in one place and you don't have to view each handler in CRM.

Comments

0

You can send objects to JavaScript functions the same way as constants. To reference objects that are not in a form, you can give them and ID.

Element:

<div id="myelement">Hello</div>

Code:

// function
function jsfunc(variabelname)
{
    // dosomething with variabelname
}

// call function with reference to object
jsfunc( document.getElementById("myelement") );

You can also have a function without predefined parameters and access whatever is sent to the function via arguments.

Example:

function jsfunc()
{
    var output = "";
    var args = arguments;

    for(var i = 0, len = args.length; i < len; i++) // loop through all arguments
    {
        var arg = args[i]; // reference the argument directly
        output += i + ": "; // add index number
        if (arg == null)
        {
            output += "null"; // arg is null
        } else {
            output += typeof(arg) + ", "; // adds type of argument
            if (typeof(arg) == "string" || typeof(arg) == "number")
            {
                // argument is a string og number, which can be easily output
                output += arg;
            } else if (typeof(arg) == "object") {
                // it is an object
                if (arg.tagName)
                {
                    // the object has a tagname which means it's an HTML element
                    output += arg.tagName;
                } else {
                    // object but not HTML element, our array
                    output += "array, " + arg.firstname;
                }
            }
        }
        output += "\n"; // add new line
    }
    alert(output);
}

jsfunc(
    "a" /* type: string */
    , 5 /* type: number */
    , document.getElementById("txtFirstName") /* type object, has tagName INPUT */
    , document.getElementById("ElementThatDoesNotExists") /* type NULL */
    , { "firstname" : "john", "lastname" : "smith" } /* type object, no tagname */
);

Output:

0: string, a
1: number, 5
2: object, INPUT
3: null
4: object, array, john

3 Comments

It's a very interesting example, even if it's unsupported. Just to be clear, though - how does the arguments get into the method (you're getting them from a variable called arguments, which seems to be globally scoped)? The method signature has only empty brackets, no input parameters as far as I understand. Perhaps I'm missing something?
The arguments object is a local variable available within all functions. It contains an array of all the arguments sent to the function even if the functions isn't defined with a fixed/predefined set of arguments.
Awesome. Didn't know of such magic in JS. This will come in handy. +1 for that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.