Creating an array of SelectOption Lists - salesforce

Im trying to set up an online test, using a visualforce page that pulls data from 3 objects in salesforce COPE_Tests__C, COPE_Questions__C, and COPE_Options__c. Once the user selects the specific test, I thought I would be able to make a call like this to get all the other data:
questions = [select id, name, question_body__c,
(select id, name, option_body__c from COPE_options__r order by name ASC)
from COPE_questions__c where COPE_test__c = :tid];
And then use apex:repeat and apex:selectRadio/apex:selectOption to generate the actual test form. But for some reason it would not render the radioboxes. So it would seem I need to create selectOption lists and then use apex:selectOptions. But im not sure how to set this up . How can I have it create a public list<selectOption> automatically for each question?
Is there a way to set up an array of list<selectOption>?

I don't know about creating it automatically but going over your question object in a loop should be pretty easy, something over the lines of
List<List<SelectOption> options = new List<List<SelectOption>;
for(COPE_Questions__C q : questions){
List<SelectOption> list = new List<SelectOption>();
for(COPE_options__r op : q.COPE_options__r){
list.add(new SelectOption(op.id, op.option_body__c);
}
options.add(list);
}
Hope it helps.

Related

Creating a Multi-Contact Event with Apex in Salesforce

I am attempting to use Apex to create a multi-contact event.
I have already enabled Allow Users to Relate Multiple Contacts to Tasks and Events in the activity settings in the scratch org.
I am following the guide and the example at the bottom of these docs but I am constantly getting an error when pushing to the scratch org:
// ...
event.setEventWhoIds(attendeeContactIds);
// ...
Method does not exist or incorrect signature: void setEventWhoIds(List<String>) from the type Event.
I also tried to write directly to the field with:
event.EventWhoIds = attendeeContactIds;
With that, I get the error, that the field is not writable.
attendeeContactIds is a List of Strings representing Contact IDs.
What could I be missing? 🤔🙇🏻‍♂️
It's bit stupid, it's readonly in apex. It's exposed so integrations can quickly create event and essentially a related list together in one all-or-nothing transaction. See also https://salesforce.stackexchange.com/questions/238094/eventwhoids-is-not-writeable-in-apex-class-but-working-on-jsforce
Try something like that?
Savepoint sp = Database.setSavepoint();
event e = new Event(
StartDateTime = System.now(),
EndDateTime = System.now().addHours(1)
);
insert e;
List<EventRelation> invitations = new List<EventRelation>();
for(Contact c : [SELECT Id FROM Contact LIMIT 5]){
invitations.add(new EventRelation(
EventId = e.Id,
RelationId = c.Id,
IsInvitee = true
));
}
insert invitations;
Database.rollback(sp); // unless you really want to send it out

Pre-fill with the id of an entity

How can I pre-fill a new content with an entity relationship that I know the id? I know the item is, let's say 1234, but if I use this number like that :
#Edit.Toolbar(actions: "new", contentType: "NewsItem", prefill = new { Category = 1234 } )
It doesn't work. When I implement the code above, the form for a new item shows up but instead of having the correct item selected, I have (item not found). Obviously, my argument is not correct. So, how do I code this argument?
I also tried
Category = {1234}
Category = new {(1234}}
Category = "1234"
But nothing works. Any ideas?
We're working on enhancing this, but as of now, you are best off using the guid - as shown in the wiki: https://github.com/2sic/2sxc/wiki/razor-edit.toolbar#multiple-entities-prefil

Salesforce Apex Class update custom object lookup field with id from parent

I am new to Apex and I’m struggling with creating a class to help me with some data analysis. I have data from a 3rd party (transactions__C) that has a field (fin_acct_txt__c) that is the pointer to another object (fin_accounts__C). I want to updated transactions__c with the id from fin_accounts__C into the lookup field transactions__c.fin_acct__c.
I want to do this in a class versus a trigger as there would be thousands of records loaded from the 3rd party on a monthly basis. I think doing this in bulk would be more efficient.
My thought is I create a list for transactions__c and a map for fin_accounts__c. Using the fin_acct_txt__c=fin_accounts__c.name I would be able to get the fin_accounts__c.id and update the transactions__c.fin_acct__c with that data.
But being new to Apex seems to be causing me some problems that I’m unsure how to resolve.
Here’s a copy of what I’ve done to date:
public class updateTxnFinAcctID {
// Build map of financial accts since that is unique
map<string ,fin_acct__c> finAccts = new map<string, fin_acct__c>
([select id,name from fin_acct__c where name!=null]);
//Iterate through the map to find the id to update the transactions
{
for(fin_acct__c finAcct: finAccts.values())
{
if (finAcct.name != Null)
{
finAccts.put(finAcct.name, finAcct);
}
// Find all records in transaction__c where fin_acct__c is null
//and the pointer is the name in the map
list<Transaction__c> txns =[
select id,fin_acct_txt__c from Transaction__c where fin_acct__c = null
and fin_acct_txt__c=:finaccts[0].name];
//create the list that will be used to update the transaction__c
list <Transaction__c> txnUpdate = new list <Transaction__c>();
{
//Find the id from fin_acct__c where name = fin_acct_txt__c
for (Transaction__c txn: txns){
finacct[0].Id =txn.fin_acct__c;
txnUpdate.add(txn);
}
//3. Update transaction with ID
}
}
// if (txnUpdate.size()>0 { update txnUpdate};
system.debug(txnUpdate.size());
}
}
I seem to be in a doom loop. The error I get is “Expression must be a list type: Map” pointing to the list txns = [ …]. But as that is not unique, it must be list. But I would believe I’ve got something structurally wrong here and that is a symptom of a larger issue.
Thanks.
I tried to understand what should to do your code, and I have a few tips, possibly they help to solve your issue:
1) In first loop over values of map finAccts you really don't need validation with if (finAcct.name != Null), because you already add it in SOQL query (where name!=null).
2) It's a bad practice - to put as a key to map different entities (for example, Ids and Names). I mean that when you queried fin_acct__c into the map finAccts, keys of the map are Ids of fin_acct__c. And then in the first loop you put in the same map the same objects only using their names as a key. If you really need such map with names as a keys is better to create new map and put the data there.
3) You execute SOQL query to Transaction__c object into the loop. It is likely to be the cause of an exception related to the SF limits (Especially if you are sure that the code will handle large amounts of data). Better collect all fin_acct__c names in list and move SOQL query out from the loop, using IN instead of = in where condition.
If I understood correctly that fin_acct_txt__c field contains names, not Ids, your class should looks something like:
public class updateTxnFinAcctID {
Map<String ,fin_acct__c> finAccts = new Map<String, fin_acct__c>
([select id,name from fin_acct__c where Name != null]);
Map<String, fin_acct__c> finAcctByNames = new Map<String, fin_acct__c>();
for(fin_acct__c finAcct: finAccts.values()){
finAcctByNames.put(finAcct.Name, finAcct);
}
List<Transaction__c> txns =[select id, fin_acct_txt__c, fin_acct__c
from Transaction__c where fin_acct__c = null
and fin_acct_txt__c IN finAcctByNames.keySet()];
List <Transaction__c> txnUpdate = new List<Transaction__c>();
for (Transaction__c txn: txns){
fin_acct__c relatedFinAcct = finAcctByNames.get(txn.fin_acct_txt__c);
if(relatedFinAcct != null){
txn.fin_acct__c = relatedFinAcct.Id;
txnUpdate.add(txn);
}
}
if(!txnUpdate.isEmpty()){
update txnUpdate;
system.debug(txnUpdate.size());
}
}
It possibly can contains some spelling mistakes, but it's a common idea.

Update Multi-Picklist on updating custom object

I have custom object KeywordAccountAssociation__c. This object has three fields
Account__c - Master-Detail(Account)
Keyword__c - Master-Detail(Keyword)
Compositecp__c - Text(255) (External ID) (Unique Case Sensitive)
I have a custom field in Account
DD_Segment__c - multi-picklist
Now I want to update (Insert is fine too) values of DD_Segment__c whenever KeywordAccountAssociation__c is updated. I could write trigger for this but I don't know how? I am new to Salesforce Development and my background is ruby (so getting accustomed to apex is bit difficult for me).
KeywordAccountAssociation__c has multiple rows of Account__c which has same account_id and those account_id are related to a record of custom object Keyword__c. I want to get all keywords related to one account and update in its (account's) multi-picklist. How can I achieve this? If you have doubts about this please do ask. Thanks!
One issue is related to learning to work with triggers in general, which can be started with Salesforce Apex Developer Documents on Triggers
but to answer your actual question, you would essentially need to build a trigger against your custom object that would update the related account. It might look something like this:
trigger keywordAccountUpdate on KeywordAccountAssociation__c (after insert, after update){
set<id> = new set<id>();
for (KeywordAccountAssociation__c a : Trigger.new)
accountIds.put(a.Account__c);
map<id,Account> accountMap = new map<id,Account>([select id, DD_Segment__c from Account where id in :accountIds]);
for (KeywordAccountAssociation__c kaa : Trigger.new){
if (AccountMap.containskey(kaa.Account__c)){
Account thisAccount = AccountMap.get(kaa.Account__c);
String s = thisAccount.DD_Segment__c + ';new value'; // Always add value
if ((thisAccount.DD_Segment__c).contains('second value')
s += ';second value';
AccountsToUpdate.add(new Account(id=thisAccount.id, DD_Segment__c = s));
}
}
}
Please keep in mind that I don't have the structure to test this trigger, I just free handded it, so YMMV.

how to display the data returned by get_by_user_id() in DataMapper Code Igniter?

I am new to code igniter data mapper. I have a table called user, and I am trying to retrieve data from the database table and show them to the user.
Here is what I have in the model:
$u=new User();
$results=$u->get_by_user_id($id);
//$results here will be set to huge bunch of none sense data( which also includes the row that I am looking for as well)
if ($u->exists())
{
foreach ($results->all as $row){
$data['user']['first_name']=($row->user_first); //this where I am stuck ..
$data['user']['last_name']=($row->user_last);//this is also where I am stuck..
}
I don't know how to treat results to get a required fields I am looking for and store them in the $data I am passing to the user to view.
Thanks!
When you call get_by_x() on the model, the fields will be populated with data and you can access them like this:
$u = new User();
$u->get_by_user_id($id);
if($u->exists())
{
// you can access the table columns as object fields
$data['user']['first'] = $u->first;
$data['user']['last'] = $u->last;
}
else
{
$data['error'] = 'No such user!';
}
Have a look at the documentation which is really helpful: see Get and Get By.
Also, DataMapper expects all tables to have an id column: see Table Naming Rules. If your column is named id you should then call $u->get_by_id($id) instead of $u->get_by_user_id($id).

Resources