Thursday, August 8, 2024

Misc

 ====kill all tracer sessions attached to the rule ==========

String RuleKey = tools.getParamValue("RuleKey");

((PegaAPI)tools).getSystemOperationsProvider().getListenerManagementAPI().terminateTraceForServiceInCluster(RuleKey);

============================================


Friday, July 12, 2024

Create multiple threads

 ParameterPage paramPage = new ParameterPage();

oLog.infoForced("thread:"+tools.getParamValue("thread"));

paramPage.put("ThreadName",tools.getParamValue("thread"));

paramPage.put("Location","pyActivity=DoUIAction&action=createNewWork&className=MyOrg-Lending-Work-Lending&flowName=pyStartCase");

pega_rules_utilities.callActivity(myStepPage,"RedirectAndRun",paramPage);

Monday, June 10, 2024

Read Email attachment

Param.attachmentName=pyAttachmentPage.pyAttachNames(1)

Param.attachmentValue=pyAttachmentPage.pyAttachValues(1)


 String baseDir=tools.getSystemSettings().getFSSetting("initialization/explicittempdir", "" , true, false);

String exportPath= tools.getProperty("pxProcess.pxServiceExportPath").getStringValue().replaceAll("file://web:","");

String AbsoluteFilePath=baseDir+exportPath+tools.getParamValue("FileNameUnique")+".zip";

String line="";

boolean success=true;

java.io.DataOutputStream dos = null;

String zipFilePath = tools.getParamValue("ZipFilePath");

int i=0;

//String zipFileFSPath=null;

try

{

  //Create and write contents to ZIP file

  PRFile zipFile = new PRFile(zipFilePath);

  //zipFileFSPath=zipFile.getAbsolutePath();


  String zipContents=tools.getParamValue("attachmentValue");//.replace("\n","").replaceAll("\\s", "");

  tools.getPrimaryPage().getProperty("pyDescription").setValue(zipContents);

  //byte[] decoded = java.util.Base64.getDecoder().decode(zipContents.getBytes(java.nio.charset.StandardCharsets.UTF_8));

  byte[] decoded = java.util.Base64.getMimeDecoder().decode(zipContents);

  java.io.ByteArrayInputStream bais = new java.io.ByteArrayInputStream(decoded);

  

  dos = new java.io.DataOutputStream(new PROutputStream(zipFile));

  dos.write(decoded);            

  dos.close();


  java.util.zip.ZipFile zippFile=new java.util.zip.ZipFile(AbsoluteFilePath);

  java.util.zip.ZipEntry zipEntry = zippFile.getEntry("Snapshot.json");

  java.io.InputStream inputStream = zippFile.getInputStream(zipEntry);

  java.util.Scanner scanner = new java.util.Scanner(inputStream);

  while (scanner.hasNextLine()) 

    line = line + scanner.nextLine();

  

  //java.util.Enumeration ent = zippFile.entries();

  //while(ent.hasMoreElements()) {

  //  i++;

  //}

  inputStream.close();

  zipFile.delete();

}catch(Exception e){

  

}

tools.getPrimaryPage().getProperty("pyTempText").setValue(i);

tools.getPrimaryPage().getProperty("pyLabel").setValue(AbsoluteFilePath);

tools.getPrimaryPage().getProperty("pyNote").setValue(line);

//Read the json file

Thursday, May 9, 2024

Pega JS functions

Pega close modal dialog of Tabel layout

 "pega.u.d.activeGrid.submitModal"

javascript:event

============================

Auto submit flowaction

--------------------

<pega:include name="AutoSubmit" type="Rule-Obj-HTML"/>

Below is the AutoSubmit HTML rule

<SCRIPT>

var doActionAutoSubmit = function(){

    doSubmit();

  pega.u.d.detachOnload(doActionAutoSubmit);  

}

  

<%

String strMessages = tools.getPrimaryPage().getMessagesAll();

if(strMessages.length() == 0)

{

tools.appendString("pega.u.d.attachOnload(doActionAutoSubmit, false);");

}

%>

</SCRIPT>



=====================================


<Script> function ABCRefresh() { var section = pega.u.d.getSectionByName('SectionName', ''); var theHandle = window.setInterval(function(){pega.u.d.reloadSection(section, 'ActivityName', '', false, false, '-1', false);},120000); } pega.u.d.attachOnload(ABCRefresh, false); </Script>


==============================================

<button onclick="myFunction()" type="button">Click me</button> <script> function myFunction() { var e = window.event; var options = { name: "TestDT", event: e }; pega.api.ui.actions.runDataTransform(options); } </script>




Saturday, May 4, 2024

REST connector exceptions

 "com.pega.pegarules.pub.services.ConnectorException"

"com.pega.pegarules.pub.services.ResourceUnavailableException"

Friday, April 26, 2024

Pega send email notificaiton

 





2.1

String strFileName = tools.getParamValue("OutputFilePath");

PRFile objFile = new PRFile(strFileName); //some error checking

if(objFile.exists())

  objFile.delete();


2.2

call pxGenerateExcelFile

Params - Param.FileName, WEBWB!APPRSVS!XLSX

2.3

String strFileName = tools.getParamValue("OutputFilePath");

PRFile objFile = new PRFile(strFileName); //some error checking

if (!objFile.exists())

{

         attach = false;

} if(!objFile.isFile())

{

         attach = false;

}

tools.putParamValue("OutputLocation",objFile.getName()); if (!objFile.canRead())

{

         attach = false;

    throw new PRRuntimeException("Can't continue with file upload. File \"" + strFileName + "\" is unreadable.");

} //read the file into a buffer.

java.io.DataInputStream dis = null;

byte buffer [] = null;

try

{

    // dis = new java.io.DataInputStream(new java.io.FileInputStream(objFile));    

    dis = new java.io.DataInputStream(new PRInputStream(objFile));

    buffer= new byte[dis.available()];                

    dis.readFully(buffer);

    dis.close();

}

catch (Exception e)

{

    attach = false;

         throw new PRRuntimeException("Can't continue with file upload. Can't read File \"" + strFileName + "\"");            

} //encode the file to Base64 so that we can store it on the database

strFileData = Base64Util.encodeToString(buffer);

if (strFileData == null)

{

         attach = false;

         throw new PRRuntimeException("Can't continue with file upload. Couldn't encode the file to Base64 so that we can store it on the database");            

}

2.5

Page-new att (Embed-EmailAttachment)

2.6

Property-set att

.pyDecode=true

.pyData=local.strFileData

.pyName=Param.FileName


2.7 atts (Data-EmailAttachments)

Page-copy 

from: att

to: atts.pyAttachments(<APPEND>)


Param.AttachmentPage=atts


===================================

java.io.ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream();

java.util.zip.ZipOutputStream logical_zip = null;

class ParamHelper {

  Boolean getBooleanValue(String param){

    String value = tools.getParameterPage().getString(param);

    if("true".equalsIgnoreCase(value)){

      return true;

    }else if("false".equalsIgnoreCase(value)){

      return false;

    }else{

      return null;

    }

  }

}

ParamHelper params = new ParamHelper();


try {

    // Create a ZIP archive output stream with no compression

    logical_zip = new java.util.zip.ZipOutputStream(baos);

} catch (Exception e) {

    throw new PRRuntimeException("Cannot initialize archive: " + e.getMessage());

}


ClipboardProperty cpResults = tools.findPage("pyWorkPage").getProperty("SendFiles");


try {

    for (Iterator instPgList = cpResults.iterator(); instPgList.hasNext();) {

        ClipboardProperty objOneRow = (ClipboardProperty) instPgList.next();

        ClipboardPage objOneRowPage = objOneRow.getPageValue();


        String strOutputPRFileName = objOneRowPage.getProperty("AttachStream").getStringValue();

        byte[] data = Base64Util.decodeToByteArray(strOutputPRFileName);

        String strOutputFileName = objOneRowPage.getProperty("AttachName").getStringValue();


        

     boolean isSubFolderExist = Boolean.parseBoolean(objOneRowPage.getProperty("IsSubFolderExist").getStringValue());

    


        // Determine the folder path based on the isSubFolderExist flag

        String folderPath;

        if (isSubFolderExist) {

            folderPath = "SubFolder/";

        } else {

            folderPath = "";

        }


        // Add each file to the appropriate folder in the ZIP archive

        String fileFolderPath = folderPath + strOutputFileName;

        try {

            logical_zip.putNextEntry(new java.util.zip.ZipEntry(fileFolderPath));

            logical_zip.write(data);

            logical_zip.closeEntry();

        } catch (java.io.IOException e) {

            throw new PRRuntimeException("Cannot add file to archive: " + e.getMessage());

        }

    }


    // Send the entire ZIP archive to the client

   

  String zipFileName = tools.getParameterPage().getString("ZipFileName");

  zipFileName = zipFileName + ".zip";

    try {

        logical_zip.close();

        baos.close();

        tools.sendFile(baos.toByteArray(), zipFileName, false, null, true);

    } catch (java.io.IOException e) {

        throw new PRRuntimeException("Error while sending: " + e.getMessage());

    }

} catch (Exception ex) {

    throw new PRRuntimeException("Error: " + ex.getMessage());

}

============================================================