Hi ,
I am creating a entity ,name of new entity"ProductListing" and i have some fields on this entity like product name,product amount and account name which is lookup field and when i am open the opportunity,in which product line section, i am adding a product.
After that product name,product amount and account name fill in my new entity form on "ProductListing", i copy the product name and product amoount in ProductListing but i can't get accountID. How to fetch accountID?
But i have opporunityId and using plugins
My code is:
protected override void ExecuteCrmPlugin(LocalPluginContext serviceProvider)
{
if (serviceProvider == null)
{
throw new InvalidPluginExecutionException("serviceProvider");
}
// TODO: Implement your custom Plug-in business logic.
IPluginExecutionContext context = serviceProvider.PluginExecutionContext;
//Check weather the "Traget" exists in the inpu parameter
if (context.InputParameters.Contains("Target") == false) return;
//Check weather the Input Parameter is a entity or not
if (context.InputParameters["Target"] is Entity == false) return;
//Get service and service factory from service provider
IOrganizationService service = serviceProvider.OrganizationService;
//Extract the tracing service for use in debugging and to do tracing
ITracingService tracingService = serviceProvider.TracingService;
//Check weather the Target entity have the originating lead filed or not
Entity selectedEntity = (Entity)context.InputParameters["Target"];
//For insert record in product entity
Entity entProdList = new Entity("new_productlisting");
entProdList["new_name"] = selectedEntity.GetAttributeValue<string>("productdescription");
entProdList["new_description"] = "LG";
entProdList["new_accountName"]=?
service.Create(entProdList);
}
Thanks in advance!
*This post is locked for comments
Hi Shahbaaz,
Its working .
Thanks
Hi,
Did it resolved your issue??
if you still facing problem, please let me know.
Best Regards,
Shahbaaz
Please try below code.
protected override void ExecuteCrmPlugin(LocalPluginContext serviceProvider)
{
if (serviceProvider == null)
{
throw new InvalidPluginExecutionException("serviceProvider");
}
// TODO: Implement your custom Plug-in business logic.
IPluginExecutionContext context = serviceProvider.PluginExecutionContext;
//Check weather the "Traget" exists in the inpu parameter
if (context.InputParameters.Contains("Target") == false) return;
//Check weather the Input Parameter is a entity or not
if (context.InputParameters["Target"] is Entity == false) return;
//Get service and service factory from service provider
IOrganizationService service = serviceProvider.OrganizationService;
//Extract the tracing service for use in debugging and to do tracing
ITracingService tracingService = serviceProvider.TracingService;
//Check weather the Target entity have the originating lead filed or not
Entity selectedEntity = (Entity)context.InputParameters["Target"];
Guid opportunityid = new Guid()//pass guid of opportunity which you have and get the accountid
//QueryExpression to get Accountid
//get accountid
QueryExpression getAccountid = new QueryExpression()
{
EntityName = "opportunity",
ColumnSet = new ColumnSet("parentaccountid"),//confirm the account schema name on opportunity entity
Criteria =
{
Filters ={
new FilterExpression
{
Conditions =
{
new ConditionExpression("opportunityid",ConditionOperator.Equal,opportunityid),
}
},
}
}
};
EntityCollection getAccount = service.RetrieveMultiple(getAccountid);
if (getAccount.Entities.Count > 0)
{
Guid accoutid = ((EntityReference)getAccount.Entities[0].Attributes["parentaccountid"]).Id;
//For insert record in product entity
Entity entProdList = new Entity("new_productlisting");
entProdList["new_name"] = selectedEntity.GetAttributeValue<string>("productdescription");
entProdList["new_description"] = "LG";
entProdList["new_accountName"]=new EntityReference("account", accoutid);
service.Create(entProdList);
}
}
If it helps, Mark my answer as verified.
Best Regards,
Shahbaaz
Hi Shahbaaaz,
But how to get "guidofaccount" ?
Hi ,
You need to set like
entProdList["new_accountName"]=new EntityReference("account", guidofaccount);
Please follow below code :
protected override void ExecuteCrmPlugin(LocalPluginContext serviceProvider)
{
if (serviceProvider == null)
{
throw new InvalidPluginExecutionException("serviceProvider");
}
// TODO: Implement your custom Plug-in business logic.
IPluginExecutionContext context = serviceProvider.PluginExecutionContext;
//Check weather the "Traget" exists in the inpu parameter
if (context.InputParameters.Contains("Target") == false) return;
//Check weather the Input Parameter is a entity or not
if (context.InputParameters["Target"] is Entity == false) return;
//Get service and service factory from service provider
IOrganizationService service = serviceProvider.OrganizationService;
//Extract the tracing service for use in debugging and to do tracing
ITracingService tracingService = serviceProvider.TracingService;
//Check weather the Target entity have the originating lead filed or not
Entity selectedEntity = (Entity)context.InputParameters["Target"];
//For insert record in product entity
Entity entProdList = new Entity("new_productlisting");
entProdList["new_name"] = selectedEntity.GetAttributeValue<string>("productdescription");
entProdList["new_description"] = "LG";
entProdList["new_accountName"]=new EntityReference("account", guidofaccount);
service.Create(entProdList);
}
Mark my answer as verified if it helps
Best regards,
Shahbaaz
Stay up to date on forum activity by subscribing. You can also customize your in-app and email Notification settings across all subscriptions.
André Arnaud de Cal... 291,240 Super User 2024 Season 2
Martin Dráb 230,149 Most Valuable Professional
nmaenpaa 101,156