I am working with an IBM Maximo database that issues workorders automatically. When the workorders are issued (aka Inserted into the database workorder table), I would like to auto-assign a supervisor, owner, and owner group based on a set of criteria. This only needs to happen IF the supervisor, owner, and owner group aren't already assigned. Often times a "parent workorder" has the information, but it needs to be copied into the "child" workorders (as you will see in the criteria below). The criteria for ALL of the triggers is:
WHERE status<>'COMP'
AND historyflag=0
AND istask=0
Here is the criteria for the trigger:
-If the Owner Group and Supervisor have a value, skip the record. (Do nothing)
-If the Owner Group and/or Supervisor is blank or null, and the corresponding PARENT Work Order field is not Null, copy the Owner Group and/or Supervisor from the PARENT Work Order record.
-If the Parent Work Order Owner Group and/or Supervisor is blank or null, then assign the Owner Group and Supervisor per the table values below: (I have removed names for security's sake, but all the columns are correct, i.e. B3 is supposed to have SuperA as the supervisor)
Site / OwnerGroup / Supervisor
ABC / @ABCGroup / @ABCSupervisor
DEF / @DEFGroup / @DEFSupervisor
**NOTE: SITE is not a table column, it is really the first 3 characters of the workorder.location field. For example, the location could be ABC-1234, meaning it is at site ABC, building 1234 (unfortunately, these are NOT stored in separate columns, they are only present together in the location column). In this SQL query, all buildings at a location are serviced by the same ownergroup/supervisor, so all other queries we currently use are using workorder.location='ABC%'
I've done plenty of selects, updates, and stored procedures, but this is my first trigger and want to make sure I don't royally screw up the database! Any and all help is greatly appreciated!
For those unfamiliar with Maximo, the table is: dbo.workorder and the fields are: location,ownergroup,supervisor
UPDATE1: Here is some additional information that may be of importance. Locations: First off, workorder.location will contain values such as ABC-1234, meaning it is at site ABC, building 1234 (though these are NOT separate values, it's combined). In this SQL query, all buildings at a location are serviced by the same ownergroup/supervisor, so all queries use something similar to workorder.location='ABC%'.
Here is what I would like the logic to look like for the final query:
If the supervisor field is missing, first look to see if it has a parent, and if so, does the parent have a supervisor? If not, assign based on the table above.
If the ownergroup field is missing, first look to see if it has a parent, and if so, does the parent have an ownergroup? If not, assign based on the table above.This is why I am thinking a case statement maybe the best option. Also, I currently have a list of variables such as "@ASupervisor, @B1Supervisor, @B2Supervisor,...etc" so that I can change them in the future if need be. To save a lot of redundant code, is it possible to do something like:
(in this example, location is ABC-1234, ownergroup SHOULD be @ABCGroup, supervisor should be @ABCSupervisor, where @ABCGroup and @ABCSupervisor are set earlier in the code)If the supervisor field is missing, first look to see if it has a parent, and if so, does the parent have a supervisor (then copy it's supervisor)? If not, assign supervisor X.
Where X = '@' + '(the first three characters of the location)' + 'Supervisor' (in this example, X=@ABCSupervisor)Is this possible??
UPDATE 2:
I have spoken with the person who asked fo开发者_运维技巧r this database change and we have changed some thinking here. First, parent locations and child locations should always be the same (if they're not, that's a WHOLE OTHER issue). All sites (first 3 letters of location) should have the same ownergroup and supervisor, so essentially we can just look to see if a workorder entry has a NULL value in either field, then assign it based on the location. I believe the following code will work (but would like someone to review it before I implement it on the system)
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TRIGGER dbo.AutoAssign
ON dbo.workorder
AFTER INSERT,UPDATE
AS
BEGIN
SET NOCOUNT ON;
DECLARE @ABCSupervisor varchar(30)
DECLARE @DEFSupervisor varchar(30)
DECLARE @ABCOwnerGroup varchar(20)
DECLARE @DEFOwnerGroup varchar(20)
/*EDIT VARIABLES IF FUTURE CHANGES*/
--SET Supervisor values HERE;
SET @ABCSupervisor='JOHNDOE'
SET @XYZSupervisor='JANEDOE'
--SET OwnerGroup values HERE:
SET @ABCOwnerGroup='ALPHATEAM'
SET @XYZOwnerGroup='OMEGATEAM'
--UPDATES
UPDATE dbo.workorder
SET ownergroup='@'+SUBSTR(location,1,3)+'OwnerGroup'
WHERE status<>'COMP'
AND historyflag=0
AND istask=0
AND ownergroup IS NULL
AND location IS NOT NULL
UPDATE dbo.workorder
SET supervisor='@'+SUBSTR(location,1,3)+'Supervisor'
WHERE status<>'COMP'
AND historyflag=0
AND istask=0
AND supervisor IS NULL
AND location IS NOT NULL
END
GO
The only issues I see here are that I do not join of some sort on the "inserted" table so that it only affects those entries (and not the entire table every time). If I could get some help on that, it would be greatly appreciated!!
if you have done some update-statements you are on the right way. put the virtual table INSERTED in your update-statement and join it with a unique-key, e.g. SerialNo: that would be something like that:
create trigger Trig_WorkOrder for insert, update as
BEGIN
update wo
set location = pwo.location, ...
from dbo.workorder as wo, inserted as i, dbo.parentworkorder as pwo
where wo.serialNo = i.SerialNo -- join with the actual table-entry
and wo.pwo_id = pwo.id -- join with the parentworkorder
and i.ownergroup is null -- do it if new value is empty
and (pwo.ownergroup is not null or pwo.ownergroup <> '') -- do it if parentvalue is not empty
and (pwo.Supervisor is not null or pwo.Supervisor <> '') -- do it if parentvalue is not empty
update wo
set location = pwo.location, ...
from dbo.workorder as wo, inserted as i, dbo.standardworkorder as pwo
where wo.serialNo = i.SerialNo
and wo.location = pwo.location
and wo.ownergroup is null
and (pwo.ownergroup is null or pwo.ownergroup = '')
and (pwo.Supervisor is null or pwo.Supervisor = '')
END
I would sugest to store the default values in a separate table, for convenient joining if the parentvalues are empty.
Put two update-statements inside the trigger an code the where-clause to make sure that only one statement executes...
peace and good luck
If you are using Maximo 7.x, automation scripts can do what you want.
For example, we use an automation script that checks to see if the child work order's field is null. If it is, Maximo will take the parent work order value and fill it. In our case, it a custom field called WOPM1.
WorkOrderSet = mbo.getMboSet("PARENT")
if WorkOrderSet.getMbo(0) is not None:
SUPERVISOR = WorkOrderSet.getMbo(0).getString("SUPERVISOR")
Alternatively, if you have a basic lookup to fill in OwnerGroup and Supervisor based on the first three of the location, you can do a if then type logic to fill in data in the child work order when there is a match. v_location is the variable I have defined for a parent work order location:
if v_location == 'ABC':
mbo.setValue("ownergroup","ABCOwnerGroup")
mbo.setValue("supervisor","ABCSupervisor")
For what you are trying to achieve, I'm not sure using a trigger is the best solution in Maximo - using triggers ignores all MBO business logic. Maybe you could consider customizing MBO classes or using Maximo Escalations to do this taks. Using triggers could also get you in trouble when applying Fix Packs or upgrading Maximo so if you choose to go this way, be sure to backup the triggers before any such action.
精彩评论