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 

Split String

Is it possible to pick values ​​between SELECT and FROM (String) and use the code in Apex?

Example: the user informe = SELECT casenumber,subject,Contact.Name,status FROM Case WHERE ClosedDate=TODAY
The Apex code uses only: casenumber, subject, Contact.Name, status to build a CSV::::

...
string titre= 'casenumber,subject,Contact.Name,status'+'\n';

...

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!!!
Gonzalo AbrunaGonzalo Abruna
Hello Veronica,


maybe you can use the get() method for Case records. Something like:

List<Case> listCases = database.query(caseTT);

String[] listFields = calculateListFields(caseTT); //calculateListFields would be a method returning String[], where each element will be a field API name: 
                                                                            //casenumber, subject, contact.name....
String contenuCSV = '';
for(Integer i = 0; i<listCases.size(); i++){
     String csvRow = null;
     for(String s : listFields){ //Build a row of the csv file
        if(csvRow == null){
             csvRow = listCases[i].get(s);
        }
        else{
             csvRow += ',' + listCases[i].get(s);
        }
     }
     csvRow += '\n';
     contenuCSV += csvRow;
}

Best regards,

Gonzalo.
praveen murugesanpraveen murugesan
Hi veronica,

You can split by using 

substringAfter and substringBefore methods.

eg:
string query = 'SELECT casenumber,subject,Contact.Name,status FROM Case WHERE ClosedDate=TODAY';
string First =query.substringAfter('SELECT');                // SELECT is caps because its case- sensitive
First =  First.substringBefore('WHERE');                    // WHEREis caps because its case- sensitive
system.debug('~~~~~~~~'+First);


https://developer.salesforce.com/releases/release/Winter13/New+String+Methods

https://developer.mozilla.org/en-US/docs/Web/XPath/Functions/substring-after

Mark this as solution if you got a answer.

Thanks.
marys pindamarys pinda
Thank you my friends!
I will try...
marys pindamarys pinda
I tried using a regular expression, but it is giving errors!
Any tips?

Thank you

....
//Value informed into Visualforce Page
public string caseTT {get; set;}

....

