Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM forum
Answered

Add User record in Entity Subgrid using c# in CRM 9.0

Posted on by 2,665

Hi All,

I have one entity named Interview & one subgrid with user available & am retrieving all interview records & trying to add particular user in that subgrid in Interview entity using console.Here subgrid is 1:n relationship means interview to user. I am using Associate Request message but its only updating last record only from collection.

Is it correct way to use Associate or do i need to use Update Message to associate user in subgrid if Update requires then what will be the syntax for this ?

QueryExpression EnInterview = new QueryExpression();
            EnInterview.EntityName = "mdc_interviews";
            EnInterview.ColumnSet = new ColumnSet(true);
            EnInterview.Criteria = new FilterExpression();
            EnInterview.Criteria.AddCondition("mdc_ismigrated"ConditionOperator.Equal, 755040000);
            EntityCollection InterviewColls = Service.RetrieveMultiple(EnInterview);
            AssociateRequest request1 = new AssociateRequest();
            try
            {          
                foreach (var enitem in InterviewColls.Entities)
                {
                    QueryExpression userQuery = new QueryExpression("systemuser");
                    userQuery.Criteria.AddCondition("fullname"ConditionOperator.Equal, "Shahzad Khan");
                    EntityCollection col = Service.RetrieveMultiple(userQuery);
                    if (col.Entities.Count > 0)
                    {
                        request1.Target = new EntityReference(enitem.LogicalName, enitem.Id);
                        foreach (Entity Eninci in col.Entities)
                        {                       
                            request1.RelatedEntities = new EntityReferenceCollection();
                            request1.RelatedEntities.Add(new EntityReference("systemuser", Eninci.Id));
                            request1.Relationship = new Relationship("mdc_mdc_interviews_systemuser");
                        }
                    }
                    if (request1 != null)
                        Service.Execute(request1);
                }           
            }
            catch (Exception ex)
            {
                throw ex;
            }

Any suggestions?

Thanks,

Jharana

  • Verified answer
    Mahendar Pal Profile Picture
    Mahendar Pal 45,041 on at
    RE: Add User record in Entity Subgrid using c# in CRM 9.0

    Hi,

    No need to use associate, you can simple use update request, get records which you want to show in subgrid, update their lookup field (your parent entity) so that they can be shown in the subgrid.

    Let me know if you need more details.

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

Anton Venter – Community Spotlight

Kudos to our October Community Star of the month!

Announcing Our 2024 Season 2 Super Users!

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

Dynamics 365 Community Newsletter - September 2024

Check out the latest community news

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,564 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 228,651 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans