Question

I have following code to fetch an entity records:

private void FetchEvent(string EventId, IOrganizationService crmService)
    {
        // Create the ColumnSet to be retrieved.
        ColumnSet columnSet = new ColumnSet();
        // Set the properties of the ColumnSet.
        columnSet.AddColumn("campaignid");
        columnSet.AddColumn("name");
        columnSet.AddColumn("description");
        columnSet.AddColumn("renre_giftid");
        columnSet.AddColumn("ownerid");  

        // Create the FilterExpression.
        FilterExpression filterExpression = new FilterExpression();

        // Create the ConditionExpression.
        ConditionExpression conditionExpression = new ConditionExpression();

        // Set the condition for the retrieval based on customer id field.
        conditionExpression.AttributeName = "campaignid";
        conditionExpression.Operator = ConditionOperator.Equal;
        conditionExpression.Values.Add(new string[]{EventId});

        filterExpression.FilterOperator = LogicalOperator.And;
        // Set the properties of the filter.
        filterExpression.Conditions.Add(conditionExpression);

        // Create the QueryExpression object.
        QueryExpression queryExpression = new QueryExpression();

        // Set the properties of the QueryExpression object.
        queryExpression.EntityName = "campaign";//EntityName.campaign.ToString();
        queryExpression.ColumnSet = columnSet;
        queryExpression.Criteria = filterExpression;

        RetrieveMultipleRequest InvitationResponseRequest = new RetrieveMultipleRequest();
        InvitationResponseRequest.Query = queryExpression;
        //InvitationResponseRequest.ReturnDynamicEntities = true;

        eventEntity = (Entity)((RetrieveMultipleResponse)crmService.Execute(InvitationResponseRequest)).EntityCollection.Entities[0];
    }

When I debug, after reaching the last line of this function eventEntity = (Entity)((RetrieveMultipleResponse)crmService.Execute(InvitationResponseRequest)).EntityCollection.Entities[0]; It gives an exception that:

Condition for attribute 'campaign.campaignid': expected argument(s) of type 'System.Guid' but received 'System.String[]'.

Please suggest.

Was it helpful?

Solution

You're passing a string array as the value of EntityId.

Change this:

conditionExpression.Values.Add(new string[]{EventId});

to this:

conditionExpression.Values.Add(new Guid(EventId));

You might also want to think about changing the type on the input parameter to a Guid and fixing the Pascal Casing on EventId argument to camel for consistency

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top