Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Associate the related record

Posted on by 225

Hello,

1. I have a created the subgrid "Asset Register" in the customer entity which is having the customer lookup.

2.In the contract entity i have added the "Asset Register " subgrid based on the customer selected in contract related to that customer ,only that record should be Associate to the subgrid "Asset Register" plaese any body suggest me. below is my code

namespace Asset_Register
{
public class Class1 : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
//Guid entref = Guid.Empty;
ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

Entity entity = (Entity)context.InputParameters["Target"];
//var contract = service.Retrieve(entity.LogicalName, entity.Id, new ColumnSet(true));
/*QueryExpression query1 = new QueryExpression("contract");
query1.Criteria.AddCondition("contractid", ConditionOperator.Equal, entity.Id);
query1.ColumnSet = new ColumnSet(true);
EntityCollection result1 = service.RetrieveMultiple(query1);
foreach (var updateamount1 in result1.Entities)
{*/
EntityReference entre = entity.GetAttributeValue<EntityReference>("customerid");
Guid entref = entre.Id;

QueryExpression query = new QueryExpression("new_updateamount");
query.Criteria.AddCondition("new_customerlookup", ConditionOperator.Equal, entref);
query.ColumnSet = new ColumnSet(true);
EntityCollection result = service.RetrieveMultiple(query);
foreach (var updateamount in result.Entities)
{
AssociateRequest teamToProfile = new AssociateRequest
{
Target = new EntityReference(updateamount.LogicalName, updateamount.Id),
RelatedEntities = new EntityReferenceCollection
{
new EntityReference("account", entref)
},
Relationship = new Relationship("new_account_new_updateamount_Customerlookup")
};

// Execute the request.
service.Execute(teamToProfile);


//Entity en = (Entity)context.InputParameters["Target"];
/*AssociateRequest assreq = new AssociateRequest();
assreq.Target = new EntityReference(updateamount.LogicalName, updateamount.Id);
assreq.RelatedEntities = new EntityReferenceCollection();
assreq.RelatedEntities.Add(new EntityReference("account", entref));
assreq.Relationship = new Relationship("contract_customer_accounts");
AssociateResponse assresponse = (AssociateResponse)service.Execute(assreq);*/


}
}
}
}

*This post is locked for comments

  • Amrutha B K Profile Picture
    Amrutha B K 225 on at
    RE: Associate the related record

    In Assest register ,i have customerlook up and also in contract entity customer lookup is there based on the customer the related record should be assosiated tp the subgrid.

  • RaviKashyap Profile Picture
    RaviKashyap 55,410 on at
    RE: Associate the related record

    Hi,

    Do you have a Contract lookup in your Asset register as well or is the relationship N:N? Also the plugin cab be used to throw an error if the asset is not belong to the same customer.

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans