You need to sign in to do that
Don't have an account?
marys pinda
use input text value into SOQL
I have a Visualforce page that takes a request and then sends its result by email.
At present, the request is fixed, but I want to use any request.
So goes the question: how do this part to be dynamic:
for(Case a: (List<Case>)database.query(caseTT))
{
string contenu = a.casenumber + ',' + a.subject+ ',' +a.Contact.Name+ ',' + a.status +'\n';
contenuCSV = contenuCSV + contenu ;
}
Thank you!
The codes:
At present, the request is fixed, but I want to use any request.
So goes the question: how do this part to be dynamic:
for(Case a: (List<Case>)database.query(caseTT))
{
string contenu = a.casenumber + ',' + a.subject+ ',' +a.Contact.Name+ ',' + a.status +'\n';
contenuCSV = contenuCSV + contenu ;
}
Thank you!
The codes:
PAGE VISUALFORCE:::: <apex:page controller="AAAACSV"> <style type="text/css"> .classeRequete { width: 600px; } .classeMail { width: 400px; } </style> <apex:form > <apex:PageBlock > Request : <apex:inputText styleClass="classeRequete" value="{!caseTT}"/><br /><br /> <apex:commandButton value="Valider" action="{!planifier}"/> <br/><br/> <apex:commandButton value="Effacer"/> <br/><br/> </apex:PageBlock> </apex:form> </apex:page> CLASS APEX:::: global class AAAACSV implements System.Schedulable { public string caseTT {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 ; } String lignes = contenuCSV; List<String> parts = lignes.split('\n'); integer lineNumber = parts.size(); Messaging.EmailFileAttachment csvPJ = new Messaging.EmailFileAttachment(); blob csvBlob = Blob.valueOf(contenuCSV); nomFichierCSV = 'Closed_'+Date.today().format()+'.csv'; csvPJ.setFileName(nomFichierCSV); csvPJ.setBody(csvBlob); Messaging.SingleEmailMessage email =new Messaging.SingleEmailMessage(); String[] adressMail = new list<string> {'mail@mail.com'}; String subject = 'Rreports'; email.setSubject(subject); email.setToAddresses(adressMail); email.setPlainTextBody('Mail....); email.setFileAttachments(new Messaging.EmailFileAttachment[]{csvPJ}); Messaging.SendEmailResult [] envoyer = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email}); } }
Thank you for your help!
This part is static:
for(Case a: (List<Case>)database.query(caseTT))
{
string contenu = a.casenumber + ',' + a.subject+ ',' +a.Contact.Name+ ',' + a.status +'\n';
contenuCSV = contenuCSV + contenu ;
}
It works only with Cases requests!
Have a nice day!
Do I understand correctly ? Do you want this query to work with any kind of sObjects?
If so, You've just change
Case a: (List<Case>)database.query code part to sObject a : Database.query (...)
Later you can check for exact collection type using getSObjectType() method and to whatever you want depends on type
The User Shashank_SFDC helped me make the dynamic code, but now this code sends me the results in a CSV out of order. how can I solve this?
THE CODE:
REQUEST: select casenumber,subject,Contact.Name,status from Case where ClosedDate=TODAY
RESULTAT INTO CSV:
casenumber,subject,Contact.Name,status
Closed,case02,0032000001D8WWsAAN,5002000000arWuwAAE
Closed,case01,0032000001D8WWsAAN,5002000000arWuOAAU
The code generates the results to the contrary and the author name and ID case are encoded .
Thank you,
List<String> scv = new List<String>();
for(sObject a: queryResult)
{
scv.add (a.get ('CaseNumber)' + ', '
+ a.get ('Subject') + ', '
+ a.get ('Contact').get ('Name) + ', '
+ a.get ('Status')
+ '\n'
);
}
After that, you will have array of strings where each strings are SCV-formatted one, ended with \n.
But any requests:
select Name from Contact order by Name asc
select Name, Email from Contact ....
Thank you
Since you have dynamically generated query, you cac store requested field in a collection.
Now you set a query as a whole string and pass it via setter. It's not so flexible and convinient.
The better option is to pass data via constructor and
1. Pass fields as String[]
2. Pass object from where to select as sObjectType, to minimize error.
Using this way you could have something like this
global class AAABCTest implements System.Schedulable {
private final String[] field;
private final String sObjectName;
public string caseTT {get; set;}
public final String query;
global void execute(SchedulableContext sc) {
.....
}
/**
* fields - list of fields. Like - new List<String> {'Subject', 'Name', 'whatever'}
* type - sOBjectType. For Case it would be Case.sObjectType
* tail - is the 'where' clause. Like 'where CloseDate = Today()'
*/
public AAABCTest (List<String> fields, SObjectType type, String tail) {
query = 'select ';
Boolean isFirst = true;
for (String field: fields) {
query += isFirst ? '' : ', ' + field;
isFirst = false;
}
query += ' from ' + type.getDescribe().getName() + ' ' + tail;
}
// return result in scv format
private List<String> getSCVArray () {
List<String> scv = new List<String>();
for(sObject a: queryResult) {
String scvItem;
Boolean isFirst = true;
for (String field: fields) {
Object objValue =
!field.contains('.')
? a.get (field)
: a.get (field.substring(0, field.indexOf ('.') - 1)) +
a.get (field.substring(field.indexOf ('.') + 1);
scvItem += isFirst ? '' : ', ' + String.valueOf (objValue);
isFirst = false;
}
scv.add (scvItem + '\n');
}
return scv;
}
}
But my first code works with many problems to order the fields...
Order of fields in CVS will be absolutely the same as in the query - in the both cases fields will appear in the order they inserted to String<List> fields.
So, if you'd add fields in list like this,
List <String> fields = new List<String> ('Name', 'Status', 'SomeField1', 'SomeField2') then
query will be generated with the same order, namely - 'select Name, Status, SomeField1, SomeField2....'
and cvs will have the fields appear the same way - name, status, somefield1, somefield2
Have a nice day my friends!