Mass action to update different fields depending on a condition

  • 1
  • Question
  • Updated 3 years ago
  • Answered
Hello,

I'm looking for a way to mass update a field on rows selected via mass actions, but depending on another field's value, to update a different field

Example:

If Type__c = 'New'  I want to update the field 'Date_Paid__c' (date/time) to "today"
If Type__c= 'Existing' I want to update the field 'Date_Paid_Existing__c' (date/time) to "today"


my table may contain a mix of both type, and we do not want to have to do it in 2 different actions.

I would like to be able to accomplish this from 1 Mass action.

Probably can be done with a snippet, so if anyone has ever attempted something similar, please let me know how, as i'm a JS noob :(

Thank you 
Photo of Dave

Dave

  • 5,538 Points 5k badge 2x thumb

Posted 3 years ago

  • 1
Photo of Rob Hatch

Rob Hatch, Official Rep

  • 44,006 Points 20k badge 2x thumb
Dave you are headed directly to the dark side.  This will require some Javascript to take care of. 
Photo of Dave

Dave

  • 5,538 Points 5k badge 2x thumb
That was what worried me ;sweat

In case, have you or anyone else ever done something similar? and if yes could you please share that snippet so I have a base to work with?


Thank you
Photo of JG GBC

JG GBC

  • 694 Points 500 badge 2x thumb
Hi Dave

Add this snippet as a mass action:
var params = arguments[0],
$ = skuid.$;

var model = params.model;

var list = params.list;

var selectedItem = params.item ? [params.item] : list.getSelectedItems();

$.each( selectedItem,
function( i, item )
{
row = item.row;

if(row.Type__c === 'New'){
model.updateRow(row,{
Date_Paid__c: new Date()
});
}else if(row.Type__c === 'Existing'){
model.updateRow(row,{
Date_Paid_Existing__c: new Date()
});
}
});

model.save();
Hope that helped...
Photo of Dave

Dave

  • 5,538 Points 5k badge 2x thumb
Thank you very much JG!,

It seems to work but I get an error when it's trying to save



I think it's my fault, As looking at post, I made a mistake, that field is of "Date" format and Not "Date/Time"

I tried couple different thing , i found on google to fix it, but does not seem to work , closest i got was adding this:

var today = new Date();
today.setHours(0, 0, 0, 0);

But also gave a similar error

What would i need to do for this snippet to input today without time?


Thank you again and sorry about that mistake I made



Photo of Rob Hatch

Rob Hatch, Official Rep

  • 44,006 Points 20k badge 2x thumb
I think you need to translate your "today" value from a javascript date time field into a legitimate salesforce date/time field .

Fortunately Skuid provides a utility for this.  

Add code that looks like this: 

var today = new Date();
today.setHours(0, 0, 0, 0);
var sf_today = skuid.time.getSFDateTime(today)

 Then pass the sf_today value into your update row declarations. 
Photo of Dave

Dave

  • 5,538 Points 5k badge 2x thumb
Ty Rob, but that gives me the same error.

As i explained on post earlier the fields to update are of "DATE" format and not "DATE/TIME".

So i tried to modify this to:

var today = new Date();
today.setHours(0, 0, 0, 0);
var sf_today = skuid.time.getSFDate(today) But same issue. I can see the proper date In field, but it will not allow saving, because i think it's trying to add the "time" to this date field I need to update with today's date only, not date/time Thx <img src="https://d2r1vs3d9006ap.cloudfront.net/s3_images/1390122/RackMultipart20160329-108259-2e5sm1-6_inline.JPG?1459265030" title="Image https//d2r1vs3d9006apcloudfrontnet/s3_images/1390122/RackMultipart20160329-108259-2e5sm1-6_inlineJPG1459265030">
Photo of JG GBC

JG GBC

  • 694 Points 500 badge 2x thumb
Try:
today = skuid.time.getSFDate(new Date());
That works fine for me...
(Edited)
Photo of Dave

Dave

  • 5,538 Points 5k badge 2x thumb
Ty JG for correct syntax,

now it works perfectly ! :)