locked
Plugin, insert value to a field (Type: Lookup) RRS feed

  • Question

  • I'm trying through a plugin to insert a value into a field of a Custom entity. But when I try to update the record, nothing happens.
    new_taskissuephase is type:Lookup and Target Record type:Task Issue Phase.

    The value I am trying to insert into lookup field is "Completed". So I gave the guid of the 'Completed' value in the last line of the code.

    Here is my code:

     if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
                {

                    //create entity context
                    Entity entity = (Entity)context.InputParameters["Target"];

                    if (entity.LogicalName != "task")

                        return;

                    try
                    {
                        if(entity.Contains("statecode"))
                        {
                         OptionSetValue entityStatusCode =
                                      (OptionSetValue)entity.Attributes["statecode"];

                            if(entityStatusCode.Value==2)
                            {
                                //update the description field
                            entity["new_instructions"] = String.Format("Canceled on {0}", DateTime.Now.ToString("dd/MM/yyyy"));
                            Entity task = new Entity("task");
                            service.Retrieve(task.LogicalName, entity.Id, new ColumnSet(new string[] { "new_taskissuephase" }));
                                task.Attributes.Add("new_taskissuephase",new EntityReference("new_taskissuephase",new Guid("56B27740-9C48-E311-93F1-001D09747ED0")));
                            }
                         }
                     }
                  }


    I am really not sure about the last line of adding attribute to 'task' entity. Can someone please help me and let me know if I am doing correct.
    Thanks

    Wednesday, December 17, 2014 9:23 PM

All replies

  • Hi roxannaappleby,
    You need to change below 2 lines of code:

    Entity task = new Entity("task");
    service.Retrieve(task.LogicalName, entity.Id, new ColumnSet(new string[] { "new_taskissuephase" }));
    task.Attributes.Add("new_taskissuephase", new EntityReference("new_taskissuephase", new Guid("56B27740-9C48-E311-93F1-001D09747ED0")));

    Change them to 

    entity.Attributes.Add("new_taskissuephase", new EntityReference("new_taskissuephase", new Guid("56B27740-9C48-E311-93F1-001D09747ED0")));

    Sachith Chandrasiri


    • Edited by Sachith Vidanage Thursday, December 18, 2014 12:24 AM Change of code
    Thursday, December 18, 2014 12:18 AM
  • Thanks Sachith..

    but it didn't work!

    Thursday, December 18, 2014 2:22 AM
  • Are you sure you have registered the plug-in the pre-stage?

    Thanks


    Sachith Chandrasiri

    Thursday, December 18, 2014 2:37 AM
  • Hi,

    Your plugin checks whether the entity contains the statecode attribute and then checks whether the statecode =2. This condition satisfy only when you cancel the  Task. Did you try cancelling a task?

    Thanks


    Sachith Chandrasiri

    Thursday, December 18, 2014 2:55 AM
  • Could you check the below line again?

    Istn't the field name  of the look-up fieldis "new_taskissuephaseid" instead of "new_taskissuephase" ??

     task.Attributes.Add("new_taskissuephase",new EntityReference("new_taskissuephase",new Guid("56B27740-9C48-E311-93F1-001D09747ED0")));


    Sachith Chandrasiri

    Thursday, December 18, 2014 3:24 AM
  •  I registered on Pre-Operation and Update message.

    Also, I am canceling the task. Since it is pre update, before task gets canceled these updates should happen.

    For testing purposes, I am updating an attribute 'new_instructions' and a lookup field 'new_taskissuephase'.

    If I test only updating instructions without taskissuephase, plugin is working as expected. But both updates failing when I include both updates in the code.

    Field name is new_taskissuephase.. but I tried with new_taskissuephaseid as well just to make sure and it didn't work that way too..

    Thanks

    Thursday, December 18, 2014 7:49 PM
  • Try debugging the plugin. If you are unable then take your code out of the try block and see whether it throws any exception.
    Else try throwing invalid plugin execution exceptions and see upto which line your code works. This might give you a clue.
    Also check whether your Guid is correct. 

    Thanks
    Sachith

    Sachith Chandrasiri

    Thursday, December 18, 2014 10:43 PM
  • Hi,

    Please check this below code :

    if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
                 {
                    //create entity context
                     Entity entity = (Entity)context.InputParameters["Target"];
     
                    if (entity.LogicalName != "task")
                    return;

                    try
                     {
                         if(entity.Contains("statecode"))
                         {
                          OptionSetValue entityStatusCode = (OptionSetValue)entity.Attributes["statecode"];
                                  
                            if(entityStatusCode.Value==2)
                             {
                                // Update the Value
                if(entity.Attributes.Contains("new_taskissuephase") && entity.Attributes["new_taskissuephase"] != null)
      { 
       entity.Attributes["new_taskissuephase"] = new EntityReference("new_taskissuephase",new Guid("56B27740-9C48-E311-93F1-001D09747ED0"));
      }
                else
      {
       entity.Attributes.Add("new_taskissuephase",new EntityReference("new_taskissuephase",new Guid("56B27740-9C48-E311-93F1-001D09747ED0")));
      }

                if(entity.Attributes.Contains("new_instructions") && entity.Attributes["new_instructions"] != null)
      {
       entity["new_instructions"] = String.Format("Canceled on {0}", DateTime.Now.ToString("dd/MM/yyyy"));
      }
                else
      {
       entity.Attributes.Add("new_instructions",DateTime.Now.ToString("dd/MM/yyyy"));
      }

      
                             }
                          }
                      }
                   }

    Please resolve if any syntax error find.

    Hope It will work for you.


    Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.

    Friday, December 19, 2014 11:32 AM