Skip to main content
Post a question

Notifications

Announcements

No record found.

Customer experience | Sales, Customer Insights,...
Answered

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

Like (0) ShareShare
ReportReport
Posted on 19 Jan 2021 07:10:21 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,095 on 19 Jan 2021 at 12:10:02
    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

Congratulations 2024 Spotlight Stars!

Kudos to all of our 2024 community stars!

Meet the Top 10 leaders for December!

Congratulations to our December super stars!

Get Started Blogging in the Community

Hosted or syndicated blogging is available! ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,559 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,579 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans
Loading complete