+ Start a Discussion
marys pindamarys pinda 

Use the value of a string as a type

Hi,

Is it possible to use the value of a string as a type?
How do I do this?
Thank you

Example:
String aaa = 'Case';
for(aaa a: (List<aaa>)database.query(stringQuery))
=
for(Case a: (List<Case>)database.query(stringQuery))
Priyanka JoshiPriyanka Joshi
Hello,

We can use the value of string as below:

String aaa = 'Case';
string query = 'Select Id from '+ aaa +' limit 10';
for(Sobject a: database.query(query))
{
   System.debug('==='+a);
}

It is not possible to use value of string without typecasting .
marys pindamarys pinda
I can only convert the CASE of a type?
Something like:

    String aaa = 'Case';
    database.query(aaa);

Thank you
Priyanka JoshiPriyanka Joshi
Could please provide me the exact code. I am not able to use it like that.
marys pindamarys pinda
OK...
Thank you!

CONTROLLER:

global class AAAACSV implements System.Schedulable {
public String mailSouhaite {get; set;}
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;

String aaa = 'Case';
  
for(aaa a: (List<aaa>)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...............');
                       
email.setFileAttachments(new Messaging.EmailFileAttachment[]{csvPJ});
Messaging.SendEmailResult [] envoyer = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});

}
}

PAGE:
<apex:page controller="AAAACSV">
    <style type="text/css">
        .classeRequete { width: 600px; }
        .classeMail { width: 400px; }
    </style>
      
  <apex:form >
  <apex:PageBlock >
    :::::::::: PARAMETRAGE :::::::::: <br /><br />
    Mail..........:&nbsp;<apex:inputText styleClass="classeMail" value="{!mailSouhaite}"/><br /><br />
    Requête :&nbsp;&nbsp;<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>
Priyanka JoshiPriyanka Joshi
Thank you marys.
marys pindamarys pinda
Can you help me?
I'm new at this language and it's very difficult.
Thank you
marys pindamarys pinda
Hi,

Another tip?