web
You’re offline. This is a read only version of the page.
close
Skip to main content
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,532

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

I have the same question (0)
  • Temmy Wahyu Raharjo Profile Picture
    2,914 on at
    RE: Plugin Error message - System.InvalidCastException: Specified cast is not valid.

    Hi! Instead of removing attribute you don't need. Why not just created new variable for storing attributes that you want to change?

    //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)
    {
    	var entityCreate = new Entity(retrieve.EntityLogicalName) { Id = Guid.NewGuid()};
    	entityCreate["new_selectortemplateid"] = new EntityReference("new_selectortemplate", selectortemplateId);
    	service.Create(retrieve);
    }


    For error casting. Make sure selectorTemplateId is Guid and that's should be okay. If the problem still exists, maybe the error is not from this code.

  • rthompson Profile Picture
    1,532 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.

  • gdas Profile Picture
    50,091 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.

  • RaviKashyap Profile Picture
    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.

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

    Did not work. Still getting same error message.

  • rthompson Profile Picture
    1,532 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
    1,532 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
    1,532 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
    1,532 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

  • Suggested answer
    RaviKashyap Profile Picture
    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?

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
HR-09070029-0 Profile Picture

HR-09070029-0 2

#2
ED-30091530-0 Profile Picture

ED-30091530-0 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans