Back to Basics # 65: 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 an 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 required solution [Contact Customizations Solution in this case] as shown in the   below figure.

Step 2:

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

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.

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.

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);

        },

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);

    }

}

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

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.

One thought on “Back to Basics # 65: Pass Parameters to a Webresource in Dynamics CRM

  1. Pingback: Back To Basics : Curated List of Articles in a Single Page – Common Man Tips for Power Platform, Dynamics CRM,Azure

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s