Search code examples
javascriptdynamics-crm-2011dynamics-crmodata

Error while creating Service Activity through Javascript


I am trying to create a service activity but it throws an error, code is below:

var oDataPath = Xrm.Page.context.getServerUrl() + "/XRMServices/2011/OrganizationData.svc";

    var oDataSetName = "ServiceAppointmentSet";
    var entityTypeCode = "4212"; // Type code of ServiceAppointment
    var subject = 'Test Subject';
    var entityId = Xrm.Page.data.entity.getId();
    var serviceId = "AFE9429-ABCE-E211-877A-000C298A36A7";

    var customactivity = new Object();
    customactivity.Subject = subject;
    customactivity.ActivityTypeCode = entityTypeCode;    

    customactivity.RegardingObjectId =
    {
        Id: entityId,
        LogicalName: "incident",
        Name: subject
    };

    var punchInTime = GetPunchInTime();
    customactivity.ScheduledStart = punchInTime;
    customactivity.ScheduledEnd = punchInTime;


    //---- serviceId


    if (serviceId != null) {
        customactivity.ServiceId =
            {
                Id: serviceId,
                LogicalName: "service",
                Name: "Support"
            };
    }

    var jsonCustomActivity = JSON.stringify(customactivity);

    var createRequest = new XMLHttpRequest();
    createRequest.open("POST", oDataPath + "/" + oDataSetName, false);
    createRequest.setRequestHeader("Accept", "application/json");
    createRequest.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    createRequest.send(jsonCustomActivity);

Error description:

Status: 400 Bad Request

Error:

Error processing request stream. Error encountered in converting the value from request payload for property 'Id' to type 'Guid', 
which is the property's expected type. See inner exception for more detail.

Can someone help me to find the issue?


Solution

  • Service id has only 7 characters instead of 8

    var serviceId = "AFE9429-ABCE-E211-877A-000C298A36A7";