Pass Parameters To A Webresource In Dynamics CRM

Introduction

During certain scenarios, we must send some parameters to a Webresource. To achieve this we must make use of comma separated list of parameters option that is present at event level of a form. As an example contact form was taken and passed first name logical name as parameter to get to know about the first name of the selected contact.

Step 1

Login to the required environment and select the required solution [Contact Customizations Solution in this case] as shown in the below figure.

Pass Parameters To A Webresource In Dynamics CRM

Step 2

After Step 1, select contact web resource in solution and click on Edit as shown in the below figure.

Pass Parameters To A Webresource In Dynamics CRM

Step 3

After Step 2, open contact main form and navigate to on load event and under configure event sub window, look for the section “Comma separated list of parameters that will be passed to the function” and pass logical name of contact firstname as “firstname” as shown in the below figure.

Pass Parameters To A Webresource In Dynamics CRM

Step 4

After Step 3, in Webresource javascript write the following code passing parameter contactfirstnameschema to verifyfirstname function so that in the code using formcontext respective firstname details can be fetched with the below code.

function verifyfirstname(executionContext, contactfirstnameschema) {
    //first name logical name
    let formContext = executionContext.getFormContext();
    if (formContext !== null && formContext != 'undefined') {
        var firstname = formContext.getAttribute(contactfirstnameschema).getValue();
        alert("First Name:" + firstname);
    }
}

And as shown in the below figure.

Pass Parameters To A Webresource In Dynamics CRM

Step 5

After Step 4, call the above function from respective event in this case from handleOnLoad function and the final code looks like below

handleOnLoad: function(executionContext, contactfirstnameschema) {
    console.log('on load - contact form');
    verifyfirstname(executionContext, contactfirstnameschema);
},

Pass Parameters To A Webresource In Dynamics CRM

Step 6

After Step 5, final code looks like below

if (typeof(ContosoVaccination) == "undefined") {
    var ContosoVaccination = {
        __namespace: true
    };
}
if (typeof(ContosoVaccination.Scripts) == "undefined") {
    ContosoVaccination.Scripts = {
        __namespace: true
    };
}
ContosoVaccination.Scripts.ContactForm = {
    handleOnLoad: function(executionContext, contactfirstnameschema) {
        console.log('on load - contact form');
        verifyfirstname(executionContext, contactfirstnameschema);
    },
    __namespace: true
}

function verifyfirstname(executionContext, contactfirstnameschema) {
    //firstname logical name
    let formContext = executionContext.getFormContext();
    if (formContext !== null && formContext != 'undefined') {
        var firstname = formContext.getAttribute(contactfirstnameschema).getValue();
        alert("First Name:" + firstname);
    }
}

Pass Parameters To A Webresource In Dynamics CRM

Step 7

After Step 6, save Webresource code and publish it and then open any contact record and observe an alert with firstname as shown in the below figure

Pass Parameters To A Webresource In Dynamics CRM

Note

  1. Make sure to publish all customizations and upload JavaScript (js) file.
  2. Make sure to give correct logical name with double quotes at the form level event handler

Conclusion

In this way, one can easily pass parameters to a function present in a javascript webresource.


Similar Articles