Maximo Open Forum

 View Only
  • 1.  Delete certain records from E-Mail table with Automation Script

    Posted 05-18-2021 09:55
    Hi,
    I have a requirement to delete records from E-Mail table for users starting with certain prefix like 'GT'.
    I have written the following script but it doesn't seem to work. Could anyone help me in finding the mistake in the following script.
    from psdi.server import MXServer
    import sys
    mxServer=MXServer.getMXServer()
    userInfo=mxServer.getSystemUserInfo()
    emailset=mxServer.getMboSet("EMAIL",userInfo);
    emailset.setWhere("PERSONID like 'GT%'")
    if (emailset!= None or emailset.isEmpty()==False):
       G=0
       team=emailset.getMbo(G)
       while(team is not None):
           team.delete()
           emailset.save()
           G = G + 1
           team= emailset.getMbo(G)​
    Is there any other to write the script with SQL Query to achieve the requirement?
    #Customizations

    ------------------------------
    Harshavardhan Kamepalli
    Eidiko System Integrators
    ------------------------------


  • 2.  RE: Delete certain records from E-Mail table with Automation Script

    Posted 05-19-2021 09:46
    Can you try the following after your setWhere statement? 

    team= emailSet.moveFirst()
    while team:
         team.delete(MboConstants.NOACCESSCHECK | MboConstants.NOVALIDATION)  #you may have to import from psdi.mbo import MboConstants
         emailSet.save()
         team= emailSet.moveNext()

    You may also check the MXServer logs by activating autoscript logger  to see what messages the app may be reporting​.

    ------------------------------
    Pankaj Bhide
    Berkeley National Laboratory
    ------------------------------



  • 3.  RE: Delete certain records from E-Mail table with Automation Script

    Posted 05-20-2021 03:27
    Thanks Pankaj for taking your valuable time and replying for the issue. Will try this.

    ------------------------------
    Harshavardhan Kamepalli
    Eidiko System Integrators
    ------------------------------



  • 4.  RE: Delete certain records from E-Mail table with Automation Script

    Posted 05-19-2021 14:01
    A couple of things here. 

    1) After setting the where clause, you should call a reset (IE emailset.reset())
    2) You can bulk delete by utilizing emailset.deleteAll(). This avoids looping through the records to delete if you're not executing any additional logic. 
    3) Don't save a set more than once. In your example you're saving after each record.

    I'd write this more like:
    from psdi.server import MXServer
    mxServer=MXServer.getMXServer()
    userInfo=mxServer.getSystemUserInfo()
    emailset=mxServer.getMboSet("EMAIL",userInfo);
    emailset.setWhere("PERSONID like 'GT%'")
    emailset.reset()
    emailset.deleteAll()
    emailset.save()
    emailset.close()

    ------------------------------
    Steven Shull
    Projetech Inc.
    ------------------------------



  • 5.  RE: Delete certain records from E-Mail table with Automation Script

    Posted 05-20-2021 03:28
    Thanks Steven, This modified code looks super useful for the future scripts too.

    ------------------------------
    Harshavardhan Kamepalli
    Eidiko System Integrators
    ------------------------------



  • 6.  RE: Delete certain records from E-Mail table with Automation Script

    Posted 05-20-2021 08:15
    Hello Steve, - A quick questions for my clarifications

    1) Don't save a set more than once. In your example you're saving after each record. - Is this because if there are 5 records to be deleted and if an error occurs on the 3rd record, then we want to roll back the entire set or something else e.g. does save()  implicitly close or it reset the mboset? 

    2) Does the same rule apply for add() e.g. if I want to add 5 rows in the loop, I get a mbo from mboset by using add() method, then perform setValue on every newly added mbo. In this case, shall we perform save() only after adding all the rows? 


    ------------------------------
    Pankaj Bhide
    Berkeley National Laboratory
    ------------------------------



  • 7.  RE: Delete certain records from E-Mail table with Automation Script

    Posted 05-20-2021 08:43
    Save will be treated as a single transaction ensuring all succeed or all fail. It does not (entirely) reset after save, though does reset certain things such as removing any new mbos that were flagged to be deleted. And most importantly, it does release the database connection. It's this last part that has caused issues with us in the past where subsequent saves just won't work unless we refetch the set. 

    And yes, add or deletes should be treated the same. Just like in the UI where you save once, your code should save sets once. In the case that you have child sets (IE PO->POLINE), saving the PO will save the PO and the POLINE sets automatically. Though when you retrieve from MXServer you need to save (and close) each set since they're independent.

    ------------------------------
    Steven Shull
    Projetech Inc.
    ------------------------------