You need to sign in to do that
Don't have an account?
Zuhair Shaikh
Need help with creating a class that calls for data from a standard Contact field, makes a comma-separated list and then updates it to a custom field in Account.
Hello. First time poster. I feel like this is an easy problem but I seem to be stuck at actually updating the records. So essentially, I need to create a class that will, for each account in system, update Contact_Email_List__c (which I have created) with the list of email ids (comma separated) from Contact related list of account.
It gives the following problem, "DML requires SObject or SObject list type: String". I feel like I need to transfer the string to a SObject but I can't seem to figure out how.
public class EmailListMaker { public void EmailListMaker(){ List<String> emails = new List<String>(); for (Contact em : [Select Email FROM Contact ORDER BY Name]){ emails.add(em.Email); } String emailList = String.join(emails, ', '); upsert emailList Account.Fields.Contact_Email_List__c; } }
It gives the following problem, "DML requires SObject or SObject list type: String". I feel like I need to transfer the string to a SObject but I can't seem to figure out how.
All Answers
Here is the modified code which updates concatinated string of email ids to respective account.
This method takes AccountId as parameter, updates that acount's contact email ids in Contact_Email_List__c field.
public class EmailListMaker {
public void EmailListMaker(Id AccId){
List<String> emails = new List<String>();
for (Contact em : [Select Email FROM Contact WHERE AccountId=:AccId ORDER BY Name ]){
emails.add(em.Email);
}
String emailList = String.join(emails, ', ');
// Update email string in corresponding Account
Account accUpdate = new Account(Id=AccId);
accUpdate.Contact_Email_List__c = emailList;
update accUpdate;
}
}
If you want to update all accounts, you need to go for a Batch apex class.
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_batch_interface.htm
Thanks,
Nagendra Prasad
Nagendra, sorry for being unclear, I shouldn't have said Ids. I also didn't want to input any parameter. However, thanks for replying.
Hi Zuhair,
Please use below code snippet on your requirement, It will help you definately :)
If you want to utilize in apex class, please let me know :)
Thanks
Hi Zuhair,
Below is class code snippet, please use the same and let me know :)
Please let me know, If it works
Thanks
Hi Zuhair,
Thanks :)