Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Plugin Error message - System.InvalidCastException: Specified cast is not valid.

(0) ShareShare
ReportReport
Posted on by 1,530

Hi

I can't figure out why I am get this error message System.InvalidCastException: Specified cast is not valid.

I am doing a loop to make copies of detail records.

I am creating a reference to add to a lookup field.

Any ideal what is causing this error message?

                    //Instead of iterating through all the child records and creating one by one we can also use ExecuteMultiple method
                    foreach (Entity retrieve in service.RetrieveMultiple(query).Entities)
                    {
                        //Remove the primary key of the retrieved associated record
                        retrieve.Attributes.Remove("new_selectortemplatequestionid");
                        retrieve.Attributes.Remove("new_selectortemplateid");

                        //Set the primary key value to Guid.NewGuid()
                        retrieve.Id = Guid.NewGuid();

                        EntityReference newselectorTemplateLookup = new EntityReference("new_selectortemplate", selectortemplateId);

                        retrieve.Attributes.Add("new_selectortemplateid", newselectorTemplateLookup);
                        service.Create(retrieve);
                    }


*This post is locked for comments

  • Verified answer
    rthompson Profile Picture
    rthompson 1,530 on at
    RE: Plugin Error message - System.InvalidCastException: Specified cast is not valid.

    My problem had to do with another post operation trigger happening on another entity.

  • Suggested answer
    RaviKashyap Profile Picture
    RaviKashyap 55,410 Moderator on at
    RE: Plugin Error message - System.InvalidCastException: Specified cast is not valid.

    Hi,

    Are you still getting the invalid case exception or something else?

    Can you try a small sample to create a copy of the record but with just one attribute to copy i.e. foreach record, create a new record and just add one field instead.

    Is working fine when you are running as async?

  • rthompson Profile Picture
    rthompson 1,530 on at
    RE: Plugin Error message - System.InvalidCastException: Specified cast is not valid.

    Can I pay you for help.  If so,  how much an hour

  • rthompson Profile Picture
    rthompson 1,530 on at
    RE: Plugin Error message - System.InvalidCastException: Specified cast is not valid.

    Hi,

    I am using a plugin post operation.

    The selectortemplateid is coming from the primary entity that is not committed.  I am using "Aync".

    selectortemplateid = 12bf3c3b-5f2c-e811-80da-005056ba7ce0

    I am creating a new entity and trying to copy detail entity from another entity.

    I am seeing the record in the table.

    The reference is not returning the name to the lookup.  I believe this might be causing my error message.  But not sure.

    If you notice from the image the lookup is not getting creating completely.  The "ExtensionData" is null and the "Name" is null.

    Primary.JPG

    Here is code that I am using.

    EntityReference newselectorTemplateLookup = SetReference("new_selectortemplate", selectortemplateId);
    newselectorTemplateLookup.Name = selectortemplatename;
    questionTemplate.Attributes["new_selectortemplateid"] = newselectorTemplateLookup;


    Here the primary entity

    Primary.JPG

    I tried to add the "Name" from the Primary but that still did not fix the error message.

    foreach (Entity _entity in result.Entities)
    {
      Entity questionTemplate = new Entity(_entity.LogicalName) { Id = Guid.NewGuid() };
    
      questionTemplate.Attributes.Add("new_name", _entity.GetAttributeValue<string>("new_name") ?? string.Empty);
      questionTemplate.Attributes.Add("new_sectionid", _entity.GetAttributeValue<EntityReference>("new_sectionid"));
      questionTemplate.Attributes.Add("new_questionid", _entity.GetAttributeValue<EntityReference>("new_questionid"));
    
      EntityReference newselectorTemplateLookup = SetReference("new_selectortemplate", selectortemplateId);
      newselectorTemplateLookup.Name = selectortemplatename;
      questionTemplate.Attributes["new_selectortemplateid"] = newselectorTemplateLookup;
    
      service.Create(questionTemplate);
    }


  • rthompson Profile Picture
    rthompson 1,530 on at
    RE: Plugin Error message - System.InvalidCastException: Specified cast is not valid.

    I decided to change the code to assign each attribute.

    Still getting the same error message.

                        // Loop through each detail record to copy
                        EntityCollection result = service.RetrieveMultiple(query);
                        foreach (Entity _entity in result.Entities)
                        {
                            Entity questionTemplate = new Entity();
    
                            questionTemplate.LogicalName = _entity.LogicalName;
    
                            questionTemplate.Id = Guid.NewGuid();
    
                            if (_entity.Attributes.Contains("new_name"))
                            {
                                questionTemplate["new_name"] = _entity.Attributes["new_name"];
                            }
    
                            if (_entity.Attributes.Contains("new_sectionid"))
                            {
                                EntityReference sectionLookup = (EntityReference)_entity.Attributes["new_sectionid"];
                                questionTemplate["new_sectionid"] = sectionLookup;
                            }
    
                            if (_entity.Attributes.Contains("new_questionid"))
                            {
                                EntityReference questionLookup = (EntityReference)_entity.Attributes["new_questionid"];
                                questionTemplate["new_questionid"] = questionLookup;
                            }
    
                            if (_entity.Attributes.Contains("new_selectortemplateid"))
                            {
                                EntityReference newselectorTemplateLookup = SetReference("new_selectortemplate", postImageSelectorTemplateId);
                                questionTemplate["new_selectortemplateid"] = newselectorTemplateLookup;
                            }
    
                            service.Create(questionTemplate);
                        }


  • rthompson Profile Picture
    rthompson 1,530 on at
    RE: Plugin Error message - System.InvalidCastException: Specified cast is not valid.

    I change the logic to add each attribute to make sure I was not missing anything.

    I am still getting the same error message.

    Here's my new code

                        // Loop through each detail record to copy
                        EntityCollection result = service.RetrieveMultiple(query);
                        foreach (Entity _entity in result.Entities)
                        {
                            Entity questionTemplate = new Entity();
    
                            questionTemplate.LogicalName = _entity.LogicalName;
    
                            questionTemplate.Id = Guid.NewGuid();
    
                            if (_entity.Attributes.Contains("new_name"))
                            {
                                questionTemplate["new_name"] = _entity.Attributes["new_name"];
                            }
    
                            if (_entity.Attributes.Contains("new_sectionid"))
                            {
                                EntityReference sectionLookup = (EntityReference)_entity.Attributes["new_sectionid"];
                                questionTemplate["new_sectionid"] = sectionLookup;
                            }
    
                            if (_entity.Attributes.Contains("new_questionid"))
                            {
                                EntityReference questionLookup = (EntityReference)_entity.Attributes["new_questionid"];
                                questionTemplate["new_questionid"] = questionLookup;
                            }
    
                            if (_entity.Attributes.Contains("new_selectortemplateid"))
                            {
                                EntityReference newselectorTemplateLookup = SetReference("new_selectortemplate", postImageSelectorTemplateId);
                                questionTemplate["new_selectortemplateid"] = newselectorTemplateLookup;
                            }
    
                            service.Create(questionTemplate);
                        }


  • rthompson Profile Picture
    rthompson 1,530 on at
    RE: Plugin Error message - System.InvalidCastException: Specified cast is not valid.

    Did not work. Still getting same error message.

  • RaviKashyap Profile Picture
    RaviKashyap 55,410 Moderator on at
    RE: Plugin Error message - System.InvalidCastException: Specified cast is not valid.

    Hi,

    Can you check of you have any plugin registered on create of this entity? There doesn't seem anything wrond in this code.

  • gdas Profile Picture
    gdas 50,089 Moderator on at
    RE: Plugin Error message - System.InvalidCastException: Specified cast is not valid.

    Hi ,

    Could you please try with this - 

                            Guid selectortemplatequestionid = new Guid();
                            Guid selectortemplateid = new Guid();
    
                            EntityCollection result = service.RetrieveMultiple(query);
                            foreach (Entity _entity in result.Entities)
                            {
    
                                EntityReference questionlookup = (EntityReference)_entity.Attributes["new_selectortemplatequestionid"];
                                selectortemplatequestionid = (Guid)questionlookup.Id;
                                EntityReference templatelookup = (EntityReference)_entity.Attributes["new_selectortemplateid"];
                                selectortemplateid =(Guid)templatelookup.Id;
                                questionlookup.Id = new Guid();
                                EntityReference newselectorTemplateLookup = new EntityReference("new_selectortemplate", selectortemplateid);
                                _entity.Attributes.Add("new_selectortemplateid", newselectorTemplateLookup);
                                service.Create(_entity);
    
                            }

    Hope this helps.

  • rthompson Profile Picture
    rthompson 1,530 on at
    RE: Plugin Error message - System.InvalidCastException: Specified cast is not valid.

    Hi Temmy,

    I am not removing attributes that I don't need.  I am removing and adding back.

    I am trying to understand your logic.

    Would you have an example of using ExecuteMultiple.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,516 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,409 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans