PLM 360 General Discussion

PLM 360 General Discussion

Reply
Active Contributor
ForrestJudd
Posts: 38
Registered: ‎04-28-2008
Message 1 of 3 (109 Views)

On Create / On Edit script conflict

109 Views, 2 Replies
11-15-2012 05:31 PM

I spent a frustrating few hours today troubleshooting something that turned out to be kind of bone-headed.  I thought I'd share what I learned, and also ask if there's a better way to avoid this problem than what I've currently rigged up.

 

I have a script that updates some item details based on other fields in the workspace.  I want this script to run both when the item is first created and when the item is edited.  To accomplish this, I set the same script to run upon both behaviors.

 

That ultimately caused an inability to create new records in the workspace due to a scripting error.  Howver, edits to existing records in the workspace behaved just as expected.  The script was logging the following error:

 

[GetCIRecordInfo] java.lang.RuntimeException: Unable to save script results

 

This should have occurred to me much sooner.  Eventually I figured out that I was starting a race condition when the script was running On Create as soon as the script edited an item detail.  Removing the script from the On Edit behavior immediately resolved the problem of creating new records.  

 

What I've now landed on as a solution is to have this update script run only On Edit.  To trigger it during item creation, however, I have a second script that runs On Create that just puts a dummy value into one of the item fields the real script will eventually update.  

 

Is there a way in just the single script I can prevent this error?  I didn't test it, but even if I had two different scripts set to each behavior, they'd still both execute, and they'd both be executing the same set of statements.  At best there would be duplication of work and the accompanying overhead; at worst, I'd have the same race condition with the same problem results.

Product Support
piggeej
Posts: 281
Registered: ‎03-27-2012
Message 2 of 3 (100 Views)

Re: On Create / On Edit script conflict

11-16-2012 07:55 AM in reply to: ForrestJudd

Hi Forest,

 

Would you mind posting the script so I can look it over?



Joe Piggee
Support Specialist
Product Support
Autodesk, Inc.

Active Contributor
ForrestJudd
Posts: 38
Registered: ‎04-28-2008
Message 3 of 3 (97 Views)

Re: On Create / On Edit script conflict

11-16-2012 08:19 AM in reply to: piggeej

Joe,

 

Here's the script:

 

if (item.ADD_NEW_ADDRESS == "0")

{
    // Load the record that contains a list of all Customer Information record dmsIDs
    var customerList = loadItem(874);
   
    // Build array of dmsIDs from the single pipe delimited string
    var customerIDs = customerList.RECORDSLIST.split("|");
    var customerInformationRecord;

    // iterate through each dmsID
    for (i=0; i <customerIDs.length; i++)
    {
        try
        {
            
         // Get the first record
          var custItem = loadItem(customerIDs[i]);
      
          // Check against contact first - this is the most likely unique identifier
          // and so will prevent later checks upon failure and speed up script operation
          if (custItem.CONTACT_NAME == item.CONTACT)
          
          {
           // Check against address
            if (custItem.LOCATION == item.SHIPPING_ADDRESS)
             {
                // Check against customer ID.  
                if (custItem.CUSTOMER == item.CUSTOMER_ID)
                {
                    customerInformationRecord = custItem;
                    break;
                }    
           }
       }
    
    }
    catch (err)
    {
       
    }
}


// Now that we have the customer information record set all of the
// item details based on the CI properties

if (customerInformationRecord !== null)
{
    item.ADDRESS1 = customerInformationRecord.ADDRESS1;
    item.ADDRESS2 = customerInformationRecord.ADDRESS2;
    item.ADDRESS3 = customerInformationRecord.ADDRESS3;
    item.CITY = customerInformationRecord.CITY;
    item.STATE = customerInformationRecord.STATE;
    item.ZIP_CODE = customerInformationRecord.ZIP_CODE;
    item.CONTACT_PHONE = customerInformationRecord.PHONE;
    item.EMAIL = customerInformationRecord.EMAIL;
}

}

 

This script is finding a specific record in a particular workspace based on the values of three filtered picklists on the current record.  It's being used to pull additional information into the current record about an address and contact information based on a location description and contact name.

 

I spent quite awhile testing this yesterday, and was able to reproduce the problem with a significantly simpler version of the script:

 

 // MODIFYING THE CURRENT ITEM'S DETAILS HERE AT THE BEGINNING OF THE SCRIPT
    //  (UNCOMMENTING THE LINE BELOW) DOES *NOT* TRIGGER THE ERROR
    // item.ADDRESS1 = 'test';
    

    // Load the record that contains a list of all Customer Information record dmsIDs
    var customerList = loadItem(874);
   
    // Build array of dmsIDs from the single pipe delimited string
    var customerIDs = customerList.RECORDSLIST.split("|");
    var customerInformationRecord;

    // iterate through each dmsID
    for (i=0; i <customerIDs.length; i++)
    {
       
            
         // Get the first record
          var custItem = loadItem(customerIDs[i]);
      
          // Check against contact first - this is the most likely unique identifier
          // and so will prevent later checks upon failure and speed up script operation
          if (custItem.CONTACT_NAME == item.CONTACT)
          
          {
           // Check against address
            if (custItem.LOCATION == item.SHIPPING_ADDRESS)
             {
                // ATTEMPTING TO MODIFY THE CURRENT ITEM'S DETAILS
		// (UNCOMMENTING THE LINE BELOW) TRIGGERS THE ERROR
                //  item.ADDRESS1 = 'test';
             }
      
       
           }
      }

 

I was definitely seeing variable behavior with the above simplified script.  It actually did work once during new record creation.  Every other attempt to edit item details inside the second conditional inside the for loop triggered the error.  The script itself was and is functional, so long as it's not set to run both on item create and item details edit.

 

I'm having another issue with a different script now that I think may also be related to timing and order of operations.  I'm still doing some testing with that, but I think the problem is related to when an item's 'on create' script executes as a result of calling the createNewItem library function.

 

Of course now I just went back to review that sript, and its contents have somehow vanished....sigh.  That was a complex script, and I'm sure I didn't delete it.  The script is still there, as are the comments at the top of the script describing its operation, but all of the code is gone.

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Do you have 60 seconds to spare? The Autodesk Community Team is revamping our site ranking system and we want your feedback! Please click here to launch the 5 question survey. As always your input is greatly appreciated.