Set string field value when another field is populated

I'm trying to do something very simple in SharePoint forms, but am having a hell of a time trying to get it to work. On one field being populated (field is PV), I'm trying to update another field (Status) to state of "Complete". For whatever reason, my code is failing. Can someone take a quick look for me?

fd.spRendered(function() {
function setComplete() {
    var pvLen = fd.field('PV').value;
    var statusComplete = fd.field('Status').value.name;
    if (pvLen.length > 1) {
        // check if PV is blank
        statusComplete = 'Complete';
    } 
    else {
    }
};

// Calling SetComplete when the user changes Product Field
fd.field('PV').$on('change',setComplete);

// Calling SetComplete on form loading
setComplete();
});

Dear @Gabegia,
What's the type of the Status field? If it's text, have you tried it like this?

fd.spRendered(function() {
  function setComplete() {
    var pv = fd.field('PV').value;
    if (pv.length >= 1) {
        // check if PV is blank
        fd.field('Status').value = 'Complete';
    } 
    else {
    }
  };

  // Calling SetComplete when the user changes Product Field
  fd.field('PV').$on('change',setComplete);

  // Calling SetComplete on form loading
  setComplete();
});
1 Like

Hi Nikita -
Status is a SharePoint drop-down selection string text field, and PV is just a text string field.

I tried a copy/paste of your update and it's still not functioning properly (and is still breaking the other JS I have on the form) so I'm guessing that I've got a typo in there somewhere. Do I need to use fd.field('Status').value.name for SharePoint selection fields, or should I just be able to use the .value?

Thanks for the help!

Dear Gabegia,
I recommend testing code in pieces, for example, for now you can remove the rest of the code (comment out or copy some place else) and just test this piece. Then, if there's an error, you can check browser's console and get at least some information about it. You can share a screenshot of the error here, so we can help adjust the code.

Regarding setting values to various SP fields, please, refer to this article - Managing SharePoint fields with JS — SharePoint forms

1 Like

Apparently I had messed up some of the other JS while I was playing, which caused it to fail. I rolled it back to a previous version (after retrying & having it fail without the new code), then added it in.

It's all set - thanks again for the help as always! You guys rock!

1 Like