0

The following code is my javascript that i am trying to run(without the imports or initial variable declarations i.e. imported functions used and variables like triggerStatus shown below were already declared but not pasted). The error that i get when it runs is Error Running Script [scriptToRun.js]:com.sun.phobos.script.util.ExtendedScriptException: org.mozilla.javascript.EvaluatorException: missing ; after for-loop initializer (<Unknown source>#49) in <Unknown source> at line number 49

function runScript() {
  let (survey = newDocument,
    statusQuery = new AeSpcsrvySDTO()) {
    logger.fine('Space survey [' + survey.spaceSurvey + ']');
    // Look up the status
    statusQuery.statusCode = survey.statusCode;
    let (statuses = new StatusServiceImpl(SystemContext.getInstance()).findByDTO(statusQuery, null)) {
      let (status = statuses.get(0)) {
        logger.fine('Space survey [' + survey.spaceSurvey + ']'
                      + ' status [' + survey.statusCode + ']'
                      + ' completeYn [' + AeSpcsrvySCompleteYN.valueOfFromCode(status.completeYn).name() + ']');
        if (status.completeYn != triggerStatus.code) {
          logger.fine('Ignoring space survey [' + survey.spaceSurvey + ']');
          return;
        }
        logger.fine('Space survey [' + survey.spaceSurvey + ']'
                      + ' has ' + survey.aeSpcsrvyLocDTOList.size() + ' locations');
        for (let location in Iterator(survey.aeSpcsrvyLocDTOList)) {
          logger.fine('Space survey [' + location.spaceSurvey + ']'
                        + ' region [' + location.regionCode + ']'
                        + ' facility [' + location.facId + ']'
                        + ' property [' + location.bldg + ']'
                        + ' location [' + location.locId + ']');
          let (highestUsagePercent = BigDecimal.ZERO,
               highestUsage = null) {
            for (let occupancy in Iterator(location.aeSpcsrvyOccDTOList)) {
              logger.fine('Space survey [' + occupancy.spaceSurvey + ']'
                            + ' region [' + occupancy.regionCode + ']'
                            + ' facility [' + occupancy.facId + ']'
                            + ' property [' + occupancy.bldg + ']'
                            + ' location [' + occupancy.locId + ']'
                            + ' seq [' + occupancy.seq + ']'
                            + ' percent [' + occupancy.percentOccupancy + ']');
              let (proration = occupancy.percentOccupancy.movePointLeft(2)) {
                for (let usage in Iterator(occupancy.aeSpcsrvyOccUsgDTOList)) {
                  logger.fine('Space survey [' + usage.spaceSurvey + ']'
                                + ' region [' + usage.regionCode + ']'
                                + ' facility [' + usage.facId + ']'
                                + ' property [' + usage.bldg + ']'
                                + ' location [' + usage.locId + ']'
                                + ' seq [' + usage.seq + ']'
                                + ' sub seq [' + usage.subSeq + ']'
                                + ' usage code [' + usage.usageCode + ']'
                                + ' percent [' + usage.usagePercent + ']');
                  if (usage.usageCode && usage.usagePercent
                    // There's a validation for <= 100, but not for > 0.
                    && usage.usagePercent.compareTo(BigDecimal.ZERO) > 0) {
                    let (proratedUsagePercent = usage.usagePercent.multiply(proration)) {
                      if (proratedUsagePercent.compareTo(highestUsagePercent) > 0
                        || (proratedUsagePercent.compareTo(highestUsagePercent) == 0
                        && usage.usageCode.compareTo(highestUsage.usageCode) < 0)) {
                        highestUsage = usage;
                        highestUsagePercent = proratedUsagePercent;
                        logger.fine('New highest usage percent is [' + highestUsagePercent + ']');
                      }
                    }
                  }
                }
              }
            }
            if (highestUsage) {
              // Look up the location and update the usage.
              let (locationKey = new AeBLocDPK(highestUsage.regionCode, highestUsage.facId,
                                               highestUsage.bldg, highestUsage.locId),
                   serviceImpl = new PropertyServiceImpl(SystemContext.getInstance())) {
                let (location = serviceImpl.findByPrimaryKey(locationKey, false)) {
                  location.usageCode = highestUsage.usageCode;
                  serviceImpl.save(location, false, false);
                }
              }
            }
          }
        }
      }
    }
  }
}

Looking at the specified line 49 and surrounding for loop syntax, i do not see the issue, am i missing something?

thanks

update changes using AP.'s suggestions i get a new error: current code(error line 3: Missing ; before statement):

function runScript() {
    'use strict';
    let survey = newDocument,
        statusQuery = new AeSpcsrvySDTO();
    logger.fine('Space survey [' + survey.spaceSurvey + ']');
    // Look up the status
    statusQuery.statusCode = survey.statusCode;
    let statuses = new StatusServiceImpl(SystemContext.getInstance()).findByDTO(statusQuery, null);
    let status = statuses.get(0);
    logger.fine('Space survey [' + survey.spaceSurvey + ']' +
        ' status [' + survey.statusCode + ']' +
        ' completeYn [' + AeSpcsrvySCompleteYN.valueOfFromCode(status.completeYn).name() + ']');
    if (status.completeYn != triggerStatus.code) {
        logger.fine('Ignoring space survey [' + survey.spaceSurvey + ']');
        return;
    }
    logger.fine('Space survey [' + survey.spaceSurvey + ']' +
        ' has ' + survey.aeSpcsrvyLocDTOList.size() + ' locations');
    for (let location in Iterator(survey.aeSpcsrvyLocDTOList)) {
        logger.fine('Space survey [' + location.spaceSurvey + ']' +
            ' region [' + location.regionCode + ']' +
            ' facility [' + location.facId + ']' +
            ' property [' + location.bldg + ']' +
            ' location [' + location.locId + ']');
        let highestUsagePercent = BigDecimal.ZERO,
            highestUsage = null;
        for (let occupancy in Iterator(location.aeSpcsrvyOccDTOList)) {
            logger.fine('Space survey [' + occupancy.spaceSurvey + ']' +
                ' region [' + occupancy.regionCode + ']' +
                ' facility [' + occupancy.facId + ']' +
                ' property [' + occupancy.bldg + ']' +
                ' location [' + occupancy.locId + ']' +
                ' seq [' + occupancy.seq + ']' +
                ' percent [' + occupancy.percentOccupancy + ']');
            let proration = occupancy.percentOccupancy.movePointLeft(2);
            for (let usage in Iterator(occupancy.aeSpcsrvyOccUsgDTOList)) {
                logger.fine('Space survey [' + usage.spaceSurvey + ']' +
                    ' region [' + usage.regionCode + ']' +
                    ' facility [' + usage.facId + ']' +
                    ' property [' + usage.bldg + ']' +
                    ' location [' + usage.locId + ']' +
                    ' seq [' + usage.seq + ']' +
                    ' sub seq [' + usage.subSeq + ']' +
                    ' usage code [' + usage.usageCode + ']' +
                    ' percent [' + usage.usagePercent + ']');
                if (usage.usageCode && usage.usagePercent
                    // There's a validation for <= 100, but not for > 0.
                    &&
                    usage.usagePercent.compareTo(BigDecimal.ZERO) > 0) {
                    let proratedUsagePercent = usage.usagePercent.multiply(proration);
                    if (proratedUsagePercent.compareTo(highestUsagePercent) > 0 ||
                        (proratedUsagePercent.compareTo(highestUsagePercent) == 0 &&
                            usage.usageCode.compareTo(highestUsage.usageCode) < 0)) {
                        highestUsage = usage;
                        highestUsagePercent = proratedUsagePercent;
                        logger.fine('New highest usage percent is [' + highestUsagePercent + ']');
                    }

                }
            }

        }
        if (highestUsage) {
            // Look up the location and update the usage.
            let locationKey = new AeBLocDPK(highestUsage.regionCode, highestUsage.facId, highestUsage.bldg, highestUsage.locId),
                serviceImpl = new PropertyServiceImpl(SystemContext.getInstance());
            let location = serviceImpl.findByPrimaryKey(locationKey, false);
            location.usageCode = highestUsage.usageCode;
            serviceImpl.save(location, false, false);
        }
    }
}
2
  • Could consider using .forEach like survey.aeSpcsrvyLocDTOList.forEach(element => {do something;}) Commented Mar 3, 2017 at 20:44
  • Could you format your code with: jsbeautifier.org Commented Mar 6, 2017 at 18:31

1 Answer 1

1

Not sure what you are trying to achieve with this block:

 let (proratedUsagePercent = usage.usagePercent.multiply(proration)) {
                      if (proratedUsagePercent.compareTo(highestUsagePercent) > 0
                        || (proratedUsagePercent.compareTo(highestUsagePercent) == 0
                        && usage.usageCode.compareTo(highestUsage.usageCode) < 0)) {
                        highestUsage = usage;
                        highestUsagePercent = proratedUsagePercent;
                        logger.fine('New highest usage percent is [' + highestUsagePercent + ']');
                      }
                    }

You cannot add a block in front of a let expression, maybe change that to:

let proratedUsagePercent = usage.usagePercent.multiply(proration)
if (proratedUsagePercent.compareTo(highestUsagePercent) > 0
                    || (proratedUsagePercent.compareTo(highestUsagePercent) == 0
                    && usage.usageCode.compareTo(highestUsage.usageCode) < 0)) {
                    highestUsage = usage;
                    highestUsagePercent = proratedUsagePercent;
                    logger.fine('New highest usage percent is [' + highestUsagePercent + ']');
                  }

Also: Maybe your browser may not support a let declaration outside strict mode. Replace it with var or add 'use strict;' (with the quotes) to the top of your function.

Sign up to request clarification or add additional context in comments.

8 Comments

Trying AP.'s suggestions my error message moved up to line 29: missing ; before statement (<Unknown source>#29) in <Unknown source> at line number 29 and i thought it might be the same issue as the one you pointed out with the cannot add a block in front of a let expression so i tried something similar at the line 29 block
I see + ' facility [' + occupancy.facId + ']' on line 29
oh sorry forgot to convert the line number, it would be line 2 in the code above: let (survey = newDocument, statusQuery = new AeSpcsrvySDTO()) {
Yeah that's going to become: let survey = newDocument, statusQuery = new AeSpcsrvySDTO() And the stuff in {} on a new line, without the {}
after changing that line and trying a few other things, i still get the new error of the missing ; before statement on that line 2 above, both with and without the { } i tried even adding the semi colon to the end of that let statement
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.