Dears

I created a custom workflow activity to run SSRS report and generate pdf file then attach it in Email .

when I run this On demand  workflow for one entity it is working fine and generate the ssrs report and attached fine.

but if I select multiple entities it is giving error as below

my code is here

public sealed class RenderQuoteReport : CodeActivity
{

[Input("E-Mail")]
[ReferenceTarget("email")]
public InArgument<EntityReference> Email { get; set; }

protected override void Execute(CodeActivityContext executionContext)
{
IWorkflowContext context = executionContext.GetExtension<IWorkflowContext>();
IOrganizationServiceFactory serviceFactory =executionContext.GetExtension<IOrganizationServiceFactory>();
IOrganizationService service = serviceFactory.CreateOrganizationService(null);

//Server
ReportGenerator rg = new ReportGenerator("http://servername/Reportserver/ReportExecution2005.asmx", new NetworkCredential("user", "password", "Org"));
Entity entity = RetrieveEntityById(service, "new_testinvoices", context.PrimaryEntityId);
ParameterValue[] parameters = new ParameterValue[1];
parameters[0] = new ParameterValue();
parameters[0].Name = "ReportParameter1";
String strInvoiceName = string.Empty;

if (entity.Attributes.Contains("new_name"))
{
strInvoiceName = entity.Attributes["new_name"].ToString();
parameters[0].Value = strInvoiceName;
}

byte[] reportresult = rg.Render("/OrgCRM_MSCRM/Invoice", FormatType.PDF, parameters);

if (reportresult != null)
{
Entity attachment = new Entity("activitymimeattachment");
attachment["objectid"] = Email.Get<EntityReference>(executionContext);
attachment["objecttypecode"] = "email";
attachment["filename"] = "GInvoice.pdf";
attachment["subject"] = "Accounts List";
attachment["body"] = System.Convert.ToBase64String(reportresult);
try
{
service.Create(attachment);
}
catch (Exception ex)
{
throw new Exception(string.Format("Error creating attachment - {0}", ex.Message));
}
try
{
service.Execute(new SendEmailRequest()
{
EmailId = Email.Get<EntityReference>(executionContext).Id,
IssueSend = true,
TrackingToken = string.Empty
});
}
catch (Exception ex)
{
throw new Exception(string.Format("Error sending email - {0}", ex.Message));
}
}



}


private Entity RetrieveEntityById(IOrganizationService service, string strEntityLogicalName, Guid guidEntityId)
{

Entity RetrievedEntityById = service.Retrieve(strEntityLogicalName, guidEntityId, new ColumnSet(true));

return RetrievedEntityById;

}




internal class ReportGenerator
{
#region Privates

ReportExecutionService _reportexecutionservice = null;

#endregion Privates

#region CTOR

internal ReportGenerator(string ServiceUrl, ICredentials credentials)
{
if (string.IsNullOrEmpty(ServiceUrl))
throw new Exception("Parameter ServiceUrl has to contain value");

if (credentials == null)
throw new Exception("Parameter Credentials has to contain value");

_reportexecutionservice = new ReportExecutionService()
{
Credentials = credentials,
Url = ServiceUrl
};
}

#endregion CTOR

#region Methods

internal byte[] Render(string Report, FormatType formattype)
{
return this.Render(Report, formattype, new ParameterValue[] { });
}

internal byte[] Render(string Report, FormatType formattype, ParameterValue[] parameters)
{
byte[] result = null;
string format = GetFormatType(formattype);
string historyID = null;
string devInfo = @"<DeviceInfo><Toolbar>False</Toolbar></DeviceInfo>";
string encoding;
string mimeType;
string extension;
Warning[] warnings = null;
string[] streamIDs = null;

try
{

ExecutionInfo execInfo = new ExecutionInfo();
ExecutionHeader execHeader = new ExecutionHeader();
_reportexecutionservice.ExecutionHeaderValue = execHeader;
execInfo = _reportexecutionservice.LoadReport(Report, historyID);
_reportexecutionservice.SetExecutionParameters(parameters, "en-us");
result = _reportexecutionservice.Render(format, devInfo, out extension,out mimeType, out encoding, out warnings, out streamIDs);

}
catch (Exception ex)
{
if (ex is SoapException)
{
SoapException sexc = ex as SoapException;
throw new Exception(string.Format("Error generating report - {0}", sexc.Detail.InnerText));
}
else
{
throw new Exception(string.Format("Error generating report - {0}", ex.Message));
}
}
_reportexecutionservice.Dispose();
return result;
}

private string GetFormatType(FormatType formattype)
{
switch (formattype)
{
case FormatType.XML:
case FormatType.CSV:
case FormatType.IMAGE:
case FormatType.PDF:
case FormatType.MHTML:
case FormatType.EXCEL:
case FormatType.Word:
return formattype.ToString();
case FormatType.HTML40:
return "HTML4.0";
case FormatType.HTML32:
return "HTML3.2";
default:
throw new Exception(string.Format("Rendering type {0} is not available", formattype));
}
}

#endregion Methods
}

internal enum FormatType
{
XML, CSV, IMAGE, PDF, HTML40, HTML32, MHTML, EXCEL, Word
}