Mass Action: Set multiple values in condition

  • 1
  • Question
  • Updated 3 years ago
  • Answered
Is it possible to set multiple values in a condition based on records selected in a table component?

For example, I have a list of opportunity records in a table.  I select 3 of them and press a mass action button.  I want to update the condition on my aggregate model to calculate the value of those three opportunities.  
Photo of Peter Herzog

Peter Herzog

  • 1,664 Points 1k badge 2x thumb

Posted 4 years ago

  • 1
Photo of mB Pat Vachon

mB Pat Vachon, Champion

  • 42,714 Points 20k badge 2x thumb
A javascript snippet can do it for sure. Essentially do what is described below.

It would nifty if a model called something like "SelectedRows" could be set using an action like "Add Row(s) to Model". Then you could this easily.
Photo of Zach McElrath

Zach McElrath, Employee

  • 49,056 Points 20k badge 2x thumb
Currently you can only do this with a Snippet. 

Assuming you have an Aggregate Model called "OppsAgg" with a Condition called "SelectedOppIds" of type "Multiple specified values", expecting 1 or more Opportunity Ids, then you could have this for a Mass Action Snippet:

var params = arguments[0],    
    list = params.list,
    selectedIds = skuid.$.map(list.getSelectedItems(),function(item){
       return item.row.Id; 
    });
    
var AggModel = skuid.$M('OppsAgg');
var OppIdsCondition = AggModel.getConditionByName('SelectedOppIds');
AggModel.setCondition(OppIdsCondition,selectedIds);
AggModel.updateData();

Here is the XML for a sample page that updates a table at the bottom when you select one or more rows in the top table and click "Update Selected Opp Ids":

<skuidpage unsavedchangeswarning="yes" showsidebar="true" showheader="true" tabtooverride="Opportunity">   <models>
      <model id="Opportunity" limit="100" query="true" createrowifnonefound="false" sobject="Opportunity">
         <fields>
            <field id="Name"/>
            <field id="CreatedDate"/>
            <field id="AccountId"/>
            <field id="Account.Name"/>
            <field id="Amount"/>
         </fields>
         <conditions/>
         <actions/>
      </model>
      <model id="OppsAgg" limit="" query="false" createrowifnonefound="false" sobject="Opportunity" doclone="" type="aggregate">
         <fields>
            <field id="Amount" name="sumAmount" function="SUM"/>
         </fields>
         <conditions>
            <condition type="multiple" value="" field="Id" operator="in" enclosevalueinquotes="true" state="filterableoff" inactive="true" name="SelectedOppIds">
               <values>
                  <value/>
               </values>
            </condition>
         </conditions>
         <actions/>
         <groupby method="simple">
            <field id="AccountId" name="accountId"/>
            <field id="Account.Name" name="accountName"/>
         </groupby>
      </model>
   </models>
   <components>
      <pagetitle model="Opportunity">
         <maintitle>
            <template>{{Model.labelPlural}}</template>
         </maintitle>
         <subtitle>
            <template>Home</template>
         </subtitle>
         <actions>
            <action type="savecancel"/>
         </actions>
      </pagetitle>
      <skootable showconditions="true" showsavecancel="false" searchmethod="server" searchbox="true" showexportbuttons="false" pagesize="10" createrecords="true" model="Opportunity" mode="read">
         <fields>
            <field id="Name" allowordering="true"/>
            <field id="AccountId" valuehalign="" type="">
               <label>Account</label>
            </field>
            <field id="Amount"/>
            <field id="CreatedDate" allowordering="true" valuehalign="" type=""/>
         </fields>
         <rowactions>
            <action type="edit"/>
            <action type="delete"/>
         </rowactions>
         <massactions usefirstitemasdefault="true">
            <action type="custom" label="Update Selected Opp Ids" icon="sk-icon-magic" snippet="UpdateSelectedOppIds"/>
            <action type="massupdate"/>
            <action type="massdelete"/>
         </massactions>
         <views>
            <view type="standard"/>
         </views>
      </skootable>
      <pagetitle model="OppsAgg">
         <maintitle>Total by Account</maintitle>
         <actions/>
      </pagetitle>
      <skootable showconditions="true" showsavecancel="false" searchmethod="server" searchbox="true" showexportbuttons="false" pagesize="10" createrecords="false" model="OppsAgg" buttonposition="" mode="readonly">
         <fields>
            <field id="Account.Name" name="accountName" valuehalign="" type=""/>
            <field id="Amount" name="sumAmount" decimalplaces="" valuehalign="" type=""/>
         </fields>
         <rowactions/>
         <massactions usefirstitemasdefault="true"/>
         <views>
            <view type="standard"/>
         </views>
      </skootable>
   </components>
   <resources>
      <labels/>
      <css/>
      <javascript>
         <jsitem location="inlinesnippet" name="UpdateSelectedOppIds" cachelocation="false">var params = arguments[0],
    list = params.list,
    selectedIds = skuid.$.map(list.getSelectedItems(),function(item){
       return item.row.Id; 
    });
    
var AggModel = skuid.$M('OppsAgg');
var OppIdsCondition = AggModel.getConditionByName('SelectedOppIds');
AggModel.setCondition(OppIdsCondition,selectedIds);
AggModel.updateData();</jsitem>
      </javascript>
   </resources>
</skuidpage>



Photo of Matt Sones

Matt Sones, Champion

  • 31,478 Points 20k badge 2x thumb
So, you can set conditions with multiple values for basic models, but not aggregate models? We did the former here: https://community.skuidify.com/skuid/topics/populating-values-in-a-url-from-a-table.

Why doesn't something like this work?

Add condition to your model where the Id field can be multiple specified values. Leave the values blank, and make the condition filterable default off.
Set your mass action to "Run multiple actions."
  1. Activate and set value of you condition. Set it to {{Id}}.
  2. Query model.
Photo of Zach McElrath

Zach McElrath, Employee

  • 49,056 Points 20k badge 2x thumb
Jim,

It doesn't have anything to do with what kind of Model it is, the problem is that Skuid Merge Syntax doesn't have a way right now to grab all of the Ids for all selected records in a Table, and create an Array / List of these Ids, and pass that List into the "Activate and set value of Condition" action type. That is what is required for this to work.

I looked at the community post you mentioned, and I'm not exactly sure what it is doing, but I think it will only work for the first selected record, not for all of them.
Photo of Matt Sones

Matt Sones, Champion

  • 31,478 Points 20k badge 2x thumb
Zack,

I've got it working for all selected records, but only on a basic model, not an agg model.
Photo of Zach McElrath

Zach McElrath, Employee

  • 49,056 Points 20k badge 2x thumb
Oh, do you mean that the table where you're selecting records is connected to an Aggregate Model? That would make sense. In the Snippet above, I am grabbing the Ids of the selected rows, but if your Table is on an Aggregate Model, you'll definitely need to be grabbing a different field than Id to insert into the Condition on your other Model.

For example, lets say the table where you're selecting records is connected to an aggregate Model on Opportunity, showing the SUM(Amount) grouped the AccountId field, with the AccountId field's Alias Name being: accountId

Then we have some other Model on the page that we want to update to be showing data only for the selected rows' accountIdvalues. Basically all we need to change is the field we grab off of item.row:

var params = arguments[0],    
    list = params.list,
    selectedIds = skuid.$.map(list.getSelectedItems(),function(item){
       return item.row.accountId
    });
    
var ModelToUpdate = skuid.$M('SomeOtherModel');
var ConditionOnModelToUpdate = ModelToUpdate AggModel.getConditionByName('SelectedAccountIds');
ModelToUpdate.setCondition(ConditionOnModelToUpdateOppIdsCondition,selectedIds);
ModelToUpdate.updateData();
Photo of Peter Herzog

Peter Herzog

  • 1,664 Points 1k badge 2x thumb
Awesome!  This is working great and is exactly what I needed.  Thanks Zach!
Photo of Jason Thurgood

Jason Thurgood

  • 654 Points 500 badge 2x thumb
I was trying to use a Table with a Mass Action in order to conditionally render Calendar Event Source results.  I have 2 models.  EmployeesCLAB and EmployeeItems (the Event Source).  Both models are basic (not aggregate).  The EmployeeItems model has a condition called EmployeeFilter which is inactive, set up as multi with no values and is off by default. 

My snippet for the Table Mass Action button looks like this: 

var params = arguments[0],   
    list = params.list,
    selectedIds = skuid.$.map(list.getSelectedItems(),function(item){
       return item.row.Id; 
    });
    
var ModelToUpdate = skuid.$M('EmployeeItems');
var ModelCondition = ModelToUpdate.getConditionByName('EmployeeFilter');
ModelToUpdate.setCondition(ModelCondition,selectedIds);
ModelToUpdate.updateData();
Nothing is happening when I click the Mass Action - it does not seem to be adding the multiple values to the model condition.   

Did I miss something obvious? 
Photo of Rob Hatch

Rob Hatch, Official Rep

  • 44,006 Points 20k badge 2x thumb
Hmm. I built something like this yesterday.   I set up the EmployeeFilter condition as a "field from another model" condition - with the "IN" operator so it includes the full array of ID's as its value.  Of course you can set this up as "Filterable default off" so that it doesn't run from page load this way..

I think you should be successful with that mechanism.  
Photo of Jason Thurgood

Jason Thurgood

  • 654 Points 500 badge 2x thumb
I think this is a little more complicated than that.  Perhaps I am overthinking.  But, what I am currently experimenting with based on your response looks like this: 
  • Table 1= a list of all of the Employees from a specific department (AllDepartmentEmployees)

    This table has a Mass Action that Adds New Rows into the SelectedEmployees Model and one of the fields I am populating is a Lookup to Employee

  • Table 2 = a list of the Employees that are actively being filtered
    (SelectedEmployees - starts as an empty model), rows appear in this table as a result of clicking the mass action in Table 1

  • Calendar = Shows the Employees that are actively being filtered (EmployeeItems)

    The EmployeeItems model has a Conditional Filter that is off by default that looks for records where the Employee lookup value (Id) in the SelectedEmployees model.  This filter is activated by the mass action in Table 1. 

  • Ability to remove Employees from the Filter

    Basically this would work similar to how users are added to public groups.  List of available users on the left, selected users on the right, with the ability to move rows back and forth.  The calendar would just render based on what's in the list on the right.