I would really appreciate you help if you can help with this
using System;
using System.ServiceModel;
using System.Activities;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Workflow;
using System.Collections.Generic;
using Microsoft.Crm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Sdk.Query;
using PLContext;
using System.Linq;
// www.youtube.com/watch
namespace CopyOpportunityLines
{
/// </summary>
public class CopyOpportunityLines : CodeActivity
{
//private object _serviceProxy;
#region Properties
//Property for Target Entity
// xrmtoolkit.com/.../PluginWizard
// Required for Entity Agreement
[RequiredArgument]
[Input("Agreement")]
[ReferenceTarget(msdyn_agreement.EntityLogicalName)]
public InArgument<EntityReference> TargetAgreement { get; set; }
//Property for Entity salesorder
[RequiredArgument]
[Input("Opportunity")]
[ReferenceTarget(Opportunity.EntityLogicalName)]
public InArgument<EntityReference> GetOpportunity { get; set; }
#endregion
/*
#region Output Properties
//Property for Entity salesorder detail
[Output("Contract Detail")]
[ReferenceTarget("salesorderdetail")]
public OutArgument<EntityReference> ContractDetail { get; set; }
#endregion
*/
/// <summary>
/// Executes the WorkFlow.
/// </summary>
/// <param name="crmWorkflowContext">The <see cref="LocalWorkflowContext"/> which contains the
/// <param name="executionContext" > <see cref="CodeActivityContext"/>
/// </param>
/// <remarks>
/// For improved performance, Microsoft Dynamics 365 caches WorkFlow instances.
/// The WorkFlow's Execute method should be written to be stateless as the constructor
/// is not called for every invocation of the WorkFlow. Also, multiple system threads
/// could execute the WorkFlow at the same time. All per invocation state information
/// is stored in the context. This means that you should not use global variables in WorkFlows.
/// </remarks>
protected override void Execute(CodeActivityContext context)
{
if (context == null)
{
throw new ArgumentNullException("context");
}
//IOrganizationService for communication with CRM
try
{
// TODO: Implement your custom activity handling.
//Retrieve the CrmService so that we can retrieve the loan application
//IWorkflowContext workflowContext = context.GetExtension<IWorkflowContext>();
IWorkflowContext workflowContext = context.GetExtension<IWorkflowContext>();
IOrganizationServiceFactory serviceFactory = context.GetExtension<IOrganizationServiceFactory>();
IOrganizationService service = serviceFactory.CreateOrganizationService(workflowContext.UserId);
ITracingService tracingService = context.GetExtension<ITracingService>();
// Retrieve Agreement Entity Reference
tracingService.Trace("Retrieved Org Context, Service and Service Factory");
// Get TargetEntity ID
Guid AgreementId = this.TargetAgreement.Get(context).Id;
//Get Opportunity ID
Guid OpportunityID = this.GetOpportunity.Get(context).Id;
tracingService.Trace("Retrieve OpportunityID" + OpportunityID);
string fetchQuery = @"<fetch distinct='false' no-lock='false' mapping='logical'>
< entity name = 'opportunityproduct' >
< attribute name = 'opportunityproductid' />
< attribute name = 'fc_agreement' />
< attribute name = 'opportunityproductname' />
< order descending = 'false' attribute = 'fc_agreement' />
< filter type = 'and' >
< condition attribute = 'opportunityid' value='{zzz}' operator= 'eq' />
</ filter >
</ entity >
</ fetch >";
tracingService.Trace("Retrieved FetchXML Query");
fetchQuery = fetchQuery.Replace("zzz", OpportunityID.ToString());
tracingService.Trace("Retrieved Fetch Query Trace" + fetchQuery);
EntityCollection results = service.RetrieveMultiple(new FetchExpression(fetchQuery));
tracingService.Trace("Results counted" + results.TotalRecordCount);
// Run the query with the FetchXML
try
{
if (results.Entities.Count > 0)
foreach (var opprtunitlines in results.Entities)
{
tracingService.Trace("For each loop);
}
}
catch (FaultException ex)
{
throw new InvalidPluginExecutionException("An error occurred in the plug-in - Results." + results, ex);
}
}
catch (FaultException ex)
{
throw new InvalidPluginExecutionException("An error occurred in the plug-in.", ex);
}
}
}
}