dynamics-crmmicrosoft-dynamicspowerapps

How to add a column/attribute dynamically by clicking a button in MS Dynamics 365?


I've created a Ribbon Workbench button and added an action of JavaScript to create a string attribute('resource_name') in the Opportunity Table. But, when I click on the button nothing happens, it neither shows success nor failure.

Need your expert advice on whether to change the script or to go with another approach. Please share a way to create new attributes dynamically at the click of a button. Dynamics 365/PowerApps

Javascript Used -

function addDemoAttribute() {
    var entityName = "opportunity";
    var attributeName = "resuource_name";
    var attributeDisplayName = "Resource Name";
    var maxLength = 100;
    var description = "A demo attribute integration";
    var attributeMetadata = {
        "@odata.type": "Microsoft.Dynamics.CRM.StringAttributeMetadata",
        "LogicalName": attributeName,
        "DisplayName": {
            "LocalizedLabels": [
                {
                    "Label": attributeDisplayName,
                    "LanguageCode": 1033
                }
            ]
        },
        "MaxLength": maxLength,
        "RequiredLevel": {
            "Value": "None"
        },
        "Description": {
            "LocalizedLabels": [
                {
                    "Label": description,
                    "LanguageCode": 1033
                }
            ]
        },
        "SchemaName": attributeName
    };
    var request = {
        entity: {
            logicalName: entityName
        },
        MetadataId: null,
        AttributeType: 'String',
        Attribute: attributeMetadata
    };
    Xrm.WebApi.online.execute(request).then(
        function success(result) {
            if (result.ok) {
                Xrm.Navigation.openAlertDialog({ text: "Attribute created successfully." });
            }
        },
        function(error) {
            Xrm.Navigation.openErrorDialog({ message: "Error creating attribute: " + error.message });
        }
    );
}

I tried JS code mentioned above. It will be really helpful if someone can guide me with a better approach to add attributes dynamically in Tables/Entities in MS Dynamics.


Solution

  • to do this you can call a HTTP POST Url and pass the payload. to call this URL you can use JS function or a workflow (i used workflow).

    this is the payload to hit and the URL

    URL:https://<ORGANIZATION-URL>/api/data/v9.1/EntityDefinitions(LogicalName='<ENTITY-NAME>')/Attributes

    Payload:

    {
      "SchemaName": "new_fieldname",
      "LogicalName": "new_fieldname",
      "DisplayName": {
        "LocalizedLabels": [
          {
            "Label": "New Field Title",
            "LanguageCode": 1033,
            "@odata.type": "Microsoft.Dynamics.CRM.LocalizedLabel"
          }
        ],
        "@odata.type": "Microsoft.Dynamics.CRM.Label"
      },
      "RequiredLevel": {
        "Value": "None",
        "@odata.type": "Microsoft.Dynamics.CRM.AttributeRequiredLevelManagedProperty"
      },
      "MaxLength": 100,
      "@odata.type": "Microsoft.Dynamics.CRM.StringAttributeMetadata"
    }
    

    My Test result:

    I used postman to trigger my workflow where i dynamically pass the field name, entity name, schema name and datatype enter image description here

    Once triggered, the flow composes the body and hits the URL.

    enter image description here

    as a result, the table is created dynamically.

    enter image description here