Click here to Skip to main content
15,881,715 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
//Setting the current document guid
string fetch = this.FetchXmlString.Get(executionContext);
fetch = fetch.Replace(
    "value=""",
    "value="" + context.PrimaryEntityId.ToString() + """);

//execute fetch



When I built it, its showing me the following error:

) expected,
; expected

Invalid expression term ')'
Invalid expression term ','

Only assignmen, Call, increment, decrement, and new object expressions can be used as a statement.


can I know what happen actually ?? how can I solve it ??
please help!! thank you!!

The following code is my Full Coding in C#:

C#
namespace xyz.Crm.Workflow
{
    using System;
    using System.Activities;
    using System.Linq;
    using System.ServiceModel;
    using System.Text;
    using Microsoft.Xrm.Sdk;
    using Microsoft.Xrm.Sdk.Client;
    using Microsoft.Xrm.Sdk.Messages;
    using Microsoft.Xrm.Sdk.Metadata;
    using Microsoft.Xrm.Sdk.Query;
    using Microsoft.Xrm.Sdk.Workflow;

    public sealed class DokumentPositionen : CodeActivity
    {
        /// <summary>
        /// Executes the workflow activity.
        /// </summary>
        /// <param name="executionContext">The execution context.</param>
        protected override void Execute(CodeActivityContext executionContext)
        {
            // Create the tracing service
            ITracingService tracingService = executionContext.GetExtension<ITracingService>();

            if (tracingService == null)
            {
                throw new InvalidPluginExecutionException("Failed to retrieve tracing service.");
            }

            tracingService.Trace("Entered DokumentPositionen.Execute(), Activity Instance Id: {0}, Workflow Instance Id: {1}",
                executionContext.ActivityInstanceId,
                executionContext.WorkflowInstanceId);

            // Create the context
            IWorkflowContext context = executionContext.GetExtension<IWorkflowContext>();

            if (context == null)
            {
                throw new InvalidPluginExecutionException("Failed to retrieve workflow context.");
            }

            tracingService.Trace("DokumentPositionen.Execute(), Correlation Id: {0}, Initiating User: {1}",
                context.CorrelationId,
                context.InitiatingUserId);

            IOrganizationServiceFactory serviceFactory = executionContext.GetExtension<IOrganizationServiceFactory>();
            IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
            OrganizationServiceContext orgcontext = new OrganizationServiceContext(service);

            try
            {
                //used to collect headers and values of lines
                StringBuilder sb = new StringBuilder();

                //Setting the current document guid
                string fetch = this.FetchXmlString.Get(executionContext);
                fetch = fetch.Replace(
                    "value=""",
                    "value="" + context.PrimaryEntityId.ToString() + """);

                //execute fetch
                EntityCollection entityCollection = service.RetrieveMultiple(new FetchExpression(fetch));

                if (entityCollection.Entities.Count > 0)
                {
                    RetrieveEntityRequest retrieveEntityRequest = new RetrieveEntityRequest
                    {
                        EntityFilters = EntityFilters.Attributes,
                        LogicalName = entityCollection.EntityName
                    };

                    RetrieveEntityResponse retrieveEntityResponse = (RetrieveEntityResponse)service.Execute(retrieveEntityRequest);

                    if (retrieveEntityResponse.EntityMetadata.Attributes == null)
                        throw new Exception("null Wert für Attribute");

                    if (retrieveEntityResponse.EntityMetadata.Attributes.Count() == 0)
                        throw new Exception("0 Attribute");

                    if (entityCollection.Entities.Count == 0)
                        throw new Exception("0 Datensätze");

                    //setting up headers
                    //fetch contains by default in the last 2 fields currency and parent attribute => no need => cut = -2
                    for (int i = 0; i < entityCollection.Entities[0].Attributes.Count - 2; i++)
                    {
                        tracingService.Trace("Suche Attribute mit : {0}", entityCollection.Entities[0].Attributes.ElementAt(i).Key);
                        AttributeMetadata am = retrieveEntityResponse.EntityMetadata.Attributes.Where(x => x.LogicalName == entityCollection.Entities[0].Attributes.ElementAt(i).Key).FirstOrDefault();

                        if (am != null && String.IsNullOrEmpty(am.DisplayName.ToString()) == false)
                        {
                            sb.Append(am.DisplayName.LocalizedLabels.First().Label + " ");
                        }
                    }

                    //collecting line values
                    foreach (var entity in entityCollection.Entities)
                    {
                        sb.AppendLine();

                        //fetch contains by default in the last 2 fields currency and parent attribute => no need => cut = -2
                        for (int i = 0; i < entity.Attributes.Count - 2; i++)
                        {
                            if (entity.Attributes.ElementAt(i).Value is EntityReference)
                            {
                                tracingService.Trace("Attribute wird übersprungen weil Referenz");
                            }
                            else if (entity.Attributes.ElementAt(i).Value is Money)
                            {
                                sb.Append(((Money)entity.Attributes.ElementAt(i).Value).Value.ToString("0.00") + " ");
                            }
                            else if (entity.Attributes.ElementAt(i).Value is Decimal)
                            {
                                sb.Append(((Decimal)entity.Attributes.ElementAt(i).Value).ToString("0.00") + " ");
                            }
                            else
                                sb.Append(entity.Attributes.ElementAt(i).Value.ToString() + " ");

                            // if more types...
                            //http://community.dynamics.com/product/crm/crmnontechnical/b/c5insightblog/archive/2011/03/04/dynamics-crm-2011-reading-attribute-values-inside-your-plug-in.aspx
                        }
                    }
                }
                else
                {
                    //no line in the document
                    sb.AppendLine("Keine Belegpositionen vorhanden");
                }

                this.DokumentPositionenString.Set(executionContext, sb.ToString());
            }
            catch (FaultException<OrganizationServiceFault> e)
            {
                tracingService.Trace("Exception: {0}", e.ToString());

                // Handle the exception.
                throw;
            }

            tracingService.Trace("Exiting DokumentPositionen.Execute(), Correlation Id: {0}", context.CorrelationId);
        }

        [Output("String with Positions")]
        [Default("")]
        public OutArgument<string> DokumentPositionenString { get; set; }

        [RequiredArgument]
        [Input("FetchXML")]
        public InArgument<string> FetchXmlString { get; set; }
    }
}


this is a Custom Workflow in MS CRM 2011
thank you!!
Posted

You could try changing:
C#
fetch = fetch.Replace(
                    "value=""",
                    "value="" + context.PrimaryEntityId.ToString() + """);


to:
C#
fetch = fetch.Replace(
                    "value=""",
                    String.format("value=""{0}""",context.PrimaryEntityId.ToString()));

Because I suspect you just have either too many or not enough " and this should make it easier to process.
 
Share this answer
 
C#
//Setting the current document guid
               string fetch = this.FetchXmlString.Get(executionContext);
               fetch = fetch.Replace(
                   "value="\"",
                   "value=\"" + context.PrimaryEntityId.ToString() + "\"");

               //execute fetch
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900