Policy Automation for B2C Service

Get Involved. Join the Conversation.

Topic

    Scott Heidenreich
    Seed Data parameter isn't workingAnswered
    Topic posted March 11, 2019 by Scott HeidenreichBronze Trophy: 5,000+ Points 
    160 Views, 7 Comments
    Title:
    Seed Data parameter isn't working
    Summary:
    How to get the seed_data parameter to work
    Content:

    I have a customer portal php page that is parsing out the seed data parameter from a URL.

    The code is below.

    The seed_data parameter is receiving a string called existing_i_id=123456, where 123456 is the incident ID.

    The global attribute in OPA is the existing incident id, named as existing_i_id and set to map in from a <url parameter>.

    However, the model will not load using the seed data.  I even tried hard coding the seed data into the php page and still the model won't load it.  The model will start without data if I do not use the seed data parameter in the call.

    URL example: https://ohioepa--tst1.custhelp.com/app/OPAM/opa_deployment/NewfromExisting/preload/SeedData/i_id/17937/seedData/existing_i_id%3D17937

    <rn:meta title="#rn:msg:SHP_TITLE_HDG#" template="standard.php" login_required="true" force_https="true" clickstream="opa"/>
    <?php
        $incidentID = \RightNow\Utils\Url::getParameter('i_id');
     $OPAModel = \RightNow\Utils\Url::getParameter('opa_deployment');
     $startingData = \RightNow\Utils\Url::getParameter('seedData');
     $preloadCode = \RightNow\Utils\Url::getParameter('preload');
    ?>
    <div>
     <?php If ($preloadCode == "IncidentID"):
     echo LoadExisting?>
     
      <rn:widget path="custom/opa/OPAWidget"
       policy_model=#rn:php:$OPAModel#
       init_id=#rn:php:$incidentID#
      />
     
     <?php elseif ($preloadCode == "SeedData"):
     echo NewFromExisting;
       echo $startingData;
       echo $OPAModel;
       echo $incidentID?>
     
      <rn:widget path="custom/opa/OPAWidget"
       policy_model=#rn:php:$OPAModel#
       seed_data=#rn:php:$startingData#
      /> 
        
     <?php else:
     echo NewOnly?>
       
      <rn:widget path="custom/opa/OPAWidget"
       policy_model=#rn:php:$OPAModel#
      />
     <?php endif ?>
    </div>

    Best Comment

    Scott Berry

    Hi Scott,

    The reason why the 1.2 version widget works is because it embeds the interview in an iframe and uses whatever value specified for seed_data as a URL parameter. The 1.3 widget however now uses JavaScript to start the interview, so whatever seed_data is must be a perfectly formed JSON object. We have written previously that the parameter must be JSON, however because the value was never used by JavaScript and directly to a Java backend it only needed to be JSON-ish. Now, there is no flexibility because JavaScript will only take properly formed JSON, so lets fix that:

    At the moment you're specifying a parameter called seedData with the value "existing_i_id=123456" but really you should be specifying a parameter called existing_i_id with the value 123456. The URL specified should be [your OSvC page]/existing_i_id/123456.

    The page code should be collecting all relevant parameters in variables named the same as the parameter, rather than having a value which contains the parameter name and value concatenated. Note: the getParameter function returns a string value, so we also need to ensure our variables get the correct type, i.e.
        $numberParam = intval(\RightNow\Utils\Url::getParameter('numberParam'), 10);

    You should open the page source to see what the rendered script code was if something isn't working. Search in the source for "var seedData".
    If you were to specify a static and escaped value for the seed_data parameter in php that was invalid content for a JSON, i.e. seed_data="'oops!'" you would see an error message generated by the widget on screen along the lines of "Seed data JSON object is invalid: oops! is not defined"
    However #rn:php:[variable name]# outputs the string value unquoted, so the URL /seedData/existing_i_id%3D17937 will render the page source with:
        var seedData = {existing_i_id=17937};
    This is not just invalid JSON, it is also an attempt to assign a parameter called 'existing_i_id' so the rendered widget script fails before the point of validating the JSON. If you load the page with Chrome Developer tools open, you'll see a message in the Console: Uncaught SyntaxError: Invalid shorthand property initializer.

    PHP provides an easy method to convert php class/variables into a PHP JSON class: json_encode which we use once all parameters have been collected in an object (using the same structure as we want in the final JSON.

    TLDR;

    URL:
        [your OSvC page]/stringParam/fred%20smith/numberParam/123456/booleanParam/false

    PHP Page:
        <?php
            $seedData = new \stdClass();
            $seedData->stringParam = \RightNow\Utils\Url::getParameter('stringParam');
            $seedData->numberParam = intval(\RightNow\Utils\Url::getParameter('numberParam'), 10);
            // the '10' parameter ensures the number is interpreted as base 10
            $seedData->booleanParam = \RightNow\Utils\Url::getParameter('booleanParam') === 'true';
            $seedJson = json_encode($seedData);
        ?>
        <rn:widget path="custom/opa/OPAWidget/"
            policy_model="WidgetParamsInterview"
            seed_data=#rn:php:$seedJson#
        />

    Check output HTML:
        var seedData = {"stringParam":"fred smith","numberParam":123456,"booleanParam":false};


    I'll endeavour to include an example like this in the Widget Upgrade documentation (which currently just highlights that VALID json be supplied but doesn't supply an example of how to do this using PHP and the OSvC specific parameter functions)

    Comment

     

    • Matt Sevin

      The URL does not look correct for seeding data.  Either JSON format or single attribute via URL encoded format ending in ?attrname=attr_value is what is supported. See this topic fpr more details and examples: http://documentation.custhelp.com/euf/assets/devdocs/unversioned/PolicyAutomation/en/Default.htm#Guides/Developer_Guide/Web_Interviews/Integration/Launch_an_interview.htm

       

      • Scott Heidenreich

        The format is for a php page from the customer portal to interpret the value of the seedData parameter.  Which is translated as:

        seed_data=existing_i_id%3D12345

        Even if I directly type the above literals into the php page, it still does not load incident 12345.

    • Matt Sevin

      Are you receiving any errors? : http://documentation.custhelp.com/euf/assets/devdocs/unversioned/PolicyAutomation/en/Default.htm#Guides/Developer_Guide/Service_Cloud/Test_the_OPA_widget.htm

      Verify you are using the correct / latest widget version: http://documentation.custhelp.com/euf/assets/devdocs/unversioned/PolicyAutomation/en/Default.htm#Guides/Developer_Guide/Service_Cloud/Upgrade_the_OPA_widget.htm

      Init_id should refer to an additional input table when the policy model data mappings are set to load the contact and an additional input table (e.g. incident)
      (a logged in user is required and must have permissions to load the corresponding objects at the start of the interview)

      seed_data should be JSON format for additional data (beyond the contact and related input table to load at start).

      • Scott Heidenreich

        I am not getting any errors.  The model simply doesn't start.

        I am using the V3 1.3 OPA widget.

        Init_id isn't part of this problem, as it is not a widget parameter being used when the widget call is using the the seed_data parameter.

        as far as I know this is a properly constructed json format per the documentation:

        seed_data=existing_i_id%3D12345

    • Scott Heidenreich

      I reverted my Customer Portal back to OPA widget v3 1.2 and the models that use seed_data work properly.  When I set the CP to use OPA widget v3 1.3 these models no longer work.

      This appears to be a bug in the OPA widget v3 1.3

    • Scott Berry

      Hi Scott,

      The reason why the 1.2 version widget works is because it embeds the interview in an iframe and uses whatever value specified for seed_data as a URL parameter. The 1.3 widget however now uses JavaScript to start the interview, so whatever seed_data is must be a perfectly formed JSON object. We have written previously that the parameter must be JSON, however because the value was never used by JavaScript and directly to a Java backend it only needed to be JSON-ish. Now, there is no flexibility because JavaScript will only take properly formed JSON, so lets fix that:

      At the moment you're specifying a parameter called seedData with the value "existing_i_id=123456" but really you should be specifying a parameter called existing_i_id with the value 123456. The URL specified should be [your OSvC page]/existing_i_id/123456.

      The page code should be collecting all relevant parameters in variables named the same as the parameter, rather than having a value which contains the parameter name and value concatenated. Note: the getParameter function returns a string value, so we also need to ensure our variables get the correct type, i.e.
          $numberParam = intval(\RightNow\Utils\Url::getParameter('numberParam'), 10);

      You should open the page source to see what the rendered script code was if something isn't working. Search in the source for "var seedData".
      If you were to specify a static and escaped value for the seed_data parameter in php that was invalid content for a JSON, i.e. seed_data="'oops!'" you would see an error message generated by the widget on screen along the lines of "Seed data JSON object is invalid: oops! is not defined"
      However #rn:php:[variable name]# outputs the string value unquoted, so the URL /seedData/existing_i_id%3D17937 will render the page source with:
          var seedData = {existing_i_id=17937};
      This is not just invalid JSON, it is also an attempt to assign a parameter called 'existing_i_id' so the rendered widget script fails before the point of validating the JSON. If you load the page with Chrome Developer tools open, you'll see a message in the Console: Uncaught SyntaxError: Invalid shorthand property initializer.

      PHP provides an easy method to convert php class/variables into a PHP JSON class: json_encode which we use once all parameters have been collected in an object (using the same structure as we want in the final JSON.

      TLDR;

      URL:
          [your OSvC page]/stringParam/fred%20smith/numberParam/123456/booleanParam/false

      PHP Page:
          <?php
              $seedData = new \stdClass();
              $seedData->stringParam = \RightNow\Utils\Url::getParameter('stringParam');
              $seedData->numberParam = intval(\RightNow\Utils\Url::getParameter('numberParam'), 10);
              // the '10' parameter ensures the number is interpreted as base 10
              $seedData->booleanParam = \RightNow\Utils\Url::getParameter('booleanParam') === 'true';
              $seedJson = json_encode($seedData);
          ?>
          <rn:widget path="custom/opa/OPAWidget/"
              policy_model="WidgetParamsInterview"
              seed_data=#rn:php:$seedJson#
          />

      Check output HTML:
          var seedData = {"stringParam":"fred smith","numberParam":123456,"booleanParam":false};


      I'll endeavour to include an example like this in the Widget Upgrade documentation (which currently just highlights that VALID json be supplied but doesn't supply an example of how to do this using PHP and the OSvC specific parameter functions)

    • Scott Heidenreich

      Thank you for the detailed answer.  I appreciate it.  An example in the documentation would be fantastic!  I was able to get this to work using the information you provided above.