locked
Create annotation with SDK.REST.js and set regarding field. RRS feed

  • Question

  • Hello,

    I'm trying to create annotation entity through javascript (SDK.REST.js library).

    I want to set Regarding field using ObjectId and ObjectTypeCode.
    I have following code:

      function DoWork(fileString, filename) {

                page = window.parent.Xrm.Page;
                var entityId = page.data.entity.getId();
                var entityTypeCode = page.context.getQueryStringParameters().etc;

                //create:
                var data = new Object();            

                data.Subject = "Test subject";
                data.NoteText = "Test note text";
                data.DocumentBody = fileString;
                data.FileName = filename;          
                data.ObjectId = { Id: entityId, LogicalName: "account", Name: "NameOfAccount" };
                data.IsDocument = true;          
                //data.ObjectTypeCode = { Value: 1 };
       //data.ObjectTypeCode = { Value: "1" };
                data.ObjectTypeCode = { Value: entityTypeCode };

                console.log(JSON.stringify(data)); //for debugging

                SDK.REST.createRecord(
                    data,
                    "Annotation",
                    function () {
                        console.log("succ");
                    },
                    function (error) {
                        ShowError(error.message);
                        //console.log(JSON.stringify(error));
                    });


            }


    However, I get error "Specified cast is not valid."  in event log.

    The Web Service plug-in failed in OrganizationId: a731808e-cc01-e411-80c9-00155d0a0aac; SdkMessageProcessingStepId: 06cabb1b-ea3e-db11-86a7-000a3a5473e8; EntityName: annotation; Stage: 30; MessageName: Create; AssemblyName: Microsoft.Crm.Extensibility.InternalOperationPlugin, Microsoft.Crm.ObjectModel, Version=7.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35; ClassName: Microsoft.Crm.Extensibility.InternalOperationPlugin; Exception: Unhandled Exception: System.InvalidCastException: Specified cast is not valid.
       at Microsoft.Crm.BusinessEntities.EntityNameReferenceAttributeConverter.ConvertToBusinessEntity(Object value, AttributeMetadata attributeMetadata, Dictionary`2 abbrvToChildAttributeMetadata, ICrmConversionContext context, BusinessEntity target)
       at Microsoft.Crm.BusinessEntities.EntityToBusinessEntityConverter.ConvertUsingExistingBusinessEntity(EntityMetadata entityMetadata, ICrmConversionContext conversionContext, Entity entity, BusinessEntity businessEntity)
       at Microsoft.Crm.BusinessEntities.BusinessEntity.Converter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value)
       at Microsoft.Crm.BusinessEntities.ConversionHelpers.Convert(ICrmConversionContext conversionContext, Object source, Type destinationType)
       at Microsoft.Crm.Extensibility.DictionaryMapper.Map(ParameterCollection inputs, ICrmConversionContext context)
       at Microsoft.Crm.Extensibility.InternalOperationPlugin.Execute(IServiceProvider serviceProvider)
       at Microsoft.Crm.Extensibility.V5PluginProxyStep.ExecuteInternal(PipelineExecutionContext context)
       at Microsoft.Crm.Extensibility.VersionedPluginProxyStepBase.Execute(PipelineExecutionContext context)


    In reality, I'm trying to create annotation with regarding field set on some custom entity, but for start I want to make this work for Account entities.

    What am I doing wrong? 
    When I remove this ObjectId and ObjectTypeCode attributes, annotation is created. 
    How can I set Regarding through javascript?

    Monday, June 29, 2015 12:52 PM

All replies

  • Try remove line: 
    data.ObjectTypeCode = { Value: entityTypeCode };

    As regarding set only 
    data.ObjectId =
    {
    Id: entityId, 
    LogicalName: "account"
    };


    Wednesday, January 3, 2018 9:19 AM