locked
while creating record update lookup field in the form RRS feed

  • Question

  • Hi,

    I created one custom entity "Enquiry" and  created one contact lookup field on the form.

    while creating  enquiry record , i am also creating contact record , but same contact should be update in contact lookup field on the enquiry form.

    please find the below(I registered this plugin on post operation):-

                                

     protected void ExecutePostEnquiryCreate(LocalPluginContext localContext)
            {
                if (localContext == null)
                {
                    throw new ArgumentNullException("localContext");
                }

                // TODO: Implement your custom Plug-in business logic.
                IPluginExecutionContext context = localContext.PluginExecutionContext;
                IOrganizationService services = localContext.OrganizationService;
                ITracingService tracing = localContext.TracingService;
                if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
                {
                    Entity objEn = (Entity)context.InputParameters["Target"];
                    try
                    {
                        if (objEn.LogicalName == "new_enquiry")
                        {
                            string firstName = objEn.GetAttributeValue<string>("new_firstname");
                            string lastName = objEn.GetAttributeValue<string>("new_lastname");
                            string jobTitle = objEn.GetAttributeValue<string>("new_jobtitle");
                            string email = objEn.GetAttributeValue<string>("new_email");
                            string businessPhone = objEn.GetAttributeValue<string>("new_businessphone");
                            string mobile = objEn.GetAttributeValue<string>("new_mobile");


                            Entity objContact = new Entity("contact");
                            objContact.Attributes.Add("firstname", firstName);
                            objContact.Attributes.Add("lastname", lastName);
                            objContact.Attributes.Add("jobtitle", jobTitle);
                            objContact.Attributes.Add("emailaddress1", email);
                            objContact.Attributes.Add("telephone1", businessPhone);
                            objContact.Attributes.Add("mobilephone", mobile);
                            services.Create(objContact);

                            if (context.OutputParameters.Contains("id"))
                            {
                                Guid regardingobjectid = new Guid(context.OutputParameters["id"].ToString());

                                ConditionExpression condition = new ConditionExpression();
                                condition.EntityName = "contact";
                                condition.AttributeName = "firstname";
                                condition.Operator = ConditionOperator.Equal;
                                condition.Values.Add(firstName);

                                FilterExpression filter = new FilterExpression();
                                filter.Conditions.Add(condition);

                                QueryExpression objQuery = new QueryExpression();
                                objQuery.EntityName = "contact";
                                objQuery.ColumnSet = new ColumnSet();
                                objQuery.ColumnSet.AddColumns("contactid");
                                objQuery.Criteria.AddFilter(filter);

                                EntityCollection retrieved = services.RetrieveMultiple(objQuery);

                                foreach (var c in retrieved.Entities)
                                {
                                    if (objEn.Attributes.Contains("new_contact"))
                                    {
                                    }
                                    else
                                    {
                                        objEn["new_contact"] = new EntityReference("contact", new Guid(c.Id.ToString()));
                                    }
                                }
                            }
                        }
                    }
                    catch (FaultException ex)
                    {
                        tracing.Trace("tracking error");
                        throw new InvalidPluginExecutionException("Exception:" + ex.Message + "" + ex.StackTrace);
                    }
                }
            }
        }
    }

    Please help me .

    Thanks

    Mintu Shahid

    Wednesday, November 26, 2014 3:16 PM

Answers

  • Hello Mintu,

    You need to register your plugin on pre event and replace your code like below

    protected void ExecutePostEnquiryCreate(LocalPluginContext localContext)
            {
                if (localContext == null)
                {
                    throw new ArgumentNullException("localContext");
                }

                // TODO: Implement your custom Plug-in business logic.
                IPluginExecutionContext context = localContext.PluginExecutionContext;
                IOrganizationService services = localContext.OrganizationService;
                ITracingService tracing = localContext.TracingService;
                if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
                {
                    Entity objEn = (Entity)context.InputParameters["Target"];
                    try
                    {
                        if (objEn.LogicalName == "new_enquiry")
                        {
                            string firstName = objEn.GetAttributeValue<string>("new_firstname");
                            string lastName = objEn.GetAttributeValue<string>("new_lastname");
                            string jobTitle = objEn.GetAttributeValue<string>("new_jobtitle");
                            string email = objEn.GetAttributeValue<string>("new_email");
                            string businessPhone = objEn.GetAttributeValue<string>("new_businessphone");
                            string mobile = objEn.GetAttributeValue<string>("new_mobile");

                          Entity objContact = new Entity("contact");
                            objContact.Attributes.Add("firstname", firstName);
                            objContact.Attributes.Add("lastname", lastName);
                            objContact.Attributes.Add("jobtitle", jobTitle);
                            objContact.Attributes.Add("emailaddress1", email);
                            objContact.Attributes.Add("telephone1", businessPhone);
                            objContact.Attributes.Add("mobilephone", mobile);
                           Guid contactid= services.Create(objContact);

                          objEn["new_contact"] = new EntityReference("contact",contactid);

    }

     catch (FaultException ex)
                    {
                        tracing.Trace("tracking error");
                        throw new InvalidPluginExecutionException("Exception:" + ex.Message + "" + ex.StackTrace);
                    }

    }}}


    Microsoft Dynamics CRM Training|Our Blog | Follow US | Our Facebook Page | Microsoft Dynamics CRM 2011 Application Design
    Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.

    • Marked as answer by Mintu_MS_CRM Wednesday, November 26, 2014 3:59 PM
    Wednesday, November 26, 2014 3:49 PM
    Moderator

All replies

  • Hello Mintu,

    You need to register your plugin on pre event and replace your code like below

    protected void ExecutePostEnquiryCreate(LocalPluginContext localContext)
            {
                if (localContext == null)
                {
                    throw new ArgumentNullException("localContext");
                }

                // TODO: Implement your custom Plug-in business logic.
                IPluginExecutionContext context = localContext.PluginExecutionContext;
                IOrganizationService services = localContext.OrganizationService;
                ITracingService tracing = localContext.TracingService;
                if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
                {
                    Entity objEn = (Entity)context.InputParameters["Target"];
                    try
                    {
                        if (objEn.LogicalName == "new_enquiry")
                        {
                            string firstName = objEn.GetAttributeValue<string>("new_firstname");
                            string lastName = objEn.GetAttributeValue<string>("new_lastname");
                            string jobTitle = objEn.GetAttributeValue<string>("new_jobtitle");
                            string email = objEn.GetAttributeValue<string>("new_email");
                            string businessPhone = objEn.GetAttributeValue<string>("new_businessphone");
                            string mobile = objEn.GetAttributeValue<string>("new_mobile");

                          Entity objContact = new Entity("contact");
                            objContact.Attributes.Add("firstname", firstName);
                            objContact.Attributes.Add("lastname", lastName);
                            objContact.Attributes.Add("jobtitle", jobTitle);
                            objContact.Attributes.Add("emailaddress1", email);
                            objContact.Attributes.Add("telephone1", businessPhone);
                            objContact.Attributes.Add("mobilephone", mobile);
                           Guid contactid= services.Create(objContact);

                          objEn["new_contact"] = new EntityReference("contact",contactid);

    }

     catch (FaultException ex)
                    {
                        tracing.Trace("tracking error");
                        throw new InvalidPluginExecutionException("Exception:" + ex.Message + "" + ex.StackTrace);
                    }

    }}}


    Microsoft Dynamics CRM Training|Our Blog | Follow US | Our Facebook Page | Microsoft Dynamics CRM 2011 Application Design
    Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.

    • Marked as answer by Mintu_MS_CRM Wednesday, November 26, 2014 3:59 PM
    Wednesday, November 26, 2014 3:49 PM
    Moderator
  • Thanks a lot
    Wednesday, November 26, 2014 4:00 PM