public void planifier(){
//string titre= 'Id, Subject,Demandeur,Status'+'\n';
string contenuCSV = '';
Matcher pm = Pattern.compile('(?<=select)(.*)(?=from)').matcher( caseTT );
List<Case> listCases = database.query(caseTT);
String[] listFields = pm;

for(Integer i = 0; i<listCases.size(); i++){
     String csvRow = null;
     for(String s : listFields){ 
        if(csvRow == null){
             csvRow = listCases[i].get(s);
        }
        else{
             csvRow += ',' + listCases[i].get(s);
        }
     }
     csvRow += '\n';
     contenuCSV += csvRow;
}

.......

OnpursuitOnpursuit
Hi ,

You can use substringAfter and substringBefore methods as Praveen mentioned.
They are a lot easier to use and no need to maintain expressions.

Thanks
marys pindamarys pinda
It really is easier ... but how do I do the following next step?

for(Case a: (List<case>)database.query(caseTT) )
{
   string contenu = a. casenumber  + ',' + a.subject+ ',' +a.Contact.Name+ ',' + a.s tatus +'\n';
   contenuCSV = contenuCSV + contenu ;
}


Thank you!
praveen murugesanpraveen murugesan
Hi,

If I understood question correctly, my answer is 

String fieldValues = '';
        SObjectType objectType = Schema.getGlobalDescribe().get(case);
        DescribeSObjectResult objDesc = objectType.getDescribe();
        Map<String, SObjectField> fields = objDesc.fields.getMap();
        Set<String> fieldSet = fields.keySet();
        for(String f : fieldSet)
        {
            fieldValues += f +',';
        } 
        if(fieldValues.endsWith(','))
        fieldValues= fieldValues.substring(0,fieldValues.length()-1);

// here field values will hole the all the field in Object Case.

Thanks
marys pindamarys pinda
Praveen, thank you...
I tried, but there is a error:

Attempt to de-reference a null object
Error is in expression '{!planifier}' in component <apex:commandButton> in page

global class AAAExporterCSV3 implements System.Schedulable {
public String mailSouhaite {get; set;}
public string caseTT {get; set;}

global void execute(SchedulableContext sc) {
planifier();
}

public void planifier(){
 
String contenuCSV = '';
        SObjectType objectType = Schema.getGlobalDescribe().get(caseTT);
        DescribeSObjectResult objDesc = objectType.getDescribe();
        Map<String, SObjectField> fields = objDesc.fields.getMap();
        Set<String> fieldSet = fields.keySet();
        for(String f : fieldSet)
        {
            contenuCSV += f +',';
        } 
        if(contenuCSV .endsWith(','))
        contenuCSV = contenuCSV.substring(0,contenuCSV .length()-1);

Messaging.EmailFileAttachment csvPJ = new Messaging.EmailFileAttachment();
blob csvBlob = Blob.valueOf(contenuCSV);
string csvNom = 'cases_fermes.csv';
csvPJ.setFileName(csvNom);
csvPJ.setBody(csvBlob);
Messaging.SingleEmailMessage email =new Messaging.SingleEmailMessage();
String[] adressMail = new list<string> {mailSouhaite};
String subject = 'CSV';
email.setSubject(subject);
email.setToAddresses( adressMail );
email.setPlainTextBody('mail .....');
email.setFileAttachments(new Messaging.EmailFileAttachment[]{csvPJ});
Messaging.SendEmailResult [] envoyer = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
  }
}

praveen murugesanpraveen murugesan
Hi veronica,

The object name should be in single quotes and if its a custom object you need add __c at the end.

Eg.

String contenuCSV = '';
        SObjectType objectType = Schema.getGlobalDescribe().get('caseTT__c');
        DescribeSObjectResult objDesc = objectType.getDescribe();
        Map<String, SObjectField> fields = objDesc.fields.getMap();
        Set<String> fieldSet = fields.keySet();
        for(String f : fieldSet)
        {
            contenuCSV += f +',';
        }
        if(contenuCSV .endsWith(','))
        contenuCSV = contenuCSV.substring(0,contenuCSV .length()-1);

Mark this as solution if you got a answer.

Thanks.

Praveen Murugesan


marys pindamarys pinda
Same error... =s

Attempt to de-reference a null object
Error is in expression '{!planifier}' in component <apex:commandButton> in page

CLASS APEX

global class AAAExporterCSV3 implements System.Schedulable {
public String mailSouhaite {get; set;}
public string caseTT {get; set;}

global void execute(SchedulableContext sc) {
planifier();
}

public void planifier(){
String contenuCSV = '';
        SObjectType objectType = Schema.getGlobalDescribe().get('caseTT__c');
        DescribeSObjectResult objDesc = objectType.getDescribe();
        Map<String, SObjectField> fields = objDesc.fields.getMap();
        Set<String> fieldSet = fields.keySet();
        for(String f : fieldSet)
        {
            contenuCSV += f +',';
        }
        if(contenuCSV .endsWith(','))
        contenuCSV = contenuCSV.substring(0,contenuCSV .length()-1);
        
Messaging.EmailFileAttachment csvPJ = new Messaging.EmailFileAttachment();
blob csvBlob = Blob.valueOf(contenuCSV);
string csvNom = 'cases_fermes.csv';
csvPJ.setFileName(csvNom);
csvPJ.setBody(csvBlob);
Messaging.SingleEmailMessage email =new Messaging.SingleEmailMessage();
String[] adressMail = new list<string> {mailSouhaite};
String subject = 'CSV';
email.setSubject(subject);
email.setToAddresses( adressMail );
email.setPlainTextBody('Maillll');
email.setFileAttachments(new Messaging.EmailFileAttachment[]{csvPJ});
Messaging.SendEmailResult [] envoyer = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
  }
}
PAGE VISUALFORCE

<apex:page controller="AAAExporterCSV3">
  <apex:form >
  <apex:PageBlock >
    :::::::::: PARAMETER :::::::::: <br /><br />
    Mail :<apex:inputText value="{!mailSouhaite}"/><br /><br />
    Requête :<apex:inputText value="{!caseTT}"/><br /><br />
    <apex:commandButton value="Valider" action="{!planifier}"/> <br/><br/>
    <apex:commandButton value="Effacer"/> <br/><br/>
  </apex:PageBlock>
  </apex:form>
</apex:page>


Thank you
praveen murugesanpraveen murugesan
Hi,

I think the error is coming from some other place,

I have checked through developer console with account object.

String contenuCSV = '';
        SObjectType objectType = Schema.getGlobalDescribe().get('Account');
        DescribeSObjectResult objDesc = objectType.getDescribe();
        Map<String, SObjectField> fields = objDesc.fields.getMap();
        Set<String> fieldSet = fields.keySet();
        for(String f : fieldSet)
        {
            contenuCSV += f +',';
        }
        if(contenuCSV .endsWith(','))
        contenuCSV = contenuCSV.substring(0,contenuCSV .length()-1);
system.debug('~~~~~~~~'+contenuCSV);

Its worked fine.


 User-added image

Thanks.

Praveen Murugesan

marys pindamarys pinda
I do not know what's wrong with my code ...
I'm trying to figure out!

Thank you
marys pindamarys pinda
Is it possible to do this part to be dynamic and not static?


for (Case a: (<case> List) database.query (caseTT))
{
    a.casenumber string contenu = + ',' + a.subject + ',' + a.Contact.Name + ',' + a.status + '\ n';
    contenuCSV = contenuCSV + contenu;
}

something like:

for(ValueInformedInputTexVisualforcePage a: (List<ValueInformedInputTextVisualforcePage>)database.query(caseTT) )
{
   string contenu = a.ValueInformedInputTexVisualforcePage + ',' + a.ValueInformedInputTexVisualforcePage + ',' +a.ValueInformedInputTexVisualforcePage+ ',' + a.ValueInformedInputTexVisualforcePage +'\n';
   contenuCSV = contenuCSV + contenu ;
}

Thank you!

praveen murugesanpraveen murugesan
Hi,

String contenuCSV = '';
        SObjectType objectType = Schema.getGlobalDescribe().get('Account');
        DescribeSObjectResult objDesc = objectType.getDescribe();
        Map<String, SObjectField> fields = objDesc.fields.getMap();
        Set<String> fieldSet = fields.keySet();
        for(String f : fieldSet)
        {
            contenuCSV += f +',';
        }
        if(contenuCSV .endsWith(','))
        contenuCSV = contenuCSV.substring(0,contenuCSV .length()-1);
Messaging.EmailFileAttachment csvPJ = new Messaging.EmailFileAttachment();
blob csvBlob = Blob.valueOf(contenuCSV);
string csvNom = 'cases_fermes.csv';
csvPJ.setFileName(csvNom);
csvPJ.setBody(csvBlob);
Messaging.SingleEmailMessage email =new Messaging.SingleEmailMessage();
String[] adressMail = new list<string> {'ppraveenm@gmail.com'};
String subject = 'CSV';
email.setSubject(subject);
email.setToAddresses( adressMail );
email.setPlainTextBody('mail .....');
email.setFileAttachments(new Messaging.EmailFileAttachment[]{csvPJ});
Messaging.SendEmailResult [] envoyer = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});

I have tested this code with developer console and i have received mail. if you see here mail address is manually entered.

So I think the issue is on public String mailSouhaite {get; set;}
public string caseTT {get; set;}
 pls check this.
marys pindamarys pinda
I do not understand where you wrote your request.
I need a Visualforce page to get the request from the input text (caseTT).
Where will you inform your request to use within Apex code?

Thank you!!!
praveen murugesanpraveen murugesan
Hi,

Could you pls mail me your code. both page and controller. 

my mail id is ppraveenm@gmail.com
marys pindamarys pinda
OK!
Thank you...