2

I'm not really sure if this is possible because i'm newbie but, can I push a custom string into a SOQL result?

I tried this code multiple times and i also modified it but i can't get the result i want:

List<Account> act = [SELECT Id, Name FROM Account]; 
String CmtTime = 'a_custom_string';
act.add(CmtTime);

then this one:

List<Account> clientResponse = [SELECT Id, Name FROM Account];
String CmtTime= 'a_custom_string';
List<Account> clientRes = new List<Account>();
for(Account actItem : clientResponse){
    clientRes.add(CmtTime);
    clientRes.add(actItem);
}

I want to accomplish this result:

          Id         | Name  |      CmtTime
   0015D00000axxxxx  | test1 |   a_custom_string
   0015D00000bxxxxx  | test2 |   a_custom_string
   0015D00000cxxxxx  | test3 |   a_custom_string
3

You cannot, but you can create a wrapper object with the SOQL result.

public class Wrapper{
    Account accountObj; // {get;set;} if you need to use in VF page
    String customString; // {get;set;} if you need to use in VF page
    public Wrapper(Account accountObj, String customString){
        this.accountObj = accountObj;
        this.customString= customString;
    }
}

Use this wrapper class in your code.

List<Account> clientResponse = [SELECT Id, Name FROM Account];
String CmtTime= 'a_custom_string';
List<Wrapper> clientRes = new List<Wrapper>();
for(Account actItem : clientResponse){
    clientRes.add(new Wrapper(actItem, 'a string here'));
}
|improve this answer|||||
2

Hi Welcome to Salesforce!

Firstly create field as "CmtTime" in Account Object.

Once you create field in account you can use in query as field Api name as "CmtTime__c". Remember here you have to use DML statement to update into database.

Here is how you can do

List<Account> clientResponse = [SELECT Id, Name,CmtTime__c FROM Account];
String CmtTime= 'a_custom_string';
List<Account> clientRes = new List<Account>();
for(Account actItem : clientResponse){
     actItem.CmtTime__c = CmtTime;
}
//DML statement to update account records. 
 update clientResponse;
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.