Help get this topic noticed by sharing it on Twitter, Facebook, or email.

Triggering a COT Report and a Custom Busines Rule

Hi

Does anyone have a sugestion as to how we can trigger a Custome c# Business Rul e and then a COT Report from a single UI Action

Thanks

Finbar
1 person has
this question
+1
Reply
  • You can create and open a report from a custom c# rule:

    COT lets you create a report onto the server using a controller.
    You can find their examples here:

    https://codeontime.com/print/learn/re...

    I have a custom c# rule that creates a report onto the server and then opens it.

    Here's an example of my code, I've removed my vars and replaced then with "Your Controller" etc.

    ReportArgs args = new ReportArgs();
    args.Controller = "Your Controller";
    args.TemplateName = "Your Controller_grid1.rdlc";
    args.SortExpression = "Your Sort Expression";

    args.Format = "Pdf";

    // args.Format = "Excel";
    // args.Format = "Word";

    // Example of Filter if required

    args.Filter = new FieldFilter[] {
    new FieldFilter {
    FieldName = "Your Field Name",
    Operation = RowFilterOperation.Equal,
    Value = Your Field Value
    },
    new FieldFilter {
    FieldName = "Your 2nd Field Name",
    Operation = RowFilterOperation.Equal,
    Value = Your 2nd Field Value
    }

    };

    try
    {
    byte[] reportData = Report.Execute(args);
    File.WriteAllBytes("Your Report File (Path and name)", reportData);

    }
    catch (Exception e)
    {
    Result.ShowAlert(e.ToString());
    }

    Result.NavigateUrl = "Your Report File (Path and name)";

    I do it this way as I need to leave the created reports on the server so that they can be opened for future use.

    Hope this helps although no doubt there is a better way!
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. indifferent, undecided, unconcerned sad, anxious, confused, frustrated kidding, amused, unsure, silly happy, confident, thankful, excited

  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. indifferent, undecided, unconcerned sad, anxious, confused, frustrated kidding, amused, unsure, silly happy, confident, thankful, excited

  • Hi

    Thank you the above worked find for what we need, second question habe you found a way to utilises the CSV Download functionality from a Business Rule ?

    Thanks

    Fin
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. indifferent, undecided, unconcerned sad, anxious, confused, frustrated kidding, amused, unsure, silly happy, confident, thankful, excited

  • I haven't found a way yet but I haven't yet had a good look at the COT export functions.

    I did need to create csv reports on one of my apps but I wrote the code in a business rule using a data loader and getting the data direct from the relevant tables rather than from a controller.

    I'll have a look at the COT rules when I can and see if there is a way.

    Someone else might have already done it of course.

    Sorry I can't be of more help at the moment. If I do find a way of using COT rather than having to write your own code I'll let you know.

    Peter
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. indifferent, undecided, unconcerned sad, anxious, confused, frustrated kidding, amused, unsure, silly happy, confident, thankful, excited

  • Pete

    No problem I have sorted it, I have question asking this of COT a couple of years back so I will update that but basically a rule like below will do it


    [Rule("r100")]
    public void r100Implementation(po_sap_interfaceModel instance)
    {

    var context = Context;
    ActionArgs args = new ActionArgs();
    args = Arguments;
    args.CommandName = "ExportCsv";
    args.CommandArgument = null;

    var controller = ControllerFactory.CreateDataController();
    var result = controller.Execute(args.Controller, args.View, args);

    /* Result contains a file with the csv so just put that somewher the user can get to and rename it as a .csv and get JS on the browser to open a url to that location
    */
    var fileName = ((string)(result.Values[0].Value));

    var path = HttpContext.Current.Server.MapPath("/downloadedFiles");
    Uri url = HttpContext.Current.Request.Url;
    var host = url.GetLeftPart(UriPartial.Authority);
    var fileParts = fileName.Split('\');
    var newFileName = fileParts[fileParts.Length - 1].Replace(".tmp", ".csv");

    File.Copy(fileName, path + "/" + newFileName);
    File.Delete(fileName);
    Result.ExecuteOnClient("window.open('" + host+ "/downloadedFiles/"+newFileName+ "','_blank');");
    Result.Continue();
    }
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. indifferent, undecided, unconcerned sad, anxious, confused, frustrated kidding, amused, unsure, silly happy, confident, thankful, excited

  • Opps missed a bit I stole form export.cs


    protected virtual string GenerateOutputFileName(ActionArgs args, string outputFileName)
    {
    args.CommandArgument = args.CommandName;
    args.CommandName = "FileName";
    var values = new List<FieldValue>();
    values.Add(new FieldValue("FileName", outputFileName));
    args.Values = values.ToArray();
    var result = ControllerFactory.CreateDataController().Execute(args.Controller, args.View, args);
    foreach (var v in result.Values)
    if (v.Name == "FileName")
    {
    outputFileName = Convert.ToString(v.Value);
    break;
    }
    return outputFileName;
    }

  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. indifferent, undecided, unconcerned sad, anxious, confused, frustrated kidding, amused, unsure, silly happy, confident, thankful, excited

  • Well done Fin, saves me a lot of time as well.

    Cheers

    Peter
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. indifferent, undecided, unconcerned sad, anxious, confused, frustrated kidding, amused, unsure, silly happy, confident, thankful, excited