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
indranily81indranily81 

is there a way to change an SObject type to string ?

Hi ,

 

is there a way to change an SObject type to String ?

Something like below :

 

1. Sobject [] test = [Select email from User Limit 1] ;

2. String getmail = test[0].toString() ;

 

i am having trouble in casting an Sobject to String as mentioned in line 2.

Thanks in advance.

WesNolte__cWesNolte__c

Hey

 

I think what you're trying to do is fetch the email address of that user?

 

In that case try this,

 

 

1. Sobject [] test = [Select email from User Limit 1] ;

2. String getmail = ((User)test[0]).email;

 

Cheers,
Wes

Message Edited by weznolte on 10-15-2009 02:20 AM
prageethprageeth
Hello Indranily81;
It seems that you think the query "[Select email from User Limit 1]" returns an email address.
No, it returns a sObject which has the type of User
 
For an example;
  User test = [Select email from User Limit 1]; 
  getmail =  test.email;
is correct.
 
Or you can retrieve email address directly as below.
String getmail = [Select email from User Limit 1].email ; 
 
Or your method can be changed as below. 
 

public String getEmailAddress() {

Sobject[] test = [Select email from User Limit 1];//"Limit 1" part is not necessary because you assign the result to an array.

String getmail = ((User)test[0]).email;//cast generic sObject to a User sObject

return getmail; 

}

 

Message Edited by prageeth on 10-15-2009 02:18 AM
indranily81indranily81

Hi prageeth,

First of all thank you very much for the suggestion. But my requirement is to send bulk email to all the users through a VF page. I am listing the code which will actually send the mails. I have tried to implement your idea, but somehow its not working. When I am trying to save this am getting the error message as

Compile Error: Initial term of field expression must be a concrete SObject: LIST:SObject at line 97 column 27

what I can understand is somehow the looping (probably the length attribute is not there) is wrong and I am not

sure how to put all the emails into the emailHolder array. 

Code:-

public void sendmail() {
  
     String [] emailHolder;
  
     Sobject []dataHolder = [select email from User];
           
     for (Integer i=0; i<=dataHolder.length; i++) {

     //for (Sobject i = 0 : dataHolder) {
         emailHolder[i] = ((User)dataHolder[i]).email ;
         System.debug(i);
     }

 

   /*

      mail trigger code will be written here
 

   */

}


Regards
indranily81

XactiumBenXactiumBen

Instead of using SObject, why not try just using User:

 

public void sendmail() { List<String> emailHolder = new List<String>(); User[] dataHolder = [select email from User]; for (User u : dataHolder) { emailHolder.add(u.Email); System.debug(i); } /* mail trigger code will be written here */ }

 

 

I put the changes I made in red to your code - hope it helps.

Message Edited by XactiumBen on 10-15-2009 11:27 AM
WesNolte__cWesNolte__c

Try this,

 

 public void sendmail() {
  
     List<String> emailHolder = new List<String>();   // You could use an array too

           
     for (User u: [select email from User]) {
         emailHolder.add(u.email);
         System.debug('EmailHolder: '+emailHolder);
     }

 

   /*

      mail trigger code will be written here
 

   */

}

 

Cheers,

Wes

WesNolte__cWesNolte__c
Sorry Ben, not trying to steal your thunder:) XatiumBen's solutin will work too..
prageethprageeth

Hello indranily81;

Now the problem is with your for loop. Do the change that I've marked with red. Then your code will work.

  for (Integer i=0; i <= dataHolder.size(); i++) { 

But actually as XactiumBen says, you need not to assign the query reult to a sObject. You can directly assign it to a User object.

Ex:- 

User[] dataHolder = [select email from User];  

 

 


indranily81indranily81

Thank you guys for all your help. As per your suggestion  I have modified the code and the issue is resolved but I am stuck with a new issue. It's related to sending

mail.

 

public void sendmail() {
   
     String [] emailHolder;
     User[] dataHolder = [select email from User];
       
     for (user u : dataHolder) {
        
         emailHolder.add(u.Email);
        
     }
      
     Messaging.MassEmailMessage mail = new Messaging.MassEmailMessage();  
     mail.setToAddresses(emailholder); // line 101
     Messaging.sendEmail(new Messaging.Email[] { mail } , false);
  
    }

 

    But now the error is coming as :

    Error: Compile Error: Method does not exist or incorrect signature: [Messaging.MassEmailMessage].setToAddresses

    (LIST:String) at line 101 column 6

 

   As a new apex developer I would be happy to see if there is any API for apex like we

   have for Java. What i am looking for is something like http://java.sun.com/j2se/1.5.0/docs/api/

 

 

   Regards

   indranily81

 

 

 

 

 

 

WesNolte__cWesNolte__c

Unfortunately there's not java doc equivalent:(

 

You can use this though, http://www.salesforce.com/us/developer/docs/apexcode/index.htm.

 

Wes

prageethprageeth

Messaging.MassEmailMessage has not method as setToAddresses(); 

According to your requirement you have to first decide whether to use  Messaging.MassEmailMessage or  

Messaging.SingleEmailMessage.

 

For  Messaging.MassEmailMessage  you have to use setTargetObjectIds() method.

List<id> contactIds = new List<id>();

  contactIds.add('00Q900000015xxx');

Messaging.MassEmailMessage mail = new Messaging.MassEmailMessage();

mail.setTargetObjectIds(contactIds);

 

For  Messaging.SingleEmailMessage  you can use setToAddresses() method. 

Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

mail.setToAddresses(emailholder);//your email address list