Example of Modifying Your Object to Validate Assessments

Use this example to learn how to modify your account, lead, contact, or opportunity to validate that a specified assessment template is completed, using Groovy scripts in Application Composer.

In this example, you're a sales administrator and you want to ensure that sales representatives complete the assessment template Discount Eligibility before they set an opportunity to Won.

Validating Assessment Using Scripting

To ensure that sales representatives always fill in the Discount Eligibility Assessment template before closing an opportunity:
  1. Sign in as a sales administrator.

  2. Create and activate a sandbox.

  3. Navigate to Application Composer.

  4. From the Applications list, select Sales.

  5. Expand Standard Objects, and then expand Opportunity.

  6. Click Server Scripts.

  7. On the Server Scripts Opportunity page, select Triggers.

  8. In the Object Triggers region, click the Add a new Trigger icon.

  9. On the Create Object Trigger page, select Before Update in Database from the Trigger list.

  10. Enter a trigger name.

  11. Enter the following script in the Trigger Definition region.

    def optyStatusCode = getAttribute('StatusCode')
    if (optyStatusCode == 'WON') {
          
      def id = getAttribute('OptyId')
      def vo = newView('AssessmentVO')
      def vc = vo.createViewCriteria()
      def vcr = vc.createRow()
      def vci = vcr.ensureCriteriaItem('AssessedObjectId')
      vci.setOperator('=')
      vci.setValue(id)
      vc.add(vcr)
      vo.appendViewCriteria(vc)
      vo.executeQuery()
      def completedMandatoryAsmnt = false;
      
      if (vo.getEstimatedRowCount() > 0) {
        vo.reset()
        while (vo.hasNext()) {
          def row = vo.next()
          if (row != null) {
            def tempName = row.getAttribute('TemplateName')    
           
            if (tempName == 'Discount Eligibility') {
                def status = row.getAttribute('StatusCode')  
                println("Asmnt Status is " + status)
                if(status != 'COMPLETED') {     
                    throw new oracle.jbo.ValidationException('Please complete the Mandatory Discount Eligibility Assessment before changing status to WON')
                } else {
                    completedMandatoryAsmnt = true;
                    break;
                }
             }
           }
         }
      }
      if (completedMandatoryAsmnt == false) {
          throw new oracle.jbo.ValidationException('Please create and complete Mandatory Discount Eligibility Assessment before changing status to WON')
      }  
    }
    
  12. Click Save and Close.

    The trigger script displays a message whenever a sales representative changes the opportunity status to Won. This message ensures that the sales representative fills in the Discount Eligibility assessment template before changing the status to Won.