function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
marys pindamarys pinda 

Function JS from Controller Apex

Hello
How can I use a JS function (Visualforce page) within the Controller Apex?

<apex:page controller="AAABCTest">
    <style type="text/css">
        .classeRequete { width: 600px; }
        .classeMail { width: 400px; }
    </style>
    <script type="text/javascript" src="https://raw.github.com/Stuk/jszip/master/jszip.js">
    function create_zip() {
    var zip = new JSZip();
    zip.add("{!JSENCODE(nomFichierCSV)}", "Zip\n");
    content = zip.generate();
   // location.href="data:application/zip;base64," + content;
     </script>
  <apex:form >
  <apex:PageBlock >
    :::::::::: INFOS :::::::::: <br /><br />
    Mail..........:&nbsp;<apex:inputText styleClass="classeMail" value="{!mailSouhaite}"/><br /><br />
    Request :&nbsp;&nbsp;<apex:inputText styleClass="classeRequete" value="{!caseTT}"/><br /><br />
    <apex:commandButton value="Valider" action="{!planifier}"/> <br/><br/>
  </apex:PageBlock>
  </apex:form>
</apex:page>

CONTROLLER:
global class AAABCTest implements System.Schedulable {
public String mailSouhaite {get; set;}
public string caseTT {get; set;}
public string nomFichierCSV {get; set;}
 
global void execute(SchedulableContext sc) {
planifier();
}

public void planifier(){
String query = caseTT;
String premier=query.substringAfter('select');    
premier=  premier.substringBefore('from');
   
string titre= premier+'\n';
string contenuCSV = titre;
  
for(Case a: (List<Case>)database.query(caseTT))
{
   string contenu = a.casenumber + ',' + a.subject+ ',' +a.Contact.Name+ ',' + a.status +'\n';
   contenuCSV = contenuCSV + contenu ;
}

Messaging.EmailFileAttachment csvPJ = new Messaging.EmailFileAttachment();
blob csvBlob = Blob.valueOf(contenuCSV);
nomFichierCSV = 'cases_fermes_'+Date.today().format()+'.csv';
csvPJ.setFileName(nomFichierCSV);
csvPJ.setBody(csvBlob);
Messaging.SingleEmailMessage email =new Messaging.SingleEmailMessage();
String[] adressMail = new list<string> {mailSouhaite};
String subject = 'Rapport Cases Fermés CSV - '+Date.today().format();
email.setSubject(subject);
email.setToAddresses(adressMail);
email.setPlainTextBody('mail...............');

//zip file before to send
//SOMETHING LIKE:::::
create_zip();                   
email.setFileAttachments(new Messaging.EmailFileAttachment[]{csvPJ});
Messaging.SendEmailResult [] envoyer = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});

}
}

Thank you,
Ramu_SFDCRamu_SFDC
Since javascript is a scripting language it needs to be used in HTML side in salesforce terms Visualforce side.
marys pindamarys pinda
Thank you,
But I believe there is a way to use the JS code within Visualforce page from the controller Apex ...
Not??
Ramu_SFDCRamu_SFDC
Can you please elaborate your question with an example on whether the java script code should be in Apex controller or Visualforceon ? This way either me or someone in this forum can provide you with the insights on whether it is possible or not.
marys pindamarys pinda
Thank you...

My problem:
I have a function JS within the Visualforce page that compress a file.
In Apex controller I want compress the file before sending it by email using the JS function within Visualforce page.


PAGE:
<apex:page controller="AAABCTest">
    <style type="text/css">
        .classeRequete { width: 600px; }
        .classeMail { width: 400px; }
    </style>
    <script type="text/javascript" src="https://raw.github.com/Stuk/jszip/master/jszip.js">
    function create_zip() {
    var zip = new JSZip();
    zip.add("{!JSENCODE(nomFichierCSV)}", "Zip\n");
    content = zip.generate();
   // location.href="data:application/zip;base64," + content;
     </script>

  <apex:form >
  <apex:PageBlock >
    :::::::::: INFOS :::::::::: <br /><br />
    Mail..........:&nbsp;<apex:inputText styleClass="classeMail" value="{!mailSouhaite}"/><br /><br />
    Request :&nbsp;&nbsp;<apex:inputText styleClass="classeRequete" value="{!caseTT}"/><br /><br />
    <apex:commandButton value="Valider" action="{!planifier}"/> <br/><br/>
  </apex:PageBlock>
  </apex:form>
</apex:page>

CONTROLLER:
.......
Messaging.EmailFileAttachment csvPJ = new Messaging.EmailFileAttachment();
blob csvBlob = Blob.valueOf(contenuCSV);
nomFichierCSV = 'cases_fermes_'+Date.today().format()+'.csv';
csvPJ.setFileName(nomFichierCSV);
csvPJ.setBody(csvBlob);
Messaging.SingleEmailMessage email =new Messaging.SingleEmailMessage();

String lignes = contenuCSV;
List<String> parts = lignes.split('\n');
integer lineNumber = parts.size();
   
String[] adressMail = new list<string> {mailSouhaite};
email.setToAddresses(adressMail);
String subject = 'Rapport Cases Fermés CSV - '+Date.today().format();
email.setSubject(subject); 

create_zip();
email.setPlainTextBody('Bonjour\n\nIl y a des Cases qui sont fermés le '+Date.today().format()
                        +'!\nCi-joint un rapport avec les informations des Cases fermés.\nLe fichier contient '
                        +(lineNumber-1) +' lignes avec les informations des cases fermés.\n\nCordialement, Vocaza'+'\n'+contenuCSV);                     
email.setFileAttachments(new Messaging.EmailFileAttachment[]{csvPJ});
Messaging.SendEmailResult [] envoyer = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
........