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
intern2424intern2424 

Help with Dynamic SOQL

I was wonder how to create a dynamic soql statement with apex. I saved items in to an array but just don't know how to add them to the soql statement. The array looks like this:

 

My array will look like this: (First line is the object I created, second line is the name i want to put in, third line is the field name, and so on)

Person__c

John Doe

Name

27

Age__c

04/04/1988

Birth_date__c 

Dallas

City__c

Saint Tom

Street__c 

12345

Zip__c  

 

 

 

Thanks for any help you can give. 

Best Answer chosen by Admin (Salesforce Developers) 
intern2424intern2424

thank you so much. My friend came did this:

 

for (Integer i=0; emailBody.size() != i; i++) {
    String[] values = emailBody[i].split(delim, 2);
    System.debug(values[0]);
    System.debug(values[1]);
    System.debug(i);

    newPerson.put(values[0], values[1]);

 

 

I was wondering how to send that one record in an email body? This is my send email method.

 

send(emailAddress);

return result;
}

public static void send (String mailTo)
{

    String [] toAddresses = new String[] {mailTo};
    
    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
    
    mail.setToAddresses(toAddresses);
    mail.setSenderDisplayName('Email Confirmation');
    mail.setSubject('Your person has been added to the person object');
    mail.setSaveAsActivity(True);
    mail.setPlainTextBody('Thank you for adding another person to the person object.');
    
    Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail } );

    }

Thanks you for all your help.

All Answers

PragadheeshwariPragadheeshwari

Hai,

  I think you can directly insert like this,if suppose you splitted correctly,

Person__c[] newPerson = new Person__c[0];

newPerson.add(new Person__c(
               Name = name,
               Age__c = age,
               Birth_Date__c = birthdate,
               City__c = city,
               Street__c = street,
               Zip_Postal_Code__c = zip));

insert newPerson;
sfdcfoxsfdcfox

You mean, Dynamic DML. You can do this by obtaining a token for the correct type of record, then adding the appropriate values to the object, and finally attempting an insert/upsert/update, etc. You can create a generic record like so:

 

sObject s = new Account();

 

Then, you can add fields to it dynamically:

 

s.put(fieldName,fieldValue);

 

Finally, when you are done, you can insert or update the record.

 

insert s;

 

Of course, if a field does not exist, it will throw an error at this point.

 

You can use getGlobalDescribe() and getDescribe() to determine legal field and object names. Please refer to the documentation for more information.

intern2424intern2424

thank you so much. My friend came did this:

 

for (Integer i=0; emailBody.size() != i; i++) {
    String[] values = emailBody[i].split(delim, 2);
    System.debug(values[0]);
    System.debug(values[1]);
    System.debug(i);

    newPerson.put(values[0], values[1]);

 

 

I was wondering how to send that one record in an email body? This is my send email method.

 

send(emailAddress);

return result;
}

public static void send (String mailTo)
{

    String [] toAddresses = new String[] {mailTo};
    
    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
    
    mail.setToAddresses(toAddresses);
    mail.setSenderDisplayName('Email Confirmation');
    mail.setSubject('Your person has been added to the person object');
    mail.setSaveAsActivity(True);
    mail.setPlainTextBody('Thank you for adding another person to the person object.');
    
    Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail } );

    }

Thanks you for all your help.

This was selected as the best answer
sfdcfoxsfdcfox
Your code looks correct, although I've not used Messaging in a while. Is it not sending an email?
intern2424intern2424
I can send emails but I was wondering how I would put the object I Just created into the email body?