Update a field value when another field is edited

  • 1
  • Question
  • Updated 4 years ago
  • Answered
Photo of mB Pat Vachon

mB Pat Vachon, Champion

  • 42,714 Points 20k badge 2x thumb

Posted 4 years ago

  • 1
Photo of mB Pat Vachon

mB Pat Vachon, Champion

  • 42,714 Points 20k badge 2x thumb
^bump^ :D
Photo of Seth Vanderdrift

Seth Vanderdrift

  • 878 Points 500 badge 2x thumb
I think the new actions on your junction model could work.
I'm using an action to update a date/time field when another field is changed on the same mode.

I have one action, which updates a field to the current date.  There's a specific option for that.

In your case, I think you'll need a snippet, since your wanting to update one model field with a value from another model field.

I've seen samples around that do that, but I'm still new to Skuid so I can help with specifics.  I'm just sure there's lots of {} in it... :D

Seth
Photo of Rob Hatch

Rob Hatch, Official Rep

  • 44,006 Points 20k badge 2x thumb
I tend to agree with Seth.  This is not possible just using the action framework - in a table as you have. 

If you were able to get the process item in a model (by itself)  then you could add a new row to the junction object table and pass the default value for sequence to it.  I believe this would have to be a two step process.  1 being select the process - and filter the process model down to just that item,  and 2 populate the row in the junction object table. 

I also don't think you are going to be able to have this function well in the "edit existing" scenario without some javascript. 
Photo of mB Pat Vachon

mB Pat Vachon, Champion

  • 42,714 Points 20k badge 2x thumb
I was suspecting this. Basically think that I need to setup a listening function. Not even sure that's what it is called.

On change, take the id of the reference and update a model condition with it in order to get access the desired data. Then I can update the second field.
(Edited)
Photo of mB Pat Vachon

mB Pat Vachon, Champion

  • 42,714 Points 20k badge 2x thumb
It's the listening part that I'm new to. Once I have the Id, then I'm good to go.
Photo of Greg Jarrett

Greg Jarrett

  • 3,496 Points 3k badge 2x thumb
We got a similar thing working from this post - check out the first half of Ben's answer. Admittedly I haven't tried running this on a reference field, but what it basically does is:

When 'this_field__c' is updated, then get another value from the same row, and populate it into 'second__field__c'

var params = arguments[0],	
$ = skuid.$,
updates = params.updates,
defaultSequence;
//if the process reference field is updated by the user,
//then get the value of default sequence field for that row
//check that field is being pulled into the Process Model in your page
if ('Process__c' in updates) {
    defaultSequence = params.row.defaultSequence__c;
    }
    if (defaultSequence) {
        params.model.updateRow(params.row,'defaultSequence__c',defaultSequence);  
   }

See if you have any joy with that!

 
(Edited)
Photo of Greg Jarrett

Greg Jarrett

  • 3,496 Points 3k badge 2x thumb
this also needs you to create an action on the model - when model is updated, then run the snippet.

Also, as it is a reference field, this line:

params.row.defaultSequence__c;

might need to be more like:

params.row.Process__r.defaultSequence__c;
(Edited)
Photo of mB Pat Vachon

mB Pat Vachon, Champion

  • 42,714 Points 20k badge 2x thumb
Greg. You are the man! That was exactly what I was looking for. This is the code I ended up with.


var params = arguments[0],
	$ = skuid.$,
    updates = params.updates;
    
    // if the process reference field is updated by the user, 
    // then get the value of default sequence field for that row
    // check that field is being pulled into the Process Model in your page
    
    if ('RECON__Process__c' in updates) {
        var processModel = skuid.$M('CurrentLibProcess'),
            proModCond = processModel.getConditionByName('ProId'),
            defaultSequence;
            
            // set and activate condition
            processModel.setCondition(proModCond,params.row.RECON__Process__c);
            processModel.activateCondition(proModCond);
            // query model and wait til done before getting row and updating row
            $.when(processModel.updateData())
                .done(function(){
                    processRow = processModel.getFirstRow();
                    defaultSequence = processRow.RECON__Sequence__c;
                    params.model.updateRow(params.row,'RECON__Sequence__c',defaultSequence);  
                })
                .fail(function(){
                    
                });
            

    }
Photo of Greg Jarrett

Greg Jarrett

  • 3,496 Points 3k badge 2x thumb
Nice. looks like you took it to another level there setting the conditions and querying the junction object.
Photo of mB Pat Vachon

mB Pat Vachon, Champion

  • 42,714 Points 20k badge 2x thumb
Yup. As far as I know I had to do that. Still new to Skuid (3 months) and Javascript (1 month)
Photo of Rob Hatch

Rob Hatch, Official Rep

  • 44,006 Points 20k badge 2x thumb
Well done... quickly passing all of up...