update record in PCF Archives - Microsoft Dynamics 365 Blog http://microsoftdynamics.in/tag/update-record-in-pcf/ Microsoft Dynamics CRM . Microsoft Power Platform Thu, 09 Feb 2023 21:08:46 +0000 en-US hourly 1 https://wordpress.org/?v=6.5.5 https://i0.wp.com/microsoftdynamics.in/wp-content/uploads/2020/04/cropped-Microsoftdynamics365-blogs.png?fit=32%2C32 update record in PCF Archives - Microsoft Dynamics 365 Blog http://microsoftdynamics.in/tag/update-record-in-pcf/ 32 32 176351444 Update record in PCF Microsoft dynamics CE , Patch a record in PCF control, Code to update field in PCF , Code to update Field using WEBAPI PCF javascript http://microsoftdynamics.in/2023/02/10/update-record-in-pcf-microsoft-dynamics-ce-patch-a-record-in-pcf-control-code-to-update-field-in-pcf-code-to-update-field-using-webapi-pcf-javascript/ Thu, 09 Feb 2023 21:08:46 +0000 https://microsoftdynamics.in/?p=4665 In PCF (Power Apps Component Framework) for Microsoft Dynamics CRM, you can update an input field by handling the “onChange” event for the field and using the Web API to update the corresponding record. Here’s an example of how to do this: import { IInputs, IOutputs } from "./generated/ManifestTypes"; export class PCFInput implements ComponentFramework.StandardControl {...

The post Update record in PCF Microsoft dynamics CE , Patch a record in PCF control, Code to update field in PCF , Code to update Field using WEBAPI PCF javascript appeared first on Microsoft Dynamics 365 Blog.

]]>
In PCF (Power Apps Component Framework) for Microsoft Dynamics CRM, you can update an input field by handling the “onChange” event for the field and using the Web API to update the corresponding record. Here’s an example of how to do this:

import { IInputs, IOutputs } from "./generated/ManifestTypes";

export class PCFInput implements ComponentFramework.StandardControl {
    private _context: ComponentFramework.Context;
    private _input: HTMLInputElement;

    constructor() { }

    public init(context: ComponentFramework.Context, notifyOutputChanged: () => void, state: ComponentFramework.Dictionary, container:HTMLDivElement) {
        // Store the context for later use
        this._context = context;

        // Create an input element
        this._input = document.createElement("input");

        // Add the input to the container element
        container.appendChild(this._input);

        // Add a change event listener to the input
        this._input.addEventListener("change", (event) => {
            // Get the new value
            let newValue = this._input.value;

            // Update the record using the Web API
            let entityName = this._context.parameters.entityName.raw || "";
            let recordId = this._context.parameters.recordId.raw || "";
            let fieldName = this._context.parameters.fieldName.raw || "";

            let entity = {};
            entity[fieldName] = newValue;

            let request = new XMLHttpRequest();
            request.open("PATCH", encodeURI(`/${entityName}s(${recordId})`), true);
            request.setRequestHeader("OData-MaxVersion", "4.0");
            request.setRequestHeader("OData-Version", "4.0");
            request.setRequestHeader("Accept", "application/json");
            request.setRequestHeader("Content-Type", "application/json; charset=utf-8");
            request.onreadystatechange = () => {
                if (request.readyState === 4 && request.status === 204) {
                    // The update was successful
                    // ...
                }
                else if (request.readyState === 4) {
                    // There was an error
                    // ...
                }
            };
            request.send(JSON.stringify(entity));

            // Notify the platform that the value has changed
            notifyOutputChanged();
        });

        // Set the initial value
        this._input.value = this._context.parameters.fieldValue.raw || "";
    }

    // ...
}

In this code, we create an input element and add it to the container element. We also add a change event listener to the input using the addEventListener method. In the event listener, we get the new value using the value property and update the record using the Web API. The entityName, recordId, and fieldName parameters are passed to the control from the platform and used to determine which record and field to update. Finally, we call the notifyOutputChanged function to notify the platform that the value has changed.

The post Update record in PCF Microsoft dynamics CE , Patch a record in PCF control, Code to update field in PCF , Code to update Field using WEBAPI PCF javascript appeared first on Microsoft Dynamics 365 Blog.

]]>
4665