CA Service Management

 View Only
Expand all | Collapse all

spel - save text to file

  • 1.  spel - save text to file

    Posted Mar 22, 2019 02:21 PM

    Dear friends,

    is possible to save text data to a file using spel? I know how to use expand function for reading from a file, but can't remember how to save to a file..


    Thank you,


  • 2.  Re: spel - save text to file

    Posted Mar 23, 2019 12:49 AM



    This can be easily done with perl (which can be called as regular spel method). here is an example how to register perl method in spel daemon: SPEL How to Execute Remote Reference via Perl code. Don't forget to add your params to perl_function();


    There is a lot of examples how to handle files with perl but personally I prefer to use IO::File - 


    Hope this helps.

  • 3.  Re: spel - save text to file
    Best Answer

    Posted Mar 23, 2019 07:48 AM

    Be careful, or at least aware of the following:
    As far as I know, the perl interpreter is not persistent in memory, but will be instantiated with each perl method you are executing.
    This is for sure not critical if this happens once in a while, but might  have impact, when part of a constantly often reoccurring logic.
    As always, it always depends on the business requirement, which is not clear here. Why and what needs to be written into which file.
    Btw. A simple exec in spell might help as well.

  • 4.  Re: spel - save text to file

    Posted Mar 23, 2019 03:03 PM

    Michael, cdtj, thank you for your responses.


    The original case is to create XML files for PAM file trigger. It's for some integration where using "Execute CA IT PAM Action" macros doesn't fully meet my requirements.


    So far I always used the "Execute Remote Reference" macros for executing .bat file which creates the corresponding XML files. And I simply thought using only the spel for XML creation would be easier and faster...


    Anyway, I'm quite surprised that spel doesn't support this.


    Have a great weekend,


  • 5.  Re: spel - save text to file

    Posted Mar 25, 2019 06:07 AM

    Finally I simply used exec for running batch...




  • 6.  Re: spel - save text to file

    Posted Mar 25, 2019 09:59 AM

    Hi Jakub.


    Would you mind, to elaborate your integration requirements a bit more?
    What exactly are you doing?
    What is triggering your integration.
    What data is needed for your integration?
    Having some more details, one might be able to come up with additional options.
    Best regards

  • 7.  Re: spel - save text to file

    Posted Mar 26, 2019 02:52 AM

    Hi Michael,

    it's a full integration between SDM and Jira (ticket creation, comments, status updates, transfers, ...). Some of the data we need to move to Jira are on cr object, but some are based on the activity which triggers the interaction (f.e. description and some custom attributes on alg object). In this case, I found quite complicated to use "Execute CA IT PAM Action" macros which AFAIK can only use persid of the cr object as a parameter.


    So I decided to use PAM file trigger and tried to find the best option for creating the XML files. Now I use a simple batch which creates an XML file with cr persid and alg persid as the parameters for PAM process. The batch is run via spel exec.


    It works fine even if I try to trigger many activities at once, but I'm definitely open to other ideas.


    Thank you,


  • 8.  Re: spel - save text to file

    Posted Mar 27, 2019 10:07 AM

    Hi Jakub.


    Thank you very much for these insights.

    So, as far as I understand, you are triggering some kind of integration based on activity logs.

    Therefore you need the triggering activity log available in your PAM process.

    Unfortunately, the macros available for PAM Integration integration only provide the persistent_id of the corresponding cr object and not the alg persistent_id when starting PAM processes.

    My first idea was, to write the alg persid of the latest alg into the cr_object, so it would be available in the PAM process.

    But what would happen, if more than one alg gets written in the same timeframe, let say a field update and a transfer.


    What is about the following approach:

    As you are already writing you own spel code, you might be able to start a PAM process with the alg persid as a start parameter instead of the cr persid. I have no idea, what consequences this might have with the rest of the PAM integration, like displaying PAM tasks and other integration points.

    This would skip the necessity to write a file, and to start any external script.

    For sure this would be a non supported coding. It's just a send_wait of a specific method passing appropriate arguments. Doing the same as before but passing the alg persid instead of the cr persid

    Would that help ? And do you want to give it a try?




  • 9.  Re: spel - save text to file

    Posted Mar 27, 2019 04:51 PM

    Hi Michael,

    thank you for your suggestions.


    I had exactly the same idea about writing the last activity into the cr object and used it in the PAM process. It worked fine until I had to include attchments handling. As you can attach many attachments "at once", the activities are then created to fast to be processed one by one - for example the PAM process for the first attachment didn't have access to the first activity, because it was already overwritten by some later...


    The second idea sounds interesting, but I have no clue how to trigger PAM Macro in spel code. So far I always used OOB functionality - Activity Notifications and Events with "Execute CA IT PAM Action" Macros.




  • 10.  Re: spel - save text to file

    Posted Mar 28, 2019 08:21 AM

    Hi Jakub.

    here is the example you might want to play with. I dont have a PA test System available, so I wasnt able to do any pre tests.

    send_wait(0, top_object(), "call_attr", "chg", "start_itpam_workflow", (string)path, (string)name, (string)persid, (object)ticket_obj, (uuid)creator, (string)some_text);


    path - seems to be related to the PA Hierarchie of start foms- corresponding to chgcat.caextwf_start_id.caextwf_path

    name - seems to be a process Name or start form Name - corresponding to chgcat.caextwf_start_id.caextwf_form

    persid - the persid - the alg persid in your case

    ticket_obj - an object referencec of the actual ticket, I suggest to pass in the corresponding chg object

    creator - the uuid of the contact which is doing this Action

    some_text - the text which gets wriite to an activity log


    Let us know, how you proceed.



  • 11.  RE: Re: spel - save text to file

    Posted Apr 26, 2021 09:11 AM
    Hi Michael,
    ... and another project two years later ... :)

    Below is the working spel example that triggers a PAM process on a request log comment. I found that even if I trigger a PAM process on a request action I still need to call a chg method start_itpam_workflow (I haven't found a corresponding method for the cr object).

    OBJECT alg {
    POST_CI z_run_pam(persistent_id) 400001 FILTER(EVENT("INSERT") && (type == "LOG"));

    alg::z_run_pam(...) {
    logf(SIGNIFICANT, "SPEL STARTED: alg::z_run_pam for alg %s", argv[3]);

    string process_path, process_name, text;
    object alg_dob, cr_dob;
    uuid who;

    //get the current user uuid
    send_wait(0,top_object(), "call_attr", "cnt", "current_user_id");

    //get the alg object
    send_wait(0, top_object(), "call_attr", "alg", "dob_by_persid", 0, argv[3], NULL, NULL);
    alg_dob = msg[0];

    //get the cr object
    send_wait(0, top_object(), "call_attr", "cr", "dob_by_persid", 0, alg_dob.call_req_id, NULL, NULL);
    cr_dob = msg[0];

    logf(SIGNIFICANT, " ... alg::z_run_pam - corresponding request found: %s", cr_dob.ref_num);

    process_path = "/TMP/Test/";
    process_name = "simple_test";
    text = "something clever";

    send_wait(0, top_object(), "call_attr", "chg", "start_itpam_workflow", process_path, process_name, alg_dob.persistent_id, cr_dob, who, text);

    logf(SIGNIFICANT, "SPEL ENDED: alg::z_run_pam for alg %s", argv[3]);

  • 12.  RE: Re: spel - save text to file

    Posted Apr 27, 2021 01:41 AM
    Hey Jakub.
    Very nice, thanks for letting us know :)
    As so often, once you figured out a certain solution, you can adopt it to other similar challenges as well.
    In fact, this is the result and profit of experience !

    Regarding your concern about the chg method.
    In this case , you are calling a  so called factory method, which is declared within a METHODS block within the chg FACTORY block in majic.
    These kind of methods can be understood as global available methods, as you are specifying the "Full path" when calling them:
    send_wait(0, top_object(), "call_attr" , "chg", "start_itpam_workflow", ... );
    Such a method could be declared in any factory in principal.
    When calling/executing, you are specifying the concrete factory in which it is actually declared/available.

    A second kind of methods are object methods. These are declared within a METHODS block within the OBJECT block in majic
    Those can only be called for a specific instance of an OBJECT 
    In fact, in the example above, the "call_attr" method is an instance method of the TOP object.

    Again, thanks for sharing.

    Best Regards

    Principal Services Consultant
    HCL Enterprise Studio

  • 13.  Re: spel - save text to file

    Posted Mar 25, 2019 06:33 AM

    There is another way and maybe someone will found it useful in his case, so I left the instruction in this thread:

    - create new Notification method, specify interpreter on your own descretion (there could be batch file, perl script or any other script that can get data as input arguments);

    - create new Contact and specify newly created Notify method;

    - configure activity and trigger notification for specified contact.


    PS: Checking "Write to file" checkbox on notify method form will result a file creation in temp (windows/temp for NT) folder where you can check all passed variables.



    notification method:



    # $NX_ROOT/bin/

    use IO::File;

    # define path for files queue
    my $path = "../myFolder/queue";
    my $syf = 0;
    # filename would be current timestamp
    my $nixtime = time;
    my $FILE_NAME;
    my $fh;
    # folder name would be passed from SDM
    my $mailprocid = $ENV{"NX_NTF_ACK_BY"};
    my $dir = $path . "/" . $mailprocid;

    unless(-e $dir or mkdir $dir) {
         die "Unable to create $dir\n";

    # trying to create file with unique name
    while ( (!defined($fh)) && ($syf < 1000) ) {
         $FILE_NAME = $dir . "/" . $nixtime . "." . $syf;
         $fh = IO::File->new($FILE_NAME, O_RDWR|O_CREAT|O_EXCL);

    # fetching msg and email and writing them to the file
    my $msg = $ENV{"NX_NTF_MESSAGE"};
    my $email = $ENV{"NX_NTF_EMAIL_ADDRESS"};

    print $fh "NX_NTF_EMAIL_ADDRESS=$email\n";
    print $fh "NX_NTF_MESSAGE=$msg\n";

    # here we done
    close $fh;