Iterate through List and get Form Rating value

Hi all,
I’ve just spent several hours solving a couple of problems that needed piecing together bits from other topics so I thought I would share in case others find it useful.
For my specific use case I needed to:-

  • Iterate through items in a List to check a field in each list item and calculate a total value (in my case, if any of the individual list items are “Rejected” then everything is “Rejected”
  • Get the value of a 5 star rating control every time it changed to update a form field
  • Redirect the form onSubmit to an “Accept” page or a “Reject” page depending on the overall outcome
<script>
const recordId = (new URLSearchParams(window.location.search)).get('recordId');
var isRejected = false ;
var deliverableRejection = false ;
var ratingRejection = false ;
const errorLogging = false ;

window.addEventListener('block-loaded-form1', () => { 
    window.addEventListener('submit-form-success-form1', (e) => {
        setTimeout(() => {
            window.location.href = isRejected ? `/statements-of-work-approvals-deliverables-rejection?recordId=${recordId}` : '/' ;
        }, 1000);
    });
});
window.addEventListener('block-loaded-sow-deliverables-for-approval', () => {
	window.addEventListener('get-records-sow-deliverables-for-approval', calculateDeliverableRejection);
});

// Wait for the rating form input to be rendered then add onChange events
const ratingListener = setInterval(function () {
    if (document.getElementById(':r1:') != null) {
        clearInterval(ratingListener);
        for(let r=1;r<6;r++) {
            document.getElementById(':r'+r+':').addEventListener("change", calculateRatingRejection);
        }
    }
},100);

function calculateDeliverableRejection(e) {
    log ("calculateDeliverableRejection : onEntry : " + deliverableRejection) ;
    var deliverables = e.detail ;
    var thisDeliverableRejection = false ; 
    for(let i=0;i<deliverables.length;i++) {
        var thisApprovalStatus = deliverables[i].fields['Client Approved'];
        if (thisApprovalStatus == 'Pending Rejection') thisDeliverableRejection = true ;
    }
    deliverableRejection = thisDeliverableRejection ;
    log ("calculateDeliverableRejection : onExit : " + deliverableRejection) ;
    updateRejectionStatus();
}

function calculateRatingRejection() {
    log ("calculateRatingRejection : onEntry : " + ratingRejection) ;
    var formFields = document.forms[0].elements ;
    ratingRejection = ( formFields[formFields[6].name].value < 4 ) ;
    log ("calculateRatingRejection : onExit : " + ratingRejection) ;
    updateRejectionStatus();
}

function updateRejectionStatus() {
    isRejected = ( deliverableRejection || ratingRejection ) ;
    var rejectionStatus = isRejected ? 'Pending Rejection' : 'Approved' ;
    const updateFields = new CustomEvent('update-fields-form1', {
        detail: {
            'Status': rejectionStatus
        }
    });
    window.dispatchEvent(updateFields);
}

function log(msg) {
    if (errorLogging) console.log(msg);
}
</script>

I hope others find this example useful if you have to create custom code to solve similar issues.

1 Like

Thanks a lot for sharing @markpratt :slight_smile:

1 Like