locked
Filter ServiceAppointment (Activity) by Customers RRS feed

  • Question

  • Hi Everyone,

    I have a problem with ServiceAppointment filtration. I have a list of Service Appointments belong to many accounts. When I created ServiceAppointment I used following code 

    		 var newServiceAppointment = new ServiceAppointment
                    {
                        ServiceId = new   EntityReference(CRMHelper.Service.EntityLogicalName, new Guid(serviceId)),
                        Location = branchTitle,
                        Resources = new ActivityParty[] { equipmentPartyResource },
    
                        ScheduledStart = Convert.ToDateTime(startTime),
                        ScheduledEnd = Convert.ToDateTime(endTime),
                        ScheduledDurationMinutes = scheduledDurationMinutes,
    
                        Customers = new ActivityParty[] { accountPartyResource }
                    };


    where accountPartyResource and equipmentPartyResource defined as following

    var equipmentPartyResource = new ActivityParty
    {
      PartyId = new EntityReference(Equipment.EntityLogicalName, new   Guid(serviceEquipment.equipmentId))
    };
    
    var accountPartyResource = new ActivityParty
    {
      PartyId = new EntityReference(Account.EntityLogicalName, serviceAccount.Id)
    };

    Now the problem is, I need to get all service activities belong to specific account or specific equipment.

    I tried following
    FilterExpressions at QueryExpresion

     QueryExpression targetServiceAppointmentQuery = new QueryExpression
                    {
                        EntityName = ServiceAppointment.EntityLogicalName,
                        ColumnSet = new ColumnSet(
                            "activityid",
                            "subject",
                            "serviceid",
                            "location",
                            "scheduledstart",
                            "scheduledend",
                            "customers",
                            "statecode",
                            "statuscode"),
    
                        Criteria = new FilterExpression()
                        {
                            FilterOperator = LogicalOperator.And,
                            Conditions =
                            {
                                new ConditionExpression
                                {
                                    AttributeName = "customers",
                                    Operator = ConditionOperator.Equal,
                                    Values = { new string[] { serviceAccount.AccountId.ToString() } }
                                }
                            }
                        }
                    };

    Also I tired following formats
     Criteria = new FilterExpression()
                        {
                            FilterOperator = LogicalOperator.And,
                            Conditions =
                            {
                                new ConditionExpression
                                {
                                    AttributeName = "customers",
                                    Operator = ConditionOperator.Equal,
                                    Values = { new ActivityParty[] { accountPartyResource } }
                                }
                            }
                        }
    
    
    //==========================
    
     Conditions =
                            {
                                new ConditionExpression
                                {
                                    AttributeName = "customers",
                                    Operator = ConditionOperator.Equal,
                                    Values = {serviceAccount}
                                }
    }
    
    //=========================
    
     Conditions =
                            {
                                new ConditionExpression
                                {
                                    AttributeName = "customers",
                                    Operator = ConditionOperator.Equal,
                                    Values = {accountPartyResource}
                                }

    For all conditions I got following Error

    		The formatter threw an exception while trying to deserialize the message: 
    		There was an error while trying to deserialize parameter 
    		schemas.microsoft.com/.../Services:query. 
    		The InnerException message was 'Error in line 1 position 1147.
    		Element 'schemas.microsoft.com/.../Arrays:anyType' 
    		contains data from a type that maps to the name 
    		'schemas.microsoft.com/.../Contracts:Entity'. 
    		The deserializer has no knowledge of any type that maps to this name. 
    		Consider changing the implementation of the ResolveName method on your 
    		DataContractResolver to return a non-null value for name 'Entity' and namespace 
    		'schemas.microsoft.com/.../Contracts'.'.  Please see InnerException for more details.

    What I have to do?

    Thanks in advance.

    Wa'el Mohsen

    Sunday, August 7, 2016 2:09 AM

Answers

  • Thanks to following link

    https://community.dynamics.com/crm/f/117/t/139438

    I modified my code to be like 

      // Get service appointment request
                    QueryExpression targetServiceAppointmentQuery = new QueryExpression
                    {
                        EntityName = ServiceAppointment.EntityLogicalName,
                        ColumnSet = new ColumnSet(
                            "activityid",
                            "subject",
                            "serviceid",
                            "location",
                            "scheduledstart",
                            "scheduledend",
                            "new_contactidnumber",
                            "statecode",
                            "statuscode"),
    
                        Criteria = new FilterExpression()
                        {                        
                            Conditions =
                            {                          
                                new ConditionExpression
                                {
                                    AttributeName = "new_contactidnumber",
                                    Operator = ConditionOperator.Equal,
                                    Values = { userIdNumber }
                                }
    
                            }
                        }
                    };
    
                    //Add Joined Table
                    LinkEntity accountChildLink = targetServiceAppointmentQuery.AddLink(
                        ActivityParty.EntityLogicalName, "activityid", "activityid");
    
                    //Add conditions of Joined Table
                    FilterExpression equipmentChildFilterOR = accountChildLink.LinkCriteria.AddFilter(LogicalOperator.And);
    
                    equipmentChildFilterOR.AddCondition("partyid", ConditionOperator.Equal, serviceAccount.Id);
    
                    //Get Data from CRM
                    DataCollection<Entity> serviceAppointmentResult =
                        organizationService.RetrieveMultiple(targetServiceAppointmentQuery).Entities;

    It is worked well.

    Thank you


    Wa'el Mohsen


    • Marked as answer by Wa'el Sunday, August 7, 2016 2:47 AM
    • Edited by Wa'el Sunday, August 7, 2016 2:51 AM
    Sunday, August 7, 2016 2:47 AM