DBA Data[Home] [Help]

PACKAGE BODY: APPS.CS_WF_AUTO_NTFY_UPDATE_PKG

Source


1 PACKAGE BODY CS_WF_AUTO_NTFY_UPDATE_PKG AS
2 /* $Header: cswfautb.pls 120.21.12020000.2 2012/07/10 09:24:39 bkanimoz ship $ */
3 
4 -- --------------------------------------
5 -- Constants used in this package
6 -- --------------------------------------
7 
8 -- cs_newline varchar2(1) := chr(10);
9 cs_delimiter varchar2(1) := ' ';
10 l_amp        VARCHAR2(8) := fnd_global.local_chr(38);
11 l_new_line   VARCHAR2(8) := fnd_global.newline;
12 dbg_msg      VARCHAR2(4000);
13 
14 /****************************************************************************
15                             Forward Declaration
16  ****************************************************************************/
17 
18   PROCEDURE pull_from_list(itemlist	IN OUT	NOCOPY	VARCHAR2,
19 			    element	OUT	NOCOPY	VARCHAR2);
20 
21   PROCEDURE SetWorkflowAdhocUser(
22 			p_wf_username	IN OUT NOCOPY VARCHAR2,
23 			p_email_address IN VARCHAR2,
24 			p_display_name	IN VARCHAR2 DEFAULT NULL);
25 
26   PROCEDURE SetWorkflowAdhocRole(
27                         p_wf_rolename   IN OUT NOCOPY VARCHAR2,
28                         p_user_list     IN VARCHAR2) ;
29 
30    PROCEDURE Set_HTML_Notification_Details
31               ( p_itemtype         IN        VARCHAR2,
32                 p_itemkey          IN        VARCHAR2,
33                 p_actid            IN        NUMBER,
34                 p_funmode          IN        VARCHAR2,
35                 p_contact_type     IN        VARCHAR2,
36                 p_contact_id       IN        NUMBER,
37                 p_request_id       IN        NUMBER,
38 		p_request_number   IN        VARCHAR2,
39  	        p_sender           IN        VARCHAR2,
40                 p_email_preference IN        VARCHAR2,
41                 p_user_language    IN        VARCHAR2,
42 	        p_recipient        IN        VARCHAR2,
43     		p_message_name     IN        VARCHAR2,
44 		p_from_status      IN        VARCHAR2,
45 		p_to_status        IN        VARCHAR2) ;
46 
47 PROCEDURE Create_Role_List
48               ( p_itemtype         IN        VARCHAR2,
49                 p_itemkey          IN        VARCHAR2,
50                 p_actid            IN        NUMBER,
51                 p_funmode          IN        VARCHAR2,
52                 p_action_code      IN        VARCHAR2,
53                 p_role_group_type  IN        VARCHAR2,
54                 p_role_group_code  IN        VARCHAR2);
55 
56 -- ***************************************************************************
57 -- *									     *
58 -- *			   Service Request Item Type			     *
59 -- *									     *
60 -- ***************************************************************************
61 
62 --                   -----------------------------------------
63 --                   |             PUBLIC SECTION            |
64 --                   | Following procedures are customizable |
65 --                   -----------------------------------------
66 --
67 
68   /*****************************************************************************
69   -- Check_Rules_For_Event
70   --   This procedure corresponds to the CHECK_RULES_FOR_EVENT function
71   --   activity.
72   --   This procedure checks for ALL rules defined for a given business event
73   --   If there are any valid rules defined, then we continue with the process
74   --   flow, otherwise we immediately go to the END activity.
75   --
76   --  Modification History:
77   --
78   --  Date        Name       Desc
79   --  ----------  ---------  ---------------------------------------------
80   --  09/07/04	  RMANABAT   Fix for bug 3871457. In cursors sel_primary_contact_csr,
81   --			     sel_all_contacts_csr, and sel_all_contacts_csr, changed
82   --			     SELECT null notification_template_id to
83   --			     SELECT to_char(null) notification_template_id.
84   --
85   --  27-Jul-05   ANEEMUCH   Rel 12.0 changes.
86   --
87   *****************************************************************************/
88 
89   PROCEDURE Check_Rules_For_Event( itemtype   IN  VARCHAR2,
90                                    itemkey    IN  VARCHAR2,
91                                    actid      IN  NUMBER,
92                                    funmode    IN  VARCHAR2,
93                                    result     OUT NOCOPY VARCHAR2 ) IS
94 
95     l_event_name		VARCHAR2(240);
96     --l_action_code		VARCHAR2(30);
97     l_request_number    	VARCHAR2(64);
98     l_request_id		NUMBER;
99     --l_event_condition_id        CS_SR_ACTION_TRIGGERS.event_condition_id%TYPE;
100     l_event_condition_id        VARCHAR2(30);
101     l_action_code       	CS_SR_ACTION_DETAILS.action_code%TYPE;
102     l_notify_conditions_list	VARCHAR2(4000);
103     l_notify_actions_list	VARCHAR2(4000);
104     l_update_conditions_list	VARCHAR2(4000);
105     l_update_actions_list	VARCHAR2(4000);
106     l_list_element		VARCHAR2(60);
107     l_ntfy_template_id		NUMBER;
108     l_wf_manual_launch		VARCHAR2(1);
109 
110     l_API_ERROR                 EXCEPTION;
111 
112 
113     CURSOR sel_action_csr IS
114       -- This part of the query is for Update Rules
115       SELECT csad.event_condition_id,
116              csad.action_code,
117 	     to_char(null) notification_template_id,
118 	     to_char(csad.event_condition_id) || csad.action_code index_cols,
119              to_char(null) role_group_type,
120              to_char(null) role_group_code
121       FROM CS_SR_ACTION_TRIGGERS csat,
122            CS_SR_ACTION_DETAILS csad,
123 	   CS_SR_EVENT_CODES_B cec
124       WHERE
125 	  cec.WF_BUSINESS_EVENT_ID = l_event_name
126           and csat.EVENT_CODE = cec.EVENT_CODE
127           and TRUNC(SYSDATE) BETWEEN TRUNC(NVL(csat.start_date_active, SYSDATE))
128               and TRUNC(NVL(csat.end_date_active, SYSDATE))
129           and csad.event_condition_id = csat.event_condition_id
130           and TRUNC(SYSDATE) BETWEEN TRUNC(NVL(csad.start_date_active, SYSDATE))
131               and TRUNC(NVL(csad.end_date_active, SYSDATE))
132           and csat.from_to_status_code IS NULL
133           and (csad.incident_status_id IS NOT NULL OR
134                csad.resolution_code IS NOT NULL)
135           and csat.relationship_type_id IN
136 		( select cil.link_type_id
137 	          FROM cs_incident_links cil
138 	          WHERE cil.subject_id = l_request_id)
139           -- We need to add this to differentiate it from notification rules.
140           -- We may have a ntfxn rule without a template_id (partially created).
141 	  and csad.action_code NOT like 'NOTIFY%'
142       UNION
143       SELECT csad.event_condition_id,
144              csad.action_code,
145 	     to_char(null) notification_template_id,
146 	     to_char(csad.event_condition_id) || csad.action_code index_cols,
147              to_char(null) role_group_type,
148              to_char(null) role_group_code
149       FROM CS_SR_ACTION_TRIGGERS csat,
150            CS_SR_ACTION_DETAILS csad,
151 	   CS_SR_EVENT_CODES_B cec
152       WHERE
153 	  cec.WF_BUSINESS_EVENT_ID = l_event_name
154           and csat.EVENT_CODE = cec.EVENT_CODE
155           and TRUNC(SYSDATE) BETWEEN TRUNC(NVL(csat.start_date_active, SYSDATE))
156               and TRUNC(NVL(csat.end_date_active, SYSDATE))
157           and csad.event_condition_id = csat.event_condition_id
158           and TRUNC(SYSDATE) BETWEEN TRUNC(NVL(csad.start_date_active, SYSDATE))
159               and TRUNC(NVL(csad.end_date_active, SYSDATE))
160           and csat.from_to_status_code IS NOT NULL
161           and csat.relationship_type_id IS NULL
162           and csat.incident_status_id IS NOT NULL
163           -- We need to add this to differentiate it from notification rules.
164           -- We may have a ntfxn rule without a template_id (partially created).
165 	  and csad.action_code NOT like 'NOTIFY%'
166           and csad.relationship_type_id IN
167 		( select cil.link_type_id
168 	          FROM cs_incident_links cil
169 	          WHERE cil.subject_id = l_request_id)
170 	  /******************
171 	  -- For 11.5.9, we are not supporting any Update Rules defined by the user.
172 	  -- So csad.relationship_type_id will always have a value as defined from the
173 	  -- seeded Rule from HLD:
174 	  --   Rule 2: If the status of the service request changes to Closed,
175   	  --      and it has a (outgoing) Root cause of relationship, then,
176 	  --      update status of all related service requests to Clear.
177 	  -- and csad.relationship_type_id IS NULL OR
178 	  ***************/
179       -- This part of the query is for Notification Rules
180       UNION
181       SELECT csad.event_condition_id,
182 	     csad.action_code,
183 	     csad.notification_template_id,
184 	     to_char(csad.event_condition_id) || csad.action_code index_cols,
185              role_group_type,
186              role_group_code
187       FROM CS_SR_ACTION_TRIGGERS csat,
188            CS_SR_ACTION_DETAILS csad,
189 	   CS_SR_EVENT_CODES_B cec
190 /* 03/01/2004 - RHUNGUND - Bug fix for 3412833
191    Commenting the following table from the FROM clause since it was resulting
192    in a cartesian join
193 
194 	   CS_SR_ACTION_CODES_B cac
195 */
196       WHERE
197 	  cec.WF_BUSINESS_EVENT_ID = l_event_name
198           and csat.EVENT_CODE = cec.EVENT_CODE
199           and TRUNC(SYSDATE) BETWEEN TRUNC(NVL(csat.start_date_active, SYSDATE))
200               and TRUNC(NVL(csat.end_date_active, SYSDATE))
201           and csad.event_condition_id = csat.event_condition_id
202           and TRUNC(SYSDATE) BETWEEN TRUNC(NVL(csad.start_date_active, SYSDATE))
203               and TRUNC(NVL(csad.end_date_active, SYSDATE))
204           and csad.notification_template_id IS NOT NULL
205           -- We need to add this since a notification rule can still be created (partially)
206           -- without a message template.
207 	  and csad.action_code like 'NOTIFY%'
208 
209       ORDER BY index_cols;
210 
211       sel_action_rec	sel_action_csr%ROWTYPE;
212 
213     -- Fix similar to 11.5.9 bug 3106572. rmanabat 10/13/03.
214     CURSOR sel_incident_id_csr IS
215       SELECT INCIDENT_ID
216       FROM cs_incidents_all_b
217       WHERE INCIDENT_NUMBER = l_request_number;
218 
219     CURSOR sel_incident_number_csr IS
220       SELECT INCIDENT_NUMBER
221       FROM cs_incidents_all_b
222       WHERE INCIDENT_ID = l_request_id;
223 
224 
225 /* RHUNGUND - 04/05/04
226    Fix for bug 3528510
227   The following cursor will catch the scenario if the "relationshipCreated" event is raised
228   for a non-sr object. The wf should ignore link events raised for non-sr objects
229   since suport for non-sr objects is not in current scope of this wf
230 */
231 
232    CURSOR check_if_rel_is_sr_csr IS
233 	SELECT subject_type FROM
234 	cs_incident_links
235  	WHERE subject_id = l_request_number;
236   l_subject_type VARCHAR2(35) := null;
237 
238 
239   BEGIN
240     IF (funmode = 'RUN') THEN
241       l_event_name := WF_ENGINE.GetItemAttrText(
242                                   itemtype        => itemtype,
243                                   itemkey         => itemkey,
244                                   aname           => 'EVENTNAME' );
245 
246       l_request_number := WF_ENGINE.GetItemAttrText(
247                                 itemtype        => itemtype,
248                                 itemkey         => itemkey,
249                                 aname           => 'REQUEST_NUMBER' );
250 
251       l_wf_manual_launch := WF_ENGINE.GetItemAttrText(
252                                 itemtype        => itemtype,
253                                 itemkey         => itemkey,
254                                 aname           => 'MANUAL_LAUNCH' );
255 
256 /* RHUNGUND - 04/05/04
257    Fix for bug 3528510 - Added the following IF block
258 */
259       IF(l_event_name = 'oracle.apps.cs.sr.ServiceRequest.relationshipcreated' OR
260          l_event_name = 'oracle.apps.cs.sr.ServiceRequest.relationshipdeleted') THEN
261 
262           l_subject_type := WF_ENGINE.GetItemAttrText(
263                                 itemtype        => itemtype,
264                                 itemkey         => itemkey,
265                                 aname           => 'LINK_SUBJECT_TYPE' );
266 
267           If (l_subject_type <> 'SR') THEN
268           		  l_wf_manual_launch := 'Y';
269           END IF;
270 
271       END IF;
272 
273 
274 
275       -- Fix for bug 3106572. rmanabat 09/26/03.
276       IF (l_request_number IS NOT NULL) THEN
277         OPEN sel_incident_id_csr;
278         FETCH sel_incident_id_csr INTO l_request_id;
279         IF (sel_incident_id_csr%NOTFOUND OR l_request_id IS NULL) THEN
280 
281           IF((l_event_name = 'oracle.apps.cs.sr.ServiceRequest.relationshipcreated' OR
282               l_event_name = 'oracle.apps.cs.sr.ServiceRequest.relationshipdeleted')  AND
283               l_subject_type <> 'SR') THEN
284          	CLOSE sel_incident_id_csr;
285 	  ELSE
286          	CLOSE sel_incident_id_csr;
287           	raise l_API_ERROR;
288           END IF;
289 
290         END IF;
291 
292         IF sel_incident_id_csr%ISOPEN THEN
293           CLOSE sel_incident_id_csr;
294         END IF;
295 
296       END IF;
297 
298 
299       -- This check needs to be done for workflows launched manually, i.e., via UI's
300       -- Launch Workflow Tools menu option. Auto notification WF had been launched
301       -- when the SR was saved, so this WF launch is redundant. rmanabat .
302       -- This can be replaced later by using WF_RULE2 default rule function in the
303       -- event subscription, which accepts parameter values as qualifiers like
304       -- what we did in SolutionAdded event subscription.
305 
306       IF (nvl(l_wf_manual_launch,'N') = 'N') THEN
307 
308 
309         -- Knowledge Base says they are passing OBJECT_ID (incident_id) instead of number, so
310         -- make sure both are instantiated here.
311         IF (l_request_number IS NULL) THEN
312 
313           l_request_id := WF_ENGINE.GetItemAttrNumber(
314                                     itemtype        => itemtype,
315                                     itemkey         => itemkey,
316                                     aname           => 'OBJECT_ID' );
317 
318           OPEN sel_incident_number_csr;
319           FETCH sel_incident_number_csr INTO l_request_number;
320           IF (sel_incident_number_csr%NOTFOUND OR l_request_number IS NULL) THEN
321             CLOSE sel_incident_number_csr;
322             raise l_API_ERROR;
323           END IF;
324           IF sel_incident_number_csr%ISOPEN THEN
325             CLOSE sel_incident_number_csr;
326           END IF;
327 
328           WF_ENGINE.SetItemAttrText(
329                   itemtype        => 'SERVEREQ',
330                   itemkey         => itemkey,
331                   aname           => 'REQUEST_NUMBER',
332                   avalue          => l_request_number);
333 
334         END IF;
335 
336 
337         OPEN sel_action_csr;
338         LOOP
339           FETCH sel_action_csr
340 	  INTO sel_action_rec;
341           --INTO l_event_condition_id, l_action_code, l_ntfy_template_id;
342           EXIT WHEN sel_action_csr%NOTFOUND;
343 
344 	  l_event_condition_id := TO_CHAR(sel_action_rec.event_condition_id);
345 
346           /**************
347             Build a condition_id/action_code list for notification rules.
348           **************/
349           IF (sel_action_rec.notification_template_id IS NOT NULL) THEN
350 
351             -- Check for the length, not to exceed max WF text length of 4000.
352             IF (nvl(LENGTH(l_notify_conditions_list), 0) +
353 	        nvl(LENGTH(l_event_condition_id), 0) + 1) <= 4000  OR
354 	       (nvl(LENGTH(l_notify_actions_list), 0) +
355 	        nvl(LENGTH(sel_action_rec.action_code), 0) + 1 ) <= 4000 THEN
356 
357               IF l_notify_conditions_list IS NULL THEN
358                 l_notify_conditions_list := l_event_condition_id;
359                 l_notify_actions_list := sel_action_rec.action_code;
360               ELSE
361                 l_notify_conditions_list := l_notify_conditions_list || ' ' || l_event_condition_id;
362                 l_notify_actions_list := l_notify_actions_list || ' ' || sel_action_rec.action_code;
363               END IF;
364 
365 	    ELSE
366 	      /**********
367                 If the query results exceeded the max allowable WF text length, then we
368 	        set an attribute to indicate that we need to re-query again to obtain
369 	        the remainder of the results which were not put in the initial list.
370 	      **********/
371 	      WF_ENGINE.SetItemAttrText(
372                           itemtype        => itemtype,
373                           itemkey         => itemkey,
374                           aname           => 'MORE_NTFY_ACTION_LIST',
375                           avalue          => 'Y' );
376 
377             END IF;
378 
379           /**************
380             Build a condition_id/action_code list for update rules.
381           **************/
382           ELSE	-- IF (sel_action_rec.notification_template_id IS NOT NULL)
383 
384             -- Check for the length, not to exceed max WF text length of 4000.
385             IF (nvl(LENGTH(l_update_conditions_list), 0) +
386 	        nvl(LENGTH(l_event_condition_id), 0) + 1) <= 4000  OR
387 	       (nvl(LENGTH(l_update_actions_list), 0) +
388 	        nvl(LENGTH(sel_action_rec.action_code), 0) + 1 ) <= 4000 THEN
389 
390 	      IF l_update_conditions_list IS NULL THEN
391 	        l_update_conditions_list := l_event_condition_id;
392 	        l_update_actions_list := sel_action_rec.action_code;
393               ELSE
394 	        l_update_conditions_list := l_update_conditions_list || ' ' || l_event_condition_id;
395 	        l_update_actions_list := l_update_actions_list || ' ' || sel_action_rec.action_code;
396               END IF;
397 
398 	    ELSE
399 	      WF_ENGINE.SetItemAttrText(
400                           itemtype        => itemtype,
401                           itemkey         => itemkey,
402                           aname           => 'MORE_UPDATE_ACTION_LIST',
403                           avalue          => 'Y' );
404 
405             END IF;
406           END IF;
407         END LOOP;
408         CLOSE sel_action_csr;
409 
410 
411         IF (l_update_conditions_list IS NOT NULL OR l_notify_conditions_list IS NOT NULL) THEN
412 
413           WF_ENGINE.SetItemAttrText(
414   	  	itemtype	=> itemtype,
415   	  	itemkey		=> itemkey,
416   	  	aname		=> 'NTFY_CONDITION_LIST',
417   	  	avalue		=> l_notify_conditions_list );
418           WF_ENGINE.SetItemAttrText(
419   	  	itemtype	=> itemtype,
420   	  	itemkey		=> itemkey,
421   	  	aname		=> 'NTFY_ACTION_LIST',
422   	  	avalue		=> l_notify_actions_list );
423           WF_ENGINE.SetItemAttrText(
424   		itemtype	=> itemtype,
425   		itemkey		=> itemkey,
426   		aname		=> 'UPDATE_CONDITION_LIST',
427   		avalue		=> l_update_conditions_list );
428           WF_ENGINE.SetItemAttrText(
429   		itemtype	=> itemtype,
430   		itemkey		=> itemkey,
431   		aname		=> 'UPDATE_ACTION_LIST',
432   		avalue		=> l_update_actions_list );
433 
434           /*** This will be used later when we convert our message text to PL/SQL docs.
435           WF_ENGINE.SetItemAttrText(
436   		itemtype	=> itemtype,
437   		itemkey		=> itemkey,
438   		aname		=> 'REQUEST_DETAILS',
439   		avalue		=> 'PL/SQL:RMM_TEST_WF_PKG.SR_PLSQL_DOC_ATTR1/'||
440 				l_request_number || 'a');
441           ***/
442 
443           result := 'COMPLETE:Y';
444         ELSE
445           result := 'COMPLETE:N';
446         END IF;
447 
448       ELSE
449 
450 	result := 'COMPLETE:N';
451 
452       END IF;	-- IF (l_wf_manual_launch = 'N')
453 
454     ELSIF (funmode = 'CANCEL') THEN
455         result := 'COMPLETE';
456     END IF;
457 
458   EXCEPTION
459     WHEN l_API_ERROR THEN
460       WF_CORE.Context('CS_WF_AUTO_NTFY_UPDATE_PKG', 'Check_Rules_For_Event',
461                       itemtype, itemkey, actid, funmode);
462       RAISE;
463 
464     WHEN OTHERS THEN
465       WF_CORE.Context('CS_WF_AUTO_NTFY_UPDATE_PKG', 'Check_Rules_For_Event',
466 		      itemtype, itemkey, actid, funmode);
467       RAISE;
468 
469   END Check_Rules_For_Event;
470 
471 
472   /****************************************************************************
473   -- Check_Notification_Rules
474   --   This procedure corresponds to the CHECK_NOTIFICATION_RULES function
475   --   activity.
476   --
477   --   The procedure checks if there are notification rules defined by checking
478   --   the conditions/actions list. If the list is not empty, then we pull the
479   --   first item on the lists to process. Otherwise, we end the sub-process.
480   --
481   ****************************************************************************/
482 
483   PROCEDURE Check_Notification_Rules( itemtype   IN  VARCHAR2,
484                                    itemkey    IN  VARCHAR2,
485                                    actid      IN  NUMBER,
486                                    funmode    IN  VARCHAR2,
487                                    result     OUT NOCOPY VARCHAR2 ) IS
488 
489     l_notify_conditions_list	VARCHAR2(4000);
490     l_notify_actions_list	VARCHAR2(4000);
491     l_event_condition_id	CS_SR_ACTION_TRIGGERS.event_condition_id%TYPE;
492     l_action_code		CS_SR_ACTION_DETAILS.action_code%TYPE;
493     l_element			VARCHAR2(100);
494 
495 
496   BEGIN
497 
498     IF (funmode = 'RUN') THEN
499 
500       l_notify_conditions_list := WF_ENGINE.GetItemAttrText(
501   				itemtype	=> itemtype,
502   				itemkey		=> itemkey,
503   				aname		=> 'NTFY_CONDITION_LIST');
504       l_notify_actions_list := WF_ENGINE.GetItemAttrText(
505   				itemtype	=> itemtype,
506   				itemkey		=> itemkey,
507   				aname		=> 'NTFY_ACTION_LIST');
508 
509 
510       IF (l_notify_conditions_list IS NOT NULL AND l_notify_actions_list IS NOT NULL) THEN
511 
512         pull_from_list(itemlist	=> l_notify_conditions_list,
513 		       element	=> l_element);
514 	l_event_condition_id := TO_NUMBER(l_element);
515 
516         pull_from_list(itemlist	=> l_notify_actions_list,
517 		       element	=> l_action_code);
518 
519         IF (l_event_condition_id IS NOT NULL AND l_action_code IS NOT NULL) THEN
520 
521           WF_ENGINE.SetItemAttrText(
522                 itemtype        => itemtype,
523                 itemkey         => itemkey,
524                 aname           => 'NTFY_CONDITION_LIST',
525                 avalue          => l_notify_conditions_list );
526           WF_ENGINE.SetItemAttrText(
527                   itemtype        => itemtype,
528                   itemkey         => itemkey,
529                   aname           => 'NTFY_ACTION_LIST',
530                   avalue          => l_notify_actions_list );
531           WF_ENGINE.SetItemAttrNumber(
532                   itemtype        => itemtype,
533                   itemkey         => itemkey,
534                   aname           => 'NTFY_EVENT_CONDITION_ID',
535                   avalue          => l_event_condition_id );
536           WF_ENGINE.SetItemAttrText(
537                   itemtype        => itemtype,
538                   itemkey         => itemkey,
539                   aname           => 'NTFY_ACTION_CODE',
540                   avalue          => l_action_code );
541 
542           result := 'COMPLETE:Y';
543 
544         ELSE
545           result := 'COMPLETE:N';
546 
547         END IF;
548 
549       ELSE
550           result := 'COMPLETE:N';
551       END IF;
552 
553 
554     ELSIF (funmode = 'CANCEL') THEN
555       result := 'COMPLETE';
556     END IF;
557 
558   EXCEPTION
559     WHEN OTHERS THEN
560       WF_CORE.Context('CS_WF_AUTO_NTFY_UPDATE_PKG', 'Check_Notification_Rules',
561 		      itemtype, itemkey, actid, funmode);
562       RAISE;
563 
564   END Check_Notification_Rules;
565 
566 
567   /****************************************************************************
568   -- Get_Recipients_To_Notify
569   --   This procedure corresponds to the GET_NTFXN_RECIPIENTS function
570   --   activity.
571   --
572   --   This procedure checks if there are valid recipients for the notification
573   --   rule being processed.
574   --   If more than one recipient is set (as in notifying owners of related SRs),
575   --   then a list of recipients is created which we process one item at a time.
576   ****************************************************************************/
577 
578   PROCEDURE Get_Recipients_To_Notify( itemtype   IN  VARCHAR2,
579                              itemkey    IN  VARCHAR2,
580                              actid      IN  NUMBER,
581                              funmode    IN  VARCHAR2,
582                              result     OUT NOCOPY VARCHAR2 ) IS
583 
584     l_request_number            VARCHAR2(64);
585     l_request_id		NUMBER;
586     l_event_name        	VARCHAR2(240);
587     l_event_condition_id        CS_SR_ACTION_TRIGGERS.event_condition_id%TYPE;
588     l_action_code       	CS_SR_ACTION_DETAILS.action_code%TYPE;
589     l_notify_recipient		VARCHAR2(100);
590     l_request_status            VARCHAR2(30);
591     l_request_status_temp	VARCHAR2(30);
592     l_recipient_role_list	VARCHAR2(4000);
593     l_linked_subject_list       VARCHAR2(4000);
594     --l_linked_incident_id	NUMBER;
595     l_linked_incident_number	VARCHAR2(64);
596 
597     l_return_status		VARCHAR2(1);
598     l_msg_count			NUMBER;
599     l_msg_data			VARCHAR2(2000);
600     l_owner_role		VARCHAR2(320);
601     l_owner_name		VARCHAR2(240);
602     l_char_subject_id        	VARCHAR2(30);
603     l_relationship_type_id	CS_SR_ACTION_DETAILS.relationship_type_id%TYPE;
604 
605     l_trigger_link_type		VARCHAR2(240);
606     l_trigger_link_type_id	NUMBER;
607 
608     CURSOR sel_link_type_id_csr IS
609       SELECT link_type_id
610       FROM CS_SR_LINK_TYPES_VL
611       WHERE name = l_trigger_link_type;
612 
613     CURSOR sel_event_action_csr IS
614       SELECT csad.event_condition_id,
615              csad.action_code,
616              csad.notification_template_id,	/** this is the WF message name  **/
617 	     csad.relationship_type_id detail_link_type,
618              csad.role_group_type,
619              csad.role_group_code,
620 	     csat.from_to_status_code,
621 	     csat.incident_status_id trigger_incident_status_id,
622 	     csat.relationship_type_id trigger_link_type
623       FROM CS_SR_ACTION_TRIGGERS csat,
624            CS_SR_ACTION_DETAILS csad,
625 	   CS_SR_EVENT_CODES_B cec
626       WHERE
627 	  cec.WF_BUSINESS_EVENT_ID = l_event_name
628           and csat.EVENT_CODE = cec.EVENT_CODE
629           and TRUNC(SYSDATE) BETWEEN TRUNC(NVL(csat.start_date_active, SYSDATE))
630               and TRUNC(NVL(csat.end_date_active, SYSDATE))
631           and csad.event_condition_id = csat.event_condition_id
632 	  and csad.event_condition_id = l_event_condition_id
633 	  and csad.action_code = l_action_code
634           and TRUNC(SYSDATE) BETWEEN TRUNC(NVL(csad.start_date_active, SYSDATE))
635               and TRUNC(NVL(csad.end_date_active, SYSDATE))
636           and csad.notification_template_id IS NOT NULL
637           -- We need to add this since a notification rule can still be created (partially)
638           -- without a message template.
639 	  and csad.action_code like 'NOTIFY%';
640 
641     sel_event_action_rec	sel_event_action_csr%ROWTYPE;
642 
643 
644     CURSOR sel_link_csr_1 IS
645       SELECT
646         cil.object_id,
647         cil.link_id,
648 	inc.incident_owner_id
649       FROM cs_incident_links cil,
650 	   cs_incidents_all_b inc
651       WHERE cil.subject_id = l_request_id
652 	and inc.incident_id = cil.object_id
653       ORDER BY cil.object_id;
654 
655     sel_link_rec        sel_link_csr_1%ROWTYPE;
656 
657     CURSOR sel_link_csr_2 IS
658       SELECT
659         cil.object_id,
660         cil.link_id,
661 	inc.incident_owner_id
662       FROM cs_incident_links cil,
663 	   cs_incidents_all_b inc
664       WHERE cil.subject_id = l_request_id
665         AND cil.link_type_id = l_relationship_type_id
666 	AND inc.incident_id = cil.object_id
667       ORDER BY cil.object_id;
668 
669     /*
670         Roopa - bug fix for 2788741
671         For the following actions:
672             NOTIFY_OWNER
673             NOTIFY_OLD_OWNER
674             NOTIFY_NEW_OWNER
675             NOTIFY_OWNER_OF_RELATED_SR
676 
677         This activity returns N (to skip notify action) if for the above actions, the SR owner
678         himself has updated the SR
679     */
680     l_incident_owner_id NUMBER;
681     l_resource_id NUMBER;
682     l_user_id NUMBER;
683     l_prev_owner_id NUMBER;
684 
685     CURSOR sel_inc_owner_id IS
686         SELECT incident_owner_id FROM cs_incidents_all_b WHERE
687             incident_id = l_request_id;
688 
689     CURSOR sel_resource_id IS
690         SELECT resource_id FROM jtf_rs_resource_extns emp, fnd_user users WHERE
691             emp.source_id = users.employee_id and
692             users.user_id = l_user_id;
693 
694     CURSOR sel_prev_owner_id IS
695         SELECT resource_id from jtf_rs_resource_extns emp WHERE
696             source_id = l_prev_owner_id;
697 
698 
699    l_party_role_group_code        cs_party_role_group_maps.party_role_group_code%TYPE;
700    l_part_role_code               cs_party_role_group_maps.party_role_code%TYPE;
701    l_notify_party_role_list       varchar2(4000);
702    l_notify_party_role_relsr_list varchar2(4000);
703 
704    CURSOR sel_party_role_csr IS
705       SELECT party_role_code
706         FROM cs_party_role_group_maps
707        WHERE party_role_group_code = l_party_role_group_code
708          AND trunc(sysdate) between trunc(nvl(start_date_active,sysdate))
709                                 and trunc(nvl(end_date_active,sysdate));
710 
711    sel_party_role_rec sel_party_role_csr%ROWTYPE;
712 
713   BEGIN
714 
715   IF (funmode = 'RUN') THEN
716 
717       l_request_number := WF_ENGINE.GetItemAttrText(
718                                 itemtype        => itemtype,
719                                 itemkey         => itemkey,
720                                 aname           => 'REQUEST_NUMBER' );
721 
722 
723       l_event_name := WF_ENGINE.GetItemAttrText(
724                                   itemtype        => itemtype,
725                                   itemkey         => itemkey,
726                                   aname           => 'EVENTNAME' );
727 
728       l_event_condition_id := WF_ENGINE.GetItemAttrNumber(
729                                   itemtype        => itemtype,
730                                   itemkey         => itemkey,
731                                   aname           => 'NTFY_EVENT_CONDITION_ID' );
732 
733       l_action_code := WF_ENGINE.GetItemAttrText(
734                                   itemtype        => itemtype,
735                                   itemkey         => itemkey,
736                                   aname           => 'NTFY_ACTION_CODE' );
737 
738 
739       /***********
740 
741       SELECT INCIDENT_ID
742       INTO l_request_id
743       FROM cs_incidents_all_b
744       WHERE INCIDENT_NUMBER = l_request_number;
745       *************/
746 
747       l_request_id := WF_ENGINE.GetItemAttrNumber(
748                                   itemtype        => itemtype,
749                                   itemkey         => itemkey,
750                                   aname           => 'REQUEST_ID' );
751 
752 
753       /*
754           Roopa - begin - bug fix for 2788741
755       */
756       l_user_id := WF_ENGINE.GetItemAttrNumber(
757                                   itemtype        => itemtype,
758                                   itemkey         => itemkey,
759                                   aname           => 'USER_ID' );
760       l_prev_owner_id := WF_ENGINE.GetItemAttrNumber(
761                                   itemtype        => itemtype,
762                                   itemkey         => itemkey,
763                                   aname           => 'PREV_OWNER_ID' );
764 
765       OPEN  sel_resource_id;
766       FETCH sel_resource_id into l_resource_id;
767       CLOSE sel_resource_id;
768 
769       IF (l_action_code = 'NOTIFY_OWNER' OR l_action_code = 'NOTIFY_NEW_OWNER') THEN
770           OPEN sel_inc_owner_id;
771           FETCH sel_inc_owner_id into l_incident_owner_id;
772           CLOSE sel_inc_owner_id;
773       ELSIF (l_action_code = 'NOTIFY_OLD_OWNER') THEN
774         OPEN sel_prev_owner_id;
775         FETCH sel_prev_owner_id  into l_incident_owner_id;
776         CLOSE sel_prev_owner_id;
777       END IF;
778 
779       IF(l_resource_id = l_incident_owner_id) THEN
780         result := 'COMPLETE:N';
781         return;
782       END IF;
783 
784       /*
785           Roopa - end - bug fix for 2788741
786       */
787 
788 
789       OPEN sel_event_action_csr;
790       FETCH sel_event_action_csr
791       INTO sel_event_action_rec;
792       CLOSE sel_event_action_csr;
793 
794       -- Set the message template to be used for the notification
795       WF_ENGINE.SetItemAttrText(
796                         itemtype        => itemtype,
797                         itemkey         => itemkey,
798                         aname           => 'NTFY_MESSAGE_NAME',
799                         avalue          => sel_event_action_rec.notification_template_id );
800 
801 
802       /*** Rule 5: Notify service request owner, when the status of a service request	***/
803       /***    he/she owns, is changed.							***/
804       /*** Rule 8 : Notify the owners of related service requests when the current	***/
805       /***    service request's status is changed to a specified value			***/
806 
807       IF (sel_event_action_rec.from_to_status_code IS NOT NULL) THEN
808 
809          IF (sel_event_action_rec.from_to_status_code = 'STATUS_CHANGED_TO') THEN
810             l_request_status := WF_ENGINE.GetItemAttrText(
811                                  itemtype        => itemtype,
812                                  itemkey         => itemkey,
813                                  aname           => 'REQUEST_STATUS_NEW' );--bug 11679421
814 
815          ELSIF (sel_event_action_rec.from_to_status_code = 'STATUS_CHANGED_FROM') THEN
816                l_request_status := WF_ENGINE.GetItemAttrText(
817                                  itemtype        => itemtype,
818                                  itemkey         => itemkey,
819                                  aname           => 'REQUEST_STATUS_OLD' );
820          END IF;
821 
822          SELECT name
823            INTO l_request_status_temp
824            FROM CS_INCIDENT_STATUSES_VL
825           WHERE INCIDENT_STATUS_ID = sel_event_action_rec.trigger_incident_status_id;
826 
827          IF (l_request_status_temp = l_request_status) THEN
828 
829   	    IF (sel_event_action_rec.detail_link_type IS NOT NULL) THEN
830 	        l_relationship_type_id := sel_event_action_rec.detail_link_type;
831 	        OPEN sel_link_csr_2;
832             ELSE
833 	      OPEN sel_link_csr_1;
834 	    END IF;
835 
836             /**************************************
837              Loop through all the related service requests that satisfy
838              the notification rule being processed, and build a list
839              of service requests whose owners we will each then notify
840             ****************************************/
841             LOOP
842 	        IF (sel_event_action_rec.detail_link_type IS NOT NULL) THEN
843 	          FETCH sel_link_csr_2 INTO sel_link_rec;
844 	            EXIT WHEN sel_link_csr_2%NOTFOUND;
845 	        ELSE
846 	          FETCH sel_link_csr_1 INTO sel_link_rec;
847 	            EXIT WHEN sel_link_csr_1%NOTFOUND;
848   	        END IF;
849 
850 	          l_char_subject_id := TO_CHAR(sel_link_rec.object_id);
851 
852 	        IF ((nvl(LENGTH(l_linked_subject_list), 0) +
853 	           nvl(LENGTH(l_char_subject_id), 0) + 1) <= 4000) THEN
854 
855  	           IF l_linked_subject_list IS NULL THEN
856 	              l_linked_subject_list := l_char_subject_id;
857 
858                    ELSE
859 	              l_linked_subject_list := l_linked_subject_list || ' ' || l_char_subject_id;
860                    END IF;
861 	        ELSE
862                    /*************************************
863                     Will need to build another list later since it
864                     won't fit here. This flag will tell us later to
865                     re-query the remainding subject IDs.
866                    **************************************/
867 
868 	           WF_ENGINE.SetItemAttrText(
869               		itemtype        => itemtype,
870               		itemkey         => itemkey,
871               		aname           => 'MORE_NTFY_LINKED_SUBJECT_LIST',
872               		avalue          => 'Y' );
873 	           EXIT;
874 	        END IF;
875             END LOOP;
876 
877 	    IF sel_link_csr_2%ISOPEN THEN
878 	      CLOSE sel_link_csr_2;
879 	    ELSIF sel_link_csr_1%ISOPEN THEN
880 	      CLOSE sel_link_csr_1;
881 	    END IF;
882 
883 	    IF (l_linked_subject_list IS NOT NULL) THEN
884 
885 	        WF_ENGINE.SetItemAttrText(
886                               itemtype        => itemtype,
887                               itemkey         => itemkey,
888                               aname           => 'NTFY_LINKED_SUBJECT_LIST',
889                               avalue          => l_linked_subject_list );
890 
891 	        result := 'COMPLETE:Y';
892 	    ELSE
893 
894                 -- Roopa - there might be a scenario where SR_STATUS_CHANGED event is associated
895                 --   with an action that does not include link/rel type as an action detail
896                 --   example : SR_STATUS_CHANGED(event) + NOTIFY_OWNER(action) combination
897                 --   the above example is a valid event-action combination. Hence result = 'Y'
898 
899                 IF(sel_event_action_rec.detail_link_type IS NULL) THEN
900                   result := 'COMPLETE:Y';
901                 ELSE
902                   result := 'COMPLETE:N';
903                 END IF;
904 
905 	    END IF;
906 
907             IF (sel_event_action_rec.action_code = 'NOTIFY_ASSOCIATED_PARTIES' OR
908                 sel_event_action_rec.action_code = 'NOTIFY_ALL_ASSOCIATED_PARTIES' ) THEN
909 
910                 Create_Role_List
911                      ( p_itemtype         => itemtype,
912                        p_itemkey          => itemkey,
913                        p_actid            => actid,
914                        p_funmode          => funmode,
915                        p_action_code      => sel_event_action_rec.action_code,
916                        p_role_group_type  => sel_event_action_rec.role_group_type,
917                        p_role_group_code  => sel_event_action_rec.role_group_code) ;
918 
919             END IF ;
920 
921          ELSE
922             result := 'COMPLETE:N';
923 
924          END IF;		/**** IF (l_request_status_temp = l_request_status) ******/
925 
926          /*** Rule 6: Notify the service request owner, when a link type is created ***/
927          /***    between the current service request to another service request 	 ***/
928          /*** Rule 7 : Notify the service request owner, when a link type is deleted***/
929          /***    between the current service request to another service request	 ***/
930 
931          /*** IF (sel_event_action_rec.from_to_status_code IS NOT NULL)		 ***/
932 
933       ELSIF (sel_event_action_rec.trigger_link_type IS NOT NULL) THEN
934 
935           /********************
936            This section is for relationship created/deleted events
937           ********************/
938 	  l_trigger_link_type := WF_ENGINE.GetItemAttrText(
939                               	itemtype        => itemtype,
940                               	itemkey         => itemkey,
941                               	aname           => 'NTFY_LINK_TYPE');
942 
943 	  -- Obtain link_type_id from the link type name
944 	  OPEN sel_link_type_id_csr;
945 	  FETCH sel_link_type_id_csr
946 	  INTO l_trigger_link_type_id;
947 
948 	  IF (sel_link_type_id_csr%FOUND) THEN
949              -- LINKED_INCIDENT_ID is mandatory for this rule since we can't get a
950              -- handle of the linked SR when the link has been deleted.
951              l_linked_incident_number := WF_ENGINE.GetItemAttrText(
952                                   itemtype        => itemtype,
953                                   itemkey         => itemkey,
954                                   aname           => 'NTFY_LINKED_INCIDENT_NUMBER' );
955 
956 	     IF (sel_event_action_rec.trigger_link_type = l_trigger_link_type_id) THEN
957 	       result := 'COMPLETE:Y';
958 	     ELSE
959 	       result := 'COMPLETE:N';
960 	     END IF;
961 
962 	  ELSE
963 	     result := 'COMPLETE:N';
964 	  END IF;
965 	     CLOSE sel_link_type_id_csr;
966 
967       ELSE	/** IF (sel_event_action_rec.from_to_status_code IS NOT NULL) **/
968 
969        	   IF (sel_event_action_rec.action_code = 'NOTIFY_ASSOCIATED_PARTIES' OR
970 	       sel_event_action_rec.action_code = 'NOTIFY_ALL_ASSOCIATED_PARTIES' ) THEN
971 
972      	       Create_Role_List
973                      ( p_itemtype         => itemtype,
974                        p_itemkey          => itemkey,
975                        p_actid            => actid,
976                        p_funmode          => funmode,
977                        p_action_code      => sel_event_action_rec.action_code,
978                        p_role_group_type  => sel_event_action_rec.role_group_type,
979                        p_role_group_code  => sel_event_action_rec.role_group_code);
980 
981 	    END IF ;
982 
983 	  result := 'COMPLETE:Y';
984 
985       END IF;
986 
987 
988     ELSIF (funmode = 'CANCEL') THEN
989       result := 'COMPLETE';
990     END IF;
991 
992   EXCEPTION
993     WHEN OTHERS THEN
994       WF_CORE.Context('CS_WF_AUTO_NTFY_UPDATE_PKG', 'Get_Recipients_To_Notify',
995 		      itemtype, itemkey, actid, funmode);
996       RAISE;
997 
998   END Get_Recipients_To_Notify;
999 
1000 
1001   /**********************************************************************
1002   -- Set_Notification_Details
1003   --
1004   --   This procedure corresponds to the GET_NTFXN_RECIPIENTS function
1005   --   activity.
1006   --
1007   --   This procedure sets all the necessary workflow message attributes
1008   --   that are used to replace the tokens in the message template used
1009   --   for the notification.
1010   --   If the details are set successfully, then the result is set to
1011   --   'SET', otherwise it is 'UNSET'.
1012   --
1013   --  Modification History:
1014   --
1015   --  Date        Name       Desc
1016   --  ----------  ---------  ---------------------------------------------
1017   --  04/29/04	  RMANABAT   Fix for bug 3600819. Regression in functionality
1018   --			     of standard workflow Notify activity does not
1019   --			     allow users to have multiple email addresses
1020   --			     separatde by comma/space. Created one adhoc user
1021   --			     for each contact and attaced these to one adhoc
1022   --			     role as a list.
1023   -- 05/13/04     RMANABAT   Fix for bug 3628552. Use actual workflow users
1024   --			     for contacts, if available.
1025   -- 06/09/04     RMANABAT   Fix for bug 3663881. Added URL attribute to all
1026   --			     ntfxns to contacts. Removed hard code of jsp page
1027   --			     in URL.
1028   -- 07/13/04     RMANABAT   Fix for bug 3763848. For notification to contacts,
1029   --			     create just one ADHOC role and user and re-use
1030   --			     updating just the workflow user info.
1031   -- 08/12/04	  RMANABAT   Fix for bug 3830327. Ntfxn to contacts, duplicate
1032   --			     hz_party for the same contact points can exist.
1033   --			     This will result in error when attaching the same WF
1034   --			     user to an adhoc role. Create adhoc user for duplicate
1035   --			     contact WF user.
1036   -- 09/07/04	  RMANABAT   Fix for bug 3871457. In cursors sel_primary_contact_csr,
1037   --			     sel_all_contacts_csr, and sel_all_contacts_csr, changed
1038   --			     SELECT null person_id to SELECT to_number(null) person_id.
1039   -- 26-Jul-2005  ANEEMUCH   Rel 12.0 changes:
1040   -- 10-Mar-2006  spusegao   Modified to set the NTFY_Linked_sr_status and ntfy_linked_sr_summary attrs.
1041   --
1042   -- 14-Mar-2006  spusegao   Modified the code to send notification to the primary contacts.
1043   --                         to send a notification even if EMAIL is not specified as a primary contact.
1044   --                         The new code will derive primary contact in the following sequence.
1045   --                         1. If the primary contact point on the SR is EMAIL itself.
1046   --                         2. If primary contact point is not email then see if email for the primary
1047   --                            contact is specified in the CS schema. Derive the email address using it.
1048   --                         3. If email is not specified at all in the CS schema then check if the WF role
1049   --                            associated with the primary contact has an email address.
1050   --                         4. If WF role does not have email address then get the primary email address
1051   --                            either from HZ or HR depending on the contact type.
1052   --                         5. If primary email address is not specified then use any email address specified
1053   --                            This is true only for customer contact.
1054   --                         (Bug#5052683)
1055   --
1056   -- 15 Mar 06  spusegao     Changed the references to per_people_f  in the set_notification_details proc.
1057   --                         to use per_workforce_x view.
1058   ***********************************************************************/
1059 
1060   PROCEDURE Set_Notification_Details( itemtype   IN  VARCHAR2,
1061                              itemkey    IN  VARCHAR2,
1062                              actid      IN  NUMBER,
1063                              funmode    IN  VARCHAR2,
1064                              result     OUT NOCOPY VARCHAR2 ) IS
1065 
1066     l_request_number            VARCHAR2(64);
1067     l_action_code		CS_SR_ACTION_DETAILS.action_code%TYPE;
1068     l_notify_recipient		VARCHAR2(100);
1069     l_notification_template_id	VARCHAR2(30);
1070     l_linked_subject_list       VARCHAR2(4000);
1071     l_element			VARCHAR2(100);
1072     l_incident_owner_id		cs_incidents_all_b.INCIDENT_OWNER_ID%TYPE;
1073     l_request_id		NUMBER;
1074     l_contact_email		VARCHAR2(2000);
1075     l_contact_email_list	VARCHAR2(2000);
1076     l_contact_point_id		NUMBER;
1077     l_contact_point_id_list	VARCHAR2(2000);
1078     l_notify_subject_id		NUMBER;
1079 
1080     l_party_role_code           cs_party_role_group_maps.party_role_code%TYPE;
1081     l_party_role_name           cs_party_roles_tl.name%TYPE;
1082     l_party_role_group_code     cs_party_role_group_maps.party_role_group_code%TYPE;
1083 
1084     l_notify_party_role_list    VARCHAR2(2000);
1085     l_notify_relsr_party_role_list  VARCHAR2(2000);
1086 
1087     l_return_status             VARCHAR2(1);
1088     l_msg_count                 NUMBER;
1089     l_msg_data                  VARCHAR2(2000);
1090     l_owner_role                VARCHAR2(320);
1091     l_owner_name                VARCHAR2(240);
1092 
1093     l_adhoc_role                VARCHAR2(100);
1094     l_adhoc_display_name        VARCHAR2(360);
1095     l_user			VARCHAR2(320);
1096     l_user_display_name		VARCHAR2(320);
1097 
1098     l_link_type_name		VARCHAR2(240);
1099 
1100     l_contact_party_id_list	VARCHAR2(2000);
1101     l_contact_party_id		NUMBER;
1102     l_party_id			NUMBER;
1103     l_contact_type		VARCHAR2(30);
1104 
1105     l_adhoc_user_list		VARCHAR2(2000);
1106 
1107     l_orig_system               VARCHAR2(30);
1108     l_orig_system_id            NUMBER;
1109     l_person_id                 NUMBER;
1110 
1111     adhoc_count			NUMBER := 0;
1112     l_sr_contact_point_id       NUMBER;
1113     l_pri_sr_contact_point_id   NUMBER;
1114     l_pri_email_cont_pt_id      NUMBER;
1115     l_sr_contact_point_type     VARCHAR2(240);
1116     l_contact_first_name        VARCHAR2(240);
1117     l_contact_last_name         VARCHAR2(240);
1118 
1119     -- Local Parameters for html_notifications
1120 
1121     l_notification_preference   VARCHAR2(240);
1122     l_language                  VARCHAR2(240);
1123     l_html_notification_flag    VARCHAR2(1);
1124     l_event_condition_id        NUMBER;
1125     l_html_contact_email_list   VARCHAR2(2000);
1126     l_html_adhoc_user_list      VARCHAR2(2000);
1127     l_contact_type_list         VARCHAR2(2000);
1128     l_contact_id_list           VARCHAR2(2000);
1129     l_notification_pref_list    VARCHAR2(2000);
1130     l_language_list             VARCHAR2(2000);
1131     l_err_code                  NUMBER;
1132     l_err_str                   VARCHAR2(2000);
1133 
1134 
1135     CURSOR sel_primary_contact_csr IS
1136       SELECT hcp.EMAIL_ADDRESS,
1137 	     cshcp.party_id,
1138 	     to_number(NULL) person_id,
1139 	     cshcp.contact_type,
1140              cshcp.sr_contact_point_id
1141       FROM hz_contact_points hcp,
1142         cs_hz_sr_contact_points cshcp
1143       WHERE cshcp.INCIDENT_ID = l_request_id
1144         AND cshcp.PRIMARY_FLAG = 'Y'
1145         AND cshcp.CONTACT_POINT_TYPE = 'EMAIL'
1146         AND hcp.CONTACT_POINT_ID = cshcp.CONTACT_POINT_ID
1147         AND cshcp.contact_type <> 'EMPLOYEE'
1148         AND cshcp.party_role_code = 'CONTACT'
1149       union
1150       select ppf.email_address,
1151 	     ppf.party_id,
1152 	     ppf.person_id,
1153 	     cshcp.contact_type,
1154 	     cshcp.sr_contact_point_id
1155       from per_all_people_f ppf,
1156         cs_hz_sr_contact_points cshcp
1157       where cshcp.INCIDENT_ID = l_request_id
1158         AND cshcp.PRIMARY_FLAG = 'Y'
1159 	AND cshcp.CONTACT_POINT_TYPE = 'EMAIL'
1160         AND ppf.person_id = cshcp.party_id
1161         AND cshcp.contact_type = 'EMPLOYEE'
1162         AND TRUNC(SYSDATE) BETWEEN TRUNC(NVL(ppf.effective_start_date, SYSDATE))
1163         AND TRUNC(NVL(ppf.effective_end_date, SYSDATE));
1164 
1165 -- Breaking the primary contact cursor
1166 
1167    -- Get the primary contact info.
1168 
1169       CURSOR get_primary_contact_info IS
1170       SELECT cshcp.party_id,
1171              cshcp.contact_type,
1172              cshcp.sr_contact_point_id,
1173              cshcp.contact_point_type,
1174              cshcp.contact_point_id
1175         FROM cs_hz_sr_contact_points cshcp
1176        WHERE cshcp.INCIDENT_ID = l_request_id
1177          AND cshcp.PRIMARY_FLAG = 'Y'
1178          AND cshcp.party_role_code = 'CONTACT'
1179 	 AND cshcp.end_date_active IS NULL;--12.1.3 Inactivate Contact Points
1180 
1181   -- Get the primary contact's email information from the cs_hz_sr_contact_points table.
1182 
1183       CURSOR get_primary_email_info_CS (p_party_id IN NUMBER) IS
1184       SELECT cshcp.sr_contact_point_id,
1185              cshcp.contact_point_id
1186         FROM cs_hz_sr_contact_points cshcp
1187        WHERE cshcp.INCIDENT_ID = l_request_id
1188          AND cshcp.party_role_code = 'CONTACT'
1189          AND cshcp.contact_point_type = 'EMAIL'
1190          AND cshcp.party_id = p_party_id
1191 	 AND cshcp.end_date_active IS NULL;--12.1.3 Inactivate Contact Points
1192 
1193   -- Get the email information from the HZ schema for the primary contact.
1194 
1195      CURSOR get_primary_email_info_HZ(p_contact_point_id IN NUMBER,
1196                                       p_party_id IN NUMBER) IS
1197      SELECT email_address
1198        FROM hz_contact_points
1199       WHERE contact_point_id = p_contact_point_id
1200         AND contact_point_type = 'EMAIL'
1201         AND owner_table_name = 'HZ_PARTIES'
1202         AND owner_table_id   = p_party_id ;
1203 
1204   -- Get any customer email address starting with the primary email address.
1205 
1206      CURSOR get_any_cust_email_address(p_party_id  IN NUMBER) IS
1207      SELECT email_address
1208        FROM hz_contact_points
1209       WHERE contact_point_type = 'EMAIL'
1210         AND owner_table_name = 'HZ_PARTIES'
1211         AND owner_table_id   = p_party_id
1212       ORDER BY Primary_flag DESC;
1213 
1214   -- Get the email information from the HR schema for the primary contact.
1215 
1216      CURSOR get_primary_email_info_HR(p_person_id IN NUMBER) IS
1217      SELECT ppf.email_address
1218             --ppf.party_id,
1219             --ppf.person_id
1220        FROM per_workforce_x ppf
1221       WHERE ppf.person_id = p_person_id;
1222         --AND TRUNC(SYSDATE) BETWEEN TRUNC(NVL(ppf.effective_start_date, SYSDATE))
1223         --AND TRUNC(NVL(ppf.effective_end_date, SYSDATE));
1224 
1225   -- Get primary contact party info
1226 
1227      CURSOR get_prim_cont_party_info_reln (p_party_id IN NUMBER) IS
1228      SELECT hzp.person_first_name first_name,
1229             hzp.person_last_name last_name
1230        FROM hz_parties hzp,
1231             hz_relationships hzr
1232       WHERE hzr.PARTY_ID = p_party_id
1233         AND hzr.SUBJECT_ID = hzp.PARTY_ID
1234         AND hzr.SUBJECT_TYPE = 'PERSON';
1235 
1236      CURSOR get_prim_cont_party_info_per (p_party_id IN NUMBER) IS
1237      SELECT hzp.person_first_name first_name,
1238             hzp.person_last_name last_name
1239        FROM hz_parties hzp
1240       WHERE hzp.PARTY_ID = p_party_id;
1241 
1242      CURSOR get_prim_cont_party_info_empl (p_party_id IN NUMBER) IS
1243      SELECT ppf.first_name first_name,
1244             ppf.last_name last_name
1245        FROM per_workforce_x ppf
1246       WHERE ppf.person_id = p_party_id;
1247 
1248 --Added and condition for party role in R12 ... aneemuch
1249 
1250     CURSOR sel_all_contacts_csr IS
1251       SELECT hcp.EMAIL_ADDRESS,
1252 	     cshcp.party_id,
1253 	     to_number(NULL) person_id,
1254 	     cshcp.contact_type,
1255 	     cshcp.sr_contact_point_id
1256       FROM hz_contact_points hcp,
1257         cs_hz_sr_contact_points cshcp
1258       WHERE cshcp.INCIDENT_ID = l_request_id
1259      	AND cshcp.CONTACT_POINT_TYPE = 'EMAIL'
1260         AND hcp.CONTACT_POINT_ID = cshcp.CONTACT_POINT_ID
1261         AND cshcp.contact_type <> 'EMPLOYEE'
1262         AND cshcp.party_role_code = 'CONTACT'
1263       UNION
1264       select ppf.email_address,
1265 	     ppf.party_id,
1266 	     ppf.person_id,
1267 	     cshcp.contact_type,
1268 	     cshcp.sr_contact_point_id
1269       from per_all_people_f ppf,
1270       cs_hz_sr_contact_points cshcp
1271       where cshcp.INCIDENT_ID = l_request_id
1272 	AND cshcp.CONTACT_POINT_TYPE = 'EMAIL'
1273         AND ppf.person_id = cshcp.party_id
1274         AND cshcp.contact_type = 'EMPLOYEE'
1275         AND cshcp.party_role_code = 'CONTACT'
1276 	AND cshcp.end_date_active IS NULL--12.1.3 Inactivate Contact Points
1277         AND TRUNC(SYSDATE) BETWEEN TRUNC(NVL(ppf.effective_start_date, SYSDATE))
1278         AND TRUNC(NVL(ppf.effective_end_date, SYSDATE));
1279 --Added and condition for party role in R12 ... aneemuch
1280 
1281 
1282 -- Rel 12.0 changes
1283 -- Cursor to query contacts from cs_hz_sr_contact_points table for notification to be
1284 -- sent to all the contacts of a particular party role code.
1285 
1286     CURSOR sel_party_role_contacts_csr IS
1287       SELECT hcp.EMAIL_ADDRESS,
1288 	     cshcp.party_id,
1289 	     to_number(NULL) person_id,
1290 	     cshcp.contact_type,
1291 	     cshcp.sr_contact_point_id
1292       FROM hz_contact_points hcp,
1293         cs_hz_sr_contact_points cshcp
1294       WHERE cshcp.INCIDENT_ID = l_request_id
1295      	AND cshcp.CONTACT_POINT_TYPE = 'EMAIL'
1296         AND hcp.CONTACT_POINT_ID = cshcp.CONTACT_POINT_ID
1297         AND cshcp.contact_type <> 'EMPLOYEE'
1298         AND cshcp.party_role_code = l_party_role_code
1299       UNION
1300       select ppf.email_address,
1301              ppf.party_id,
1302              ppf.person_id,
1303              cshcp.contact_type,
1304              cshcp.sr_contact_point_id
1305       from per_all_people_f ppf,
1306       cs_hz_sr_contact_points cshcp
1307       where cshcp.INCIDENT_ID = l_request_id
1308         AND cshcp.CONTACT_POINT_TYPE = 'EMAIL'
1309         AND ppf.person_id = cshcp.party_id
1310         AND cshcp.contact_type = 'EMPLOYEE'
1311         AND cshcp.party_role_code = l_party_role_code
1312         AND TRUNC(SYSDATE) BETWEEN TRUNC(NVL(ppf.effective_start_date, SYSDATE))
1313         AND TRUNC(NVL(ppf.effective_end_date, SYSDATE));
1314 
1315 --Added and condition for party role in R12 ... aneemuch
1316 
1317 
1318     CURSOR sel_new_contact_csr IS
1319       SELECT hcp.EMAIL_ADDRESS,
1320 	     cshcp.party_id,
1321 	     to_number(NULL) person_id,
1322 	     cshcp.contact_type,
1323 	     cshcp.sr_contact_point_id
1324       FROM hz_contact_points hcp,
1325            cs_hz_sr_contact_points cshcp
1326       WHERE
1327     	hcp.CONTACT_POINT_TYPE = 'EMAIL'
1328         AND hcp.CONTACT_POINT_ID = l_contact_point_id
1329         AND hcp.CONTACT_POINT_ID = cshcp.CONTACT_POINT_ID
1330         AND cshcp.contact_type <> 'EMPLOYEE'
1331         AND cshcp.party_role_code = 'CONTACT'
1332       UNION
1333       SELECT ppf.EMAIL_ADDRESS,
1334 	     ppf.party_id,
1335 	     ppf.person_id,
1336 	     cshcp.contact_type,
1337 	     cshcp.sr_contact_point_id
1338       FROM per_all_people_f ppf,
1339            cs_hz_sr_contact_points cshcp
1340       WHERE
1341     	cshcp.CONTACT_POINT_TYPE = 'EMAIL'
1342         AND ppf.PERSON_ID = l_contact_point_id
1343         AND cshcp.PARTY_ID = ppf.person_id
1344         AND cshcp.contact_type = 'EMPLOYEE'
1345         AND cshcp.party_role_code = 'CONTACT'
1346         AND TRUNC(SYSDATE) BETWEEN TRUNC(NVL(ppf.effective_start_date, SYSDATE))
1347         AND TRUNC(NVL(ppf.effective_end_date, SYSDATE));
1348 
1349     CURSOR sel_new_party_role_contact_csr IS
1350       SELECT hcp.EMAIL_ADDRESS,
1351              cshcp.party_id,
1352              to_number(NULL) person_id,
1353              cshcp.contact_type,
1354              cshcp.sr_contact_point_id,
1355              cpr.name,
1356              cpr.party_role_code
1357       FROM hz_contact_points hcp,
1358            cs_hz_sr_contact_points cshcp,
1359            cs_party_roles_tl cpr
1360       WHERE
1361         hcp.CONTACT_POINT_TYPE = 'EMAIL'
1362         AND hcp.CONTACT_POINT_ID = l_contact_point_id
1363         AND hcp.CONTACT_POINT_ID = cshcp.CONTACT_POINT_ID
1364         AND cshcp.party_role_code = cpr.party_role_code
1365         AND cpr.language = userenv('LANG')
1366         AND cshcp.contact_type <> 'EMPLOYEE'
1367         AND cshcp.party_role_code <> 'CONTACT'
1368       UNION
1369       SELECT ppf.EMAIL_ADDRESS,
1370              ppf.party_id,
1371              ppf.person_id,
1372              cshcp.contact_type,
1373              cshcp.sr_contact_point_id,
1374              cpr.name,
1375              cpr.party_role_code
1376       FROM per_all_people_f ppf,
1377            cs_hz_sr_contact_points cshcp,
1378            cs_party_roles_tl cpr
1379       WHERE
1380         cshcp.CONTACT_POINT_TYPE = 'EMAIL'
1381         AND ppf.PERSON_ID = l_contact_point_id
1382         AND cshcp.PARTY_ID = ppf.person_id
1383         AND cshcp.contact_type = 'EMPLOYEE'
1384         AND cshcp.party_role_code = cpr.party_role_code
1385         AND cpr.language = userenv('LANG')
1386         AND cshcp.party_role_code <> 'CONTACT'
1387         AND TRUNC(SYSDATE) BETWEEN TRUNC(NVL(ppf.effective_start_date, SYSDATE))
1388         AND TRUNC(NVL(ppf.effective_end_date, SYSDATE));
1389 
1390     CURSOR sel_link_csr IS
1391       SELECT
1392 	inc.incident_number,
1393         inc.incident_owner_id,
1394         cil.object_id,
1395         cil.link_type_id,
1396 	cilt.name link_type_name
1397       FROM cs_incident_links cil,
1398 	cs_incidents_all_b inc,
1399 	CS_SR_LINK_TYPES_VL cilt
1400       WHERE cil.subject_id = l_request_id
1401 	and cil.object_id = l_notify_subject_id
1402 	and inc.incident_id = cil.object_id
1403         and cilt.link_type_id(+) = cil.link_type_id;
1404 
1405     sel_link_rec        sel_link_csr%ROWTYPE;
1406 
1407     /*
1408         Roopa - bug fix for 2788741
1409         For the following action:
1410             NOTIFY_OWNER_OF_RELATED_SR
1411 
1412         This activity returns N (to skip notify action) if for the above actions, the SR owner
1413         himself has updated the SR
1414     */
1415     l_incident_owner_id NUMBER;
1416     l_resource_id NUMBER;
1417     l_user_id NUMBER;
1418 
1419     CURSOR sel_resource_id IS
1420         SELECT resource_id FROM jtf_rs_resource_extns emp, fnd_user users WHERE
1421             emp.source_id = users.employee_id and
1422             users.user_id = l_user_id;
1423 
1424     /* Roopa - begin
1425        Fix for bug # 2788156
1426        The wf role should be checked on the employee/source id and NOT on the incident_owner_id
1427        This cursor will get us the employee id
1428     */
1429 
1430     CURSOR l_sel_employee_id_csr IS
1431       SELECT source_id FROM jtf_rs_resource_extns emp
1432       WHERE emp.resource_id = sel_link_rec.incident_owner_id;
1433 
1434     l_employee_id NUMBER;
1435 
1436     CURSOR l_sel_curr_sr_details_csr IS
1437       SELECT incident_type_id,incident_status_id,incident_urgency_id,incident_severity_id,summary
1438       FROM cs_incidents_all_vl inc
1439       WHERE inc.incident_number = l_request_number;
1440 
1441     l_sel_curr_sr_details_rec  l_sel_curr_sr_details_csr%ROWTYPE;
1442 
1443     l_prev_type_id NUMBER;
1444     l_prev_status_id NUMBER;
1445     l_prev_severity_id NUMBER;
1446     l_prev_urgency_id NUMBER;
1447     l_prev_summary VARCHAR2(240);
1448 
1449     l_lookup_code VARCHAR2(30);
1450     l_changed_field VARCHAR2(30);
1451     l_changed_field_list VARCHAR2(240);
1452     l_updated_by VARCHAR2(240);
1453 
1454 
1455     CURSOR l_sel_lookup_value_csr IS
1456       SELECT a.lookup_code, a.meaning FROM cs_lookups a
1457       WHERE a.lookup_type = 'CS_SR_UPDATED_FIELDS';
1458 
1459     l_sel_lookup_value_rec l_sel_lookup_value_csr%ROWTYPE;
1460 
1461 
1462     CURSOR l_get_resource_name_csr IS
1463       SELECT source_last_name,source_first_name
1464       from jtf_rs_resource_extns a, fnd_user b, cs_incidents_all_b c
1465       WHERE c.last_updated_by = b.user_id and
1466         b.employee_id = a.source_id and
1467         c.incident_number = l_request_number;
1468 
1469     l_get_resource_name_rec l_get_resource_name_csr%ROWTYPE;
1470 
1471     CURSOR l_get_create_rsrc_csr IS
1472       SELECT source_last_name,source_first_name
1473       from jtf_rs_resource_extns a, fnd_user b, cs_incidents_all_b c
1474       WHERE c.created_by = b.user_id and
1475         b.employee_id = a.source_id and
1476         c.incident_number = l_request_number;
1477 
1478     l_get_create_rsrc_rec l_get_create_rsrc_csr%ROWTYPE;
1479 
1480     l_created_by VARCHAR2(240);
1481 
1482     l_solution_number VARCHAR2(30);
1483     l_solution_summary VARCHAR2(500);
1484 
1485     CURSOR l_getsoln_summary_csr IS
1486         SELECT name from CS_KB_SETS_VL
1487           WHERE set_number = l_solution_number
1488           and status='PUB';
1489 
1490 /* ROOPA - 11/12/03 */
1491     l_request_id_temp NUMBER;
1492     l_notify_contact_name VARCHAR2(200);
1493     l_serviceRequest_URL VARCHAR2(200);
1494 
1495  -- The following cursor is needed to fill in the 3 NEW WF item attributes:
1496  --   1. NTFY_LINKED_SR_STATUS
1497  --   2. NTFY_LINKED_SR_SUMMARY
1498  --   3. NTFY_LINK_TYPE
1499     CURSOR sel_linked_sr_details_csr IS
1500         SELECT a.summary, b.name
1501         FROM cs_incidents_all_vl a,
1502              cs_incident_statuses_vl b
1503         WHERE a.incident_status_id = b.incident_status_id AND
1504               a.incident_id = l_request_id;
1505 
1506     sel_linked_sr_details_rec sel_linked_sr_details_csr%ROWTYPE;
1507 
1508 -- The following cursor is needed to fill in the 1 NEW WF item attributes:
1509   --   1. NTFY_LINK_TYPE
1510     CURSOR sel_linked_sr_type_csr IS
1511         SELECT cilt.name link_type_name
1512         FROM cs_incident_links cil,
1513              CS_SR_LINK_TYPES_VL cilt
1514         WHERE cilt.link_type_id = cil.link_type_id AND
1515               cil.subject_id = l_notify_subject_id AND
1516 			  cil.object_id = l_request_id;
1517 
1518     sel_linked_sr_type_rec sel_linked_sr_type_csr%ROWTYPE;
1519 
1520 
1521 
1522 -- The following cursor is needed to derive the contact's name.
1523 -- At the point of opening this cursor, we already have the contact point id.
1524 -- We use this contact point id to go against the following 2 tables to derive the
1525 -- contact's full name :
1526 --     1) PER_ALL_PEOPLE_F - for contact type = EMPLOYEE
1527 --     2) HZ_RELATIONSHIPS - for contact type = PARTY_RELATIONSHIP
1528 --     3) HZ_PARTIES - for contact type = PERSON
1529 
1530     l_tmp_contact_point_id NUMBER;
1531 
1532    /* CURSOR sel_contact_name_csr IS
1533       SELECT hzp.person_first_name first_name,
1534              hzp.person_last_name last_name,
1535              cshcp.contact_type,
1536              cshcp.contact_point_id
1537       FROM hz_parties hzp,
1538         hz_relationships hzr,
1539         cs_hz_sr_contact_points cshcp
1540       WHERE cshcp.INCIDENT_ID = l_request_id
1541         AND cshcp.sr_contact_point_id = l_tmp_contact_point_id
1542      	AND cshcp.CONTACT_POINT_TYPE = 'EMAIL'
1543         AND cshcp.contact_type = 'PARTY_RELATIONSHIP'
1544         AND cshcp.PARTY_ID = hzr.PARTY_ID
1545         AND hzr.SUBJECT_ID = hzp.PARTY_ID
1546         AND hzr.SUBJECT_TYPE = 'PERSON'
1547       UNION
1548       SELECT hzp.person_first_name first_name,
1549              hzp.person_last_name last_name,
1550              cshcp.contact_type,
1551              cshcp.contact_point_id
1552       FROM hz_parties hzp,
1553            cs_hz_sr_contact_points cshcp
1554       WHERE cshcp.INCIDENT_ID = l_request_id
1555         AND cshcp.sr_contact_point_id = l_tmp_contact_point_id
1556      	AND cshcp.CONTACT_POINT_TYPE = 'EMAIL'
1557         AND cshcp.contact_type = 'PERSON'
1558         AND cshcp.PARTY_ID = hzp.PARTY_ID
1559       UNION
1560       SELECT ppf.first_name first_name,
1561              ppf.last_name last_name,
1562              cshcp.contact_type,
1563              cshcp.contact_point_id
1564       FROM per_all_people_f ppf,
1565            cs_hz_sr_contact_points cshcp
1566       WHERE cshcp.INCIDENT_ID = l_request_id
1567         AND cshcp.sr_contact_point_id = l_tmp_contact_point_id
1568      	AND cshcp.CONTACT_POINT_TYPE = 'EMAIL'
1569         AND cshcp.contact_type = 'EMPLOYEE'
1570         AND cshcp.PARTY_ID = ppf.PERSON_ID;*/
1571 
1572       CURSOR sel_contact_name_csr IS
1573       SELECT hzp.person_first_name first_name,
1574              hzp.person_last_name last_name
1575       FROM hz_parties hzp,
1576         hz_relationships hzr,
1577         cs_hz_sr_contact_points cshcp
1578       WHERE cshcp.INCIDENT_ID = l_request_id
1579         AND cshcp.sr_contact_point_id = l_tmp_contact_point_id
1580      	AND cshcp.CONTACT_POINT_TYPE = 'EMAIL'
1581         AND cshcp.contact_type = 'PARTY_RELATIONSHIP'
1582         AND cshcp.PARTY_ID = hzr.PARTY_ID
1583         AND hzr.SUBJECT_ID = hzp.PARTY_ID
1584         AND hzr.SUBJECT_TYPE = 'PERSON'
1585       UNION  --start bug 12666217
1586       SELECT hzp.person_first_name first_name,
1587              hzp.person_last_name last_name
1588       FROM hz_parties hzp,
1589            cs_hz_sr_contact_points cshcp
1590       WHERE cshcp.INCIDENT_ID = l_request_id
1591         AND cshcp.sr_contact_point_id = l_tmp_contact_point_id
1592      	AND cshcp.CONTACT_POINT_TYPE = 'EMAIL'
1593         AND cshcp.contact_type = 'PERSON'
1594         AND cshcp.PARTY_ID = hzp.PARTY_ID
1595       UNION  -- start Bug  12610714
1596       SELECT ppf.first_name first_name,
1597                     ppf.last_name last_name
1598       From per_all_people_f ppf,
1599                 cs_hz_sr_contact_points cshcp
1600     WHERE cshcp.INCIDENT_ID = l_request_id
1601     AND cshcp.sr_contact_point_id = l_tmp_contact_point_id
1602     AND cshcp.CONTACT_POINT_TYPE = 'EMAIL'
1603     AND cshcp.contact_type = 'EMPLOYEE'
1604     AND cshcp.PARTY_ID = ppf.PERSON_ID
1605      AND ppf.effective_end_date = (select max(ppf1.effective_end_date)
1606                                                        from per_all_people_f ppf1,
1607                                                                   cs_hz_sr_contact_points cshcp1
1608                                                         WHERE cshcp1.INCIDENT_ID = l_request_id
1609                                                          AND cshcp1.sr_contact_point_id = l_tmp_contact_point_id
1610                                                          AND cshcp1.CONTACT_POINT_TYPE = 'EMAIL'
1611                                                          AND cshcp1.contact_type = 'EMPLOYEE'
1612                                                          AND cshcp1.PARTY_ID = ppf1.PERSON_ID);
1613       --end bug 12666217
1614 
1615       /*SELECT first_name first_name,
1616               last_name last_name
1617          FROM (
1618            SELECT ppf.first_name first_name,
1619                   ppf.last_name last_name
1620            FROM per_all_people_f ppf,
1621                 cs_hz_sr_contact_points cshcp
1622            WHERE cshcp.INCIDENT_ID = l_request_id
1623            AND cshcp.sr_contact_point_id = l_tmp_contact_point_id
1624      	   AND cshcp.CONTACT_POINT_TYPE = 'EMAIL'
1625            AND cshcp.contact_type = 'EMPLOYEE'
1626            AND cshcp.PARTY_ID = ppf.PERSON_ID
1627 	   order by   ppf.effective_end_date desc) ;
1628 	-- End Bug 11776536 */
1629 
1630     sel_contact_name_rec sel_contact_name_csr%ROWTYPE;
1631 
1632 l_temp_contact_id_list VARCHAR2(2000);
1633 l_temp_item_key VARCHAR2(100);
1634 
1635 CURSOR check_if_item_attr_exists_csr IS
1636 	select text_value from wf_item_attribute_values
1637 	where item_type='SERVEREQ'
1638         and   name = 'CONTACT_PARTY_ID_LIST'
1639         and item_key = l_temp_item_key;
1640 
1641 
1642     -- There probably is an api to obtain the jsp name given the function,
1643     -- we can replace this cursor with that api when we find out.
1644     CURSOR sel_jsp_name_csr IS
1645       select WEB_HTML_CALL from fnd_form_functions
1646       where FUNCTION_NAME='IBU_SR_DETAILS';
1647     l_jsp_name	VARCHAR2(240);
1648 
1649 --
1650 /* End ROOPA -  11/12/03 */
1651 
1652     CURSOR c_user(p_orig_system IN VARCHAR2, p_orig_system_id IN NUMBER) IS
1653       select name,
1654 	     substrb(display_name,1,360),
1655 	     email_address,
1656              notification_preference,
1657              language
1658       from   wf_users
1659       where  orig_system     = p_orig_system
1660       and    orig_system_id  = p_orig_system_id
1661       order by status, start_date;
1662     l_email_address	VARCHAR2(320);
1663 
1664 
1665    CURSOR c_party_role_csr (p_party_role_code in VARCHAR2) IS
1666       SELECT name
1667         FROM cs_party_roles_vl
1668        WHERE party_role_code = p_party_role_code;
1669 
1670    	-- Cursor to check if the HTML notification is required.
1671 
1672    CURSOR c_check_html_notification IS
1673      SELECT NVL(html_notification,'N')
1674 	   FROM cs_sr_action_details
1675 	  WHERE event_condition_id = l_event_condition_id
1676 	    AND action_code        = l_action_code ;
1677 
1678 
1679   BEGIN
1680 
1681     IF (funmode = 'RUN') THEN
1682 
1683       l_temp_item_key := itemkey;
1684 
1685       l_action_code := WF_ENGINE.GetItemAttrText(
1686                                   itemtype        => itemtype,
1687                                   itemkey         => itemkey,
1688                                   aname           => 'NTFY_ACTION_CODE' );
1689 
1690       l_event_condition_id := WF_ENGINE.GetItemAttrText(
1691                                   itemtype        => itemtype,
1692                                   itemkey         => itemkey,
1693                                   aname           => 'NTFY_EVENT_CONDITION_ID' );
1694 
1695       l_request_number := WF_ENGINE.GetItemAttrText(
1696                                 itemtype        => itemtype,
1697                                 itemkey         => itemkey,
1698                                 aname           => 'REQUEST_NUMBER' );
1699 
1700       l_request_id := WF_ENGINE.GetItemAttrNumber(
1701                                 itemtype        => itemtype,
1702                                 itemkey         => itemkey,
1703                                 aname           => 'REQUEST_ID' );
1704 
1705       /* Roopa - begin
1706           Fix for bug # 2809232
1707       */
1708       OPEN l_sel_curr_sr_details_csr;
1709       FETCH l_sel_curr_sr_details_csr into l_sel_curr_sr_details_rec;
1710       CLOSE l_sel_curr_sr_details_csr;
1711 
1712       l_prev_type_id := WF_ENGINE.GetItemAttrText(
1713                                     itemtype        => itemtype,
1714                                     itemkey         => itemkey,
1715                                     aname           => 'PREV_TYPE_ID' );
1716 
1717       l_prev_status_id :=  WF_ENGINE.GetItemAttrText(
1718                                 itemtype        => itemtype,
1719                                 itemkey         => itemkey,
1720                                 aname           => 'PREV_STATUS_ID' );
1721 
1722       l_prev_severity_id :=  WF_ENGINE.GetItemAttrText(
1723                                 itemtype        => itemtype,
1724                                 itemkey         => itemkey,
1725                                 aname           => 'PREV_SEVERITY_ID' );
1726       l_prev_urgency_id :=  WF_ENGINE.GetItemAttrText(
1727                                 itemtype        => itemtype,
1728                                 itemkey         => itemkey,
1729                                 aname           => 'PREV_URGENCY_ID' );
1730 
1731 
1732       l_prev_summary :=  WF_ENGINE.GetItemAttrText(
1733                                 itemtype        => itemtype,
1734                                 itemkey         => itemkey,
1735                                 aname           => 'PREV_SUMMARY' );
1736 
1737 
1738 
1739 
1740       /****************************
1741        create a list of SR attributes that were updated, which will later
1742        be included in the ntfxn message text.
1743       ****************************/
1744 
1745       FOR i in l_sel_lookup_value_csr LOOP
1746         IF(i.lookup_code = 'CS_SR_SEVERITY') THEN
1747            IF(l_prev_severity_id <> l_sel_curr_sr_details_rec.incident_severity_id) THEN
1748                 l_changed_field := i.meaning;
1749             ELSE
1750                 l_changed_field := null;
1751            END IF;
1752         ELSIF  (i.lookup_code = 'CS_SR_STATUS') THEN
1753            IF(l_prev_status_id <> l_sel_curr_sr_details_rec.incident_status_id) THEN
1754                 l_changed_field := i.meaning;
1755            ELSE
1756                 l_changed_field := null;
1757            END IF;
1758         ELSIF  (i.lookup_code = 'CS_SR_TYPE') THEN
1759            IF(l_prev_type_id <> l_sel_curr_sr_details_rec.incident_type_id) THEN
1760                 l_changed_field := i.meaning;
1761            ELSE
1762                 l_changed_field := null;
1763            END IF;
1764         ELSIF  (i.lookup_code = 'CS_SR_URGENCY') THEN
1765            IF(l_prev_urgency_id <> l_sel_curr_sr_details_rec.incident_urgency_id) THEN
1766                 l_changed_field := i.meaning;
1767            ELSE
1768                 l_changed_field := null;
1769            END IF;
1770         ELSIF  (i.lookup_code = 'CS_SR_SUMMARY') THEN
1771            IF(l_prev_summary <> l_sel_curr_sr_details_rec.summary) THEN
1772                 l_changed_field := i.meaning;
1773            ELSE
1774                 l_changed_field := null;
1775            END IF;
1776         END IF;
1777 
1778         IF(l_changed_field is not null) THEN
1779             IF (l_changed_field_list is NULL) THEN
1780                 l_changed_field_list := l_changed_field;
1781                 l_changed_field := null;
1782             ELSE
1783                 l_changed_field_list := l_changed_field_list || ',' || l_changed_field;
1784                 l_changed_field := null;
1785             END IF;
1786         END IF;
1787 
1788       END LOOP;
1789 
1790       IF(l_changed_field_list is not null) THEN
1791         l_changed_field_list := l_changed_field_list || ' of ';
1792     	  WF_ENGINE.SetItemAttrText(
1793                   itemtype        => itemtype,
1794                   itemkey         => itemkey,
1795                   aname           => 'CHANGED_LIST',
1796                   avalue          => l_changed_field_list );
1797       END IF;
1798 
1799       OPEN l_get_resource_name_csr;
1800       FETCH l_get_resource_name_csr INTO l_get_resource_name_rec;
1801       CLOSE l_get_resource_name_csr;
1802 
1803       l_updated_by := l_get_resource_name_rec.source_first_name || ' '
1804                       || l_get_resource_name_rec.source_last_name;
1805 
1806       WF_ENGINE.SetItemAttrText(
1807                  itemtype        => itemtype,
1808                  itemkey         => itemkey,
1809                  aname           => 'UPDATED_BY',
1810                  avalue          => l_updated_by );
1811 
1812       /* Roopa - end
1813          Fix for bug # 2809232
1814       */
1815 
1816       /* Roopa - begin
1817           Fix for bug # 2809222
1818       */
1819       OPEN l_get_create_rsrc_csr;
1820       FETCH l_get_create_rsrc_csr INTO l_get_create_rsrc_rec;
1821       CLOSE l_get_create_rsrc_csr;
1822 
1823       l_created_by := l_get_resource_name_rec.source_first_name || ' '
1824                       || l_get_resource_name_rec.source_last_name;
1825 
1826       WF_ENGINE.SetItemAttrText(
1827                     itemtype        => itemtype,
1828                     itemkey         => itemkey,
1829                     aname           => 'CREATED_BY',
1830                     avalue          => l_created_by );
1831       /* Roopa - end
1832           Fix for bug # 2809222
1833       */
1834 
1835 
1836       /* Roopa - begin
1837           Fix for bug # 2804495
1838       */
1839       l_solution_number := WF_ENGINE.GetItemAttrText(
1840                   		itemtype        => itemtype,
1841                   		itemkey         => itemkey,
1842                   		aname           => 'SOLUTION_NUMBER');
1843       IF (l_solution_number is not null) THEN
1844           OPEN l_getsoln_summary_csr;
1845           FETCH l_getsoln_summary_csr INTO l_solution_summary;
1846           CLOSE l_getsoln_summary_csr;
1847 
1848           IF(l_solution_summary is not null) THEN
1849              WF_ENGINE.SetItemAttrText(
1850                    itemtype        => itemtype,
1851                    itemkey         => itemkey,
1852                    aname           => 'SOLUTION SUMMARY',
1853                    avalue          => l_solution_summary);
1854 
1855           END IF;
1856 
1857       END IF;
1858 
1859       /* Roopa - end
1860           Fix for bug # 2804495
1861       */
1862 
1863 
1864       IF (l_action_code = 'NOTIFY_OWNER' OR l_action_code = 'NOTIFY_NEW_OWNER') THEN
1865         l_notify_recipient := WF_ENGINE.GetItemAttrText(
1866                 		itemtype        => itemtype,
1867                 		itemkey         => itemkey,
1868                 		aname           => 'OWNER_ROLE');
1869 
1870       ELSIF (l_action_code = 'NOTIFY_OLD_OWNER') THEN
1871         l_notify_recipient := WF_ENGINE.GetItemAttrText(
1872                 		itemtype        => itemtype,
1873                 		itemkey         => itemkey,
1874                 		aname           => 'PREV_OWNER_ROLE');
1875 
1876       ELSIF (l_action_code = 'NOTIFY_OWNER_OF_RELATED_SR') THEN
1877 	l_linked_subject_list := WF_ENGINE.GetItemAttrText(
1878                           		itemtype        => itemtype,
1879                           		itemkey         => itemkey,
1880                           		aname           => 'NTFY_LINKED_SUBJECT_LIST');
1881         IF (l_linked_subject_list IS NOT NULL) THEN
1882           pull_from_list(itemlist => l_linked_subject_list,
1883                          element  => l_element);
1884 
1885 	  l_notify_subject_id := TO_NUMBER(l_element);
1886 
1887 	  WF_ENGINE.SetItemAttrText(
1888               itemtype        => itemtype,
1889               itemkey         => itemkey,
1890               aname           => 'NTFY_LINKED_SUBJECT_LIST',
1891               avalue          => l_linked_subject_list );
1892 
1893 	  WF_ENGINE.SetItemAttrNumber(
1894               itemtype        => itemtype,
1895               itemkey         => itemkey,
1896               aname           => 'NTFY_SUBJECT_ID',
1897               avalue          => l_notify_subject_id );
1898 
1899 	  OPEN sel_link_csr;
1900 	  FETCH sel_link_csr INTO sel_link_rec;
1901 	  CLOSE sel_link_csr;
1902 
1903 
1904           /*
1905               Roopa - begin - bug fix for 2788741
1906           */
1907 
1908           l_user_id := WF_ENGINE.GetItemAttrNumber(
1909                                       itemtype        => itemtype,
1910                                       itemkey         => itemkey,
1911                                       aname           => 'USER_ID' );
1912           OPEN  sel_resource_id;
1913           FETCH sel_resource_id into l_resource_id;
1914           CLOSE sel_resource_id;
1915 
1916           IF(l_resource_id = sel_link_rec.incident_owner_id) THEN
1917 
1918             result := 'COMPLETE:UNSET';
1919             return;
1920           END IF;
1921 
1922           /*
1923               Roopa - end - bug fix for 2788741
1924           */
1925 
1926 	  WF_ENGINE.SetItemAttrText(
1927               itemtype        => itemtype,
1928               itemkey         => itemkey,
1929               aname           => 'NTFY_LINKED_INCIDENT_NUMBER',
1930               avalue          => sel_link_rec.incident_number );
1931 
1932 	  -- Get link Type name from link_type_id
1933 
1934 	  WF_ENGINE.SetItemAttrText(
1935               itemtype        => itemtype,
1936               itemkey         => itemkey,
1937               aname           => 'NTFY_LINK_TYPE',
1938               avalue          => sel_link_rec.link_type_name );
1939 
1940           /* Roopa - begin
1941              Fix for bug # 2788156
1942              The wf role should be checked on the employee/source id and NOT on the incident_owner_id
1943              This cursor will get us the employee id
1944           */
1945 
1946           OPEN l_sel_employee_id_csr;
1947           FETCH l_sel_employee_id_csr into l_employee_id;
1948           CLOSE l_sel_employee_id_csr;
1949 
1950           /* Roopa - end
1951              Fix for bug # 2788156
1952           */
1953 
1954           IF(l_employee_id is not null) THEN
1955 	    CS_WORKFLOW_PUB.Get_Employee_Role (
1956                     p_api_version           =>  1.0,
1957                     p_return_status         =>  l_return_status,
1958                     p_msg_count             =>  l_msg_count,
1959                     p_msg_data              =>  l_msg_data,
1960                     --p_employee_id           =>  l_incident_owner_id,
1961                     p_employee_id           =>  l_employee_id,
1962                     p_role_name             =>  l_owner_role,
1963                     p_role_display_name     =>  l_owner_name );
1964 
1965 	    IF (l_owner_role IS NOT NULL) THEN
1966 	          l_notify_recipient := l_owner_role;
1967 	    END IF;
1968           END iF;
1969 
1970         END IF;	/*** IF (l_linked_subject_list IS NOT NULL) ***/
1971 
1972 
1973 
1974 
1975    /* ROOPA - 11/12/03 */
1976    /* 11.5.10 enhancement - we have added 2 new action codes:
1977         1. NOTIFY_PRIM_CONTACT_OF_REL_SR - notify the prim contact of all related SRs
1978         2. NOTIFY_ALL_CONTACTS_OF_REL_SR - notify all contacts of all related SRs
1979 
1980       Facts
1981       ------
1982       1. The above 2 actions are associated only with "Service Request Status Changed" event.
1983 
1984       2. If the SR's status changes to/from a particular status AND if the SR has a specified
1985          relationship with it's linked SRs, only then these 2 actions will be executed.
1986 
1987       3. NOTIFY_PRIM_CONTACT_OF_REL_SR notifies only the primary contacts of related SRs while
1988          NOTIFY_ALL_CONTACTS_OF_REL_SR notifies all contacts of all related SRs
1989 
1990          The following block handles notification rules defined for these 2 actions
1991    */
1992    ELSIF (l_action_code = 'NOTIFY_PRIM_CONTACT_OF_REL_SR') THEN
1993 
1994 	l_linked_subject_list := WF_ENGINE.GetItemAttrText(
1995                           		itemtype        => itemtype,
1996                           		itemkey         => itemkey,
1997                            		aname           => 'NTFY_LINKED_SUBJECT_LIST');
1998 
1999         -- Check if the html_notification flag is set
2000 
2001           OPEN c_check_html_notification;
2002          FETCH c_check_html_notification INTO l_html_notification_flag ;
2003          CLOSE c_check_html_notification;
2004 
2005 --    LOOP
2006 
2007       IF (l_linked_subject_list IS NOT NULL) THEN
2008 
2009           pull_from_list(itemlist => l_linked_subject_list,
2010                              element  => l_element);
2011 
2012 	  l_notify_subject_id := TO_NUMBER(l_element);
2013 
2014 	  WF_ENGINE.SetItemAttrText(
2015               itemtype        => itemtype,
2016               itemkey         => itemkey,
2017               aname           => 'NTFY_LINKED_SUBJECT_LIST',
2018               avalue          => l_linked_subject_list );
2019 
2020 	  WF_ENGINE.SetItemAttrNumber(
2021               itemtype        => itemtype,
2022               itemkey         => itemkey,
2023               aname           => 'NTFY_SUBJECT_ID',
2024               avalue          => l_notify_subject_id );
2025 
2026 	  OPEN sel_link_csr;
2027 	  FETCH sel_link_csr INTO sel_link_rec;
2028 	  CLOSE sel_link_csr;
2029 
2030            OPEN sel_linked_sr_type_csr;
2031           FETCH sel_linked_sr_type_csr INTO sel_linked_sr_type_rec;
2032           CLOSE sel_linked_sr_type_csr;
2033 
2034 
2035           l_user_id := WF_ENGINE.GetItemAttrNumber(
2036                                      itemtype        => itemtype,
2037                                      itemkey         => itemkey,
2038                                      aname           => 'USER_ID' );
2039 
2040 	  WF_ENGINE.SetItemAttrText(
2041               itemtype        => itemtype,
2042               itemkey         => itemkey,
2043               aname           => 'NTFY_LINKED_INCIDENT_NUMBER',
2044               avalue          => sel_link_rec.incident_number );
2045 
2046 
2047           -- Get link Type name from link_type_id
2048 
2049             WF_ENGINE.SetItemAttrText(
2050                       itemtype        => itemtype,
2051                       itemkey         => itemkey,
2052                       aname           => 'NTFY_LINK_TYPE',
2053                       avalue          => sel_linked_sr_type_rec.link_type_name );
2054 
2055 
2056               l_request_id := l_notify_subject_id;
2057 
2058               OPEN sel_linked_sr_details_csr;
2059               FETCH sel_linked_sr_details_csr into sel_linked_sr_details_rec;
2060               CLOSE sel_linked_sr_details_csr;
2061 
2062               WF_ENGINE.SetItemAttrText(
2063                       itemtype        => itemtype,
2064                       itemkey         => itemkey,
2065                       aname           => 'NTFY_LINKED_SR_STATUS',
2066                       avalue          => sel_linked_sr_details_rec.name );
2067 
2068               WF_ENGINE.SetItemAttrText(
2069                       itemtype        => itemtype,
2070                       itemkey         => itemkey,
2071                       aname           => 'NTFY_LINKED_SR_SUMMARY',
2072                       avalue          => sel_linked_sr_details_rec.summary );
2073 
2074 
2075                 OPEN get_primary_contact_info;
2076 
2077         	 LOOP
2078                     FETCH get_primary_contact_info
2079                      INTO l_contact_party_id,l_contact_type,l_tmp_contact_point_id,l_sr_contact_point_type,l_sr_contact_point_id;
2080                     EXIT WHEN get_primary_contact_info%NOTFOUND;
2081 
2082                     -- get the email address from the CS schema if l_contact_type is not EMAIL.
2083 
2084                        -- Get primary contact point ID from cs schema
2085 
2086                        IF l_sr_contact_point_type <> 'EMAIL' THEN
2087 
2088                            OPEN get_primary_email_info_CS (l_contact_party_id) ;
2089                           FETCH get_primary_email_info_CS INTO l_pri_sr_contact_point_id, l_pri_email_cont_pt_id ;
2090                           CLOSE get_primary_email_info_CS;
2091 
2092                        ELSE
2093                          l_pri_sr_contact_point_id := l_tmp_contact_point_id;
2094                          l_pri_email_cont_pt_id    := l_sr_contact_point_id;
2095                        END IF ;
2096 
2097                        -- get the email address if contact point id found in CS schema.
2098 
2099                        IF l_pri_email_cont_pt_id IS NOT NULL THEN
2100 
2101                           IF (l_contact_type = 'PERSON' OR l_contact_type = 'PARTY_RELATIONSHIP') THEN
2102 
2103                                OPEN get_primary_email_info_HZ (l_pri_email_cont_pt_id,l_contact_party_id);
2104                               FETCH get_primary_email_info_HZ INTO l_contact_email;
2105                               CLOSE get_primary_email_info_HZ;
2106 
2107                           ELSIF l_contact_type = 'EMPLOYEE' THEN
2108                                OPEN get_primary_email_info_HR (l_contact_party_id);
2109                               FETCH get_primary_email_info_HR INTO l_contact_email;
2110                               CLOSE get_primary_email_info_HR;
2111 
2112                           END IF;
2113                        END IF;
2114 
2115                     -- This section gets the workflow user of the contact.
2116 
2117                        IF (l_contact_type = 'EMPLOYEE') THEN
2118                            l_orig_system := 'PER';
2119                            l_orig_system_id := l_contact_party_id; -- l_person_id;
2120                        ELSE
2121                            l_orig_system := 'HZ_PARTY';
2122                            l_orig_system_id := l_contact_party_id;
2123                        END IF;
2124 
2125                         OPEN c_user(l_orig_system,l_orig_system_id);
2126                        FETCH c_user INTO l_user, l_user_display_name, l_email_address, l_notification_preference,l_language;
2127                        CLOSE c_user;
2128 
2129                        IF (l_email_address IS NOT NULL) AND (l_contact_email IS NULL) THEN
2130                           l_contact_email := l_email_address;
2131 
2132                        END IF ;
2133 
2134                     -- This section finds some email address of customer contact. if email address is not present
2135                     -- with the WF role or as a contact point in the CS schama
2136 
2137                        IF ((l_email_address IS NULL) AND (l_contact_email IS NULL) AND (l_contact_type <> 'EMPLOYEE')) THEN
2138 
2139                           FOR get_any_cust_email_address_rec IN get_any_cust_email_address(l_contact_party_id)
2140                               LOOP
2141                                  l_contact_email := get_any_cust_email_address_rec.email_address;
2142                                  EXIT WHEN get_any_cust_email_address%FOUND;
2143                               END LOOP;
2144                        END IF ;
2145 
2146                     -- Check for WF email list length not to exceed 2000
2147 
2148         	    IF (nvl(LENGTH(l_contact_email_list), 0) + nvl(LENGTH(l_contact_email),0) + 1) <= 2000 THEN
2149 
2150                          IF (l_contact_email IS NOT NULL)THEN
2151 
2152                             IF l_contact_type = 'PERSON' THEN
2153 
2154                                 OPEN get_prim_cont_party_info_per(l_contact_party_id) ;
2155                                FETCH get_prim_cont_party_info_per INTO l_contact_first_name , l_contact_last_name;
2156                                CLOSE get_prim_cont_party_info_per;
2157 
2158                             ELSIF l_contact_type = 'PARTY_RELATIONSHIP' THEN
2159 
2160                                 OPEN get_prim_cont_party_info_reln(l_contact_party_id) ;
2161                                FETCH get_prim_cont_party_info_reln INTO l_contact_first_name , l_contact_last_name;
2162                                CLOSE get_prim_cont_party_info_reln;
2163 
2164                             ELSIF l_contact_type = 'EMPLOYEE' THEN
2165 
2166                                 OPEN get_prim_cont_party_info_empl(l_contact_party_id) ;
2167                                FETCH get_prim_cont_party_info_empl INTO l_contact_first_name , l_contact_last_name;
2168                                CLOSE get_prim_cont_party_info_empl;
2169 
2170                             END IF;
2171 
2172                            l_notify_contact_name := l_contact_first_name|| ' ' ||l_contact_last_name;
2173 
2174 
2175                               WF_ENGINE.SetItemAttrText(
2176                                       itemtype        => itemtype,
2177                                       itemkey         => itemkey,
2178                                       aname           => 'NTFY_LINKED_SR_CONTACT',
2179                                       avalue          => l_notify_contact_name );
2180 
2181                               OPEN sel_jsp_name_csr;
2182                 	      FETCH sel_jsp_name_csr INTO l_jsp_name;
2183                 	      CLOSE sel_jsp_name_csr;
2184 
2185                 	      l_serviceRequest_URL := FND_PROFILE.Value('JTF_BIS_OA_HTML') || '/' || l_jsp_name
2186         				      || '?srID=' || sel_link_rec.incident_number;
2187 
2188                               --l_serviceRequest_URL := FND_PROFILE.Value('JTF_BIS_OA_HTML') || '/ibuSRDetails.jsp?srID=' || sel_link_rec.incident_number;
2189                               WF_ENGINE.SetItemAttrText(
2190                                       itemtype        => itemtype,
2191                                       itemkey         => itemkey,
2192                                       aname           => 'NTFY_REQUEST_NUMBER_URL',
2193                                       avalue          => l_serviceRequest_URL);
2194 
2195 
2196                     	      IF (l_contact_email_list IS NULL) THEN
2197 	                              l_contact_email_list := l_contact_email ;
2198             	              ELSE
2199 	                              l_contact_email_list := l_contact_email_list ||','||l_contact_email;
2200                               END IF;
2201 
2202                               IF (l_html_contact_email_list IS NULL) THEN
2203 	                         l_html_contact_email_list := l_contact_email ;
2204 	                      ELSE
2205 	                         l_html_contact_email_list := l_html_contact_email_list ||' '||l_contact_email;
2206 	                      END IF;
2207 
2208                 	      -- This section gets the workflow user of the contact, if none exist create an adhoc.
2209                               -- If contact does not have workflow user, create an adhoc user.
2210                               -- Adhoc user name is re-used for performance.
2211                 	      -- The same party_id could have different contact info, but workflow schema
2212                 	      -- only has one user/role which is the party in hz_party.
2213 
2214                               IF (l_user IS NULL OR
2215 	                	  (l_user is not null and instr(l_adhoc_user_list,l_user) > 0) OR
2216                 		  (l_user is not null and l_email_address <> l_contact_email) ) THEN
2217 
2218                 		adhoc_count := adhoc_count + 1;
2219 
2220                                 l_user := 'CS_WF_CONTACT_USER_DUMMY' || to_char(adhoc_count) ;
2221 
2222                 		-- need to pass display name also, otherwise the 'To' field in mail and ntfxn
2223 	                	-- will show the adhoc name.
2224 
2225                                   l_user_display_name := l_contact_first_name || ', ' || l_contact_last_name;
2226 
2227                                 SetWorkflowAdhocUser(p_wf_username      => l_user,
2228                                                      p_email_address    => l_contact_email,
2229 				                     p_display_name	=> l_user_display_name);
2230 
2231                               END IF;
2232 
2233                               IF (l_adhoc_user_list IS NULL) THEN
2234                                l_adhoc_user_list := l_user ;
2235                               ELSE
2236                                l_adhoc_user_list := l_adhoc_user_list ||','||l_user;
2237                               END IF;
2238 
2239                               -- Added code for HTML Notification  (01/30/2006) release 12.0
2240 
2241                               IF l_html_notification_flag = 'Y' THEN
2242 
2243   		                 IF l_html_adhoc_user_list IS NULL THEN
2244 				    l_html_adhoc_user_list  := l_user;
2245 				 ELSE
2246 				    l_html_adhoc_user_list  := l_html_adhoc_user_list ||' ' ||l_user;
2247 				 END IF ;
2248 
2249                                  IF l_contact_type_list IS NULL THEN
2250                                     IF l_contact_type = 'EMPLOYEE' THEN
2251                                        l_contact_type_list := 'EMP';
2252                                     ELSE -- IF l_contact_type = 'HZ_PARTY' THEN
2253                                        l_contact_type_list := 'PARTY';
2254                                     END IF;
2255                                  ELSE
2256                                     IF l_contact_type = 'EMPLOYEE' THEN
2257                                        l_contact_type_list := l_contact_type_list||' '||'EMP';
2258                                     ELSE -- IF l_contact_type = 'HZ_PARTY' THEN
2259                                        l_contact_type_list := l_contact_type_list||' '||'PARTY';
2260                                     END IF;
2261                                  END IF ;
2262 
2263                                  IF l_contact_id_list IS NULL THEN
2264                                     IF l_contact_type = 'EMPLOYEE' THEN
2265                                        l_contact_id_list := NVL(l_person_id,l_contact_party_id);
2266                                     ELSE -- IF l_contact_type = 'HZ_PARTY' THEN
2267                                        l_contact_id_list := l_contact_party_id;
2268                                     END IF;
2269                                  ELSE
2270                                     IF l_contact_type = 'EMPLOYEE' THEN
2271                                        l_contact_id_list := l_contact_id_list||' '||NVL(l_person_id,l_contact_party_id);
2272                                     ELSE -- IF l_contact_type = 'HZ_PARTY' THEN
2273                                        l_contact_id_list := l_contact_id_list||' '||l_contact_party_id;
2274                                     END IF;
2275                                  END IF ;
2276 
2277                                  IF l_notification_pref_list IS NULL THEN
2278                                     l_notification_pref_list := NVL(l_notification_preference,'MAILTEXT');
2279                                  ELSE
2280                                     l_notification_pref_list := l_notification_pref_list||' '||NVL(l_notification_preference,'MAILTEXT');
2281                                  END IF ;
2282 
2283                                IF l_language_list IS NULL THEN
2284                                     l_language_list := NVL(l_language,'AMERICAN');
2285                                  ELSE
2286                                     l_language_list := l_language_list||' '||NVL(l_language,'AMERICAN');
2287                                  END IF ;
2288                               END IF ;   -- end if for l_html_notification_flag
2289 
2290                               -- 11.5.10 enhancement for create interaction when email to contact sent.
2291 
2292                               IF l_contact_type <> 'EMPLOYEE' THEN
2293 
2294       	                         IF (l_contact_party_id_list IS NULL) THEN
2295 	           	             l_contact_party_id_list := TO_CHAR(l_contact_party_id);
2296                     	         ELSE
2297         	                     l_contact_party_id_list := l_contact_party_id_list || ' '
2298                             	     || TO_CHAR(l_contact_party_id);
2299     	                         END IF;
2300                               END IF;
2301 
2302 	                      -- end 11.5.10 enhancement.
2303                          END IF;
2304 
2305 	            ELSE
2306 	               EXIT;
2307 	            END IF;
2308 
2309 	         END LOOP;
2310               CLOSE get_primary_contact_info;
2311 
2312               l_request_id := WF_ENGINE.GetItemAttrNumber(
2313                                 itemtype        => itemtype,
2314                                 itemkey         => itemkey,
2315                                 aname           => 'REQUEST_ID' );
2316 
2317               -- Fix for bug 3392429. Only create ADHOC role and notification if there
2318               -- is/are email addresses, since we send notifications for contacts via
2319               -- email only. rmanabat 02/12/04 .
2320               IF (l_contact_email_list IS NOT NULL) THEN
2321 
2322          	 -- If there's just one user in the list, then make the recipient that user.
2323         	 IF (instr(l_adhoc_user_list, ',') = 0) THEN
2324         	   l_notify_recipient := l_adhoc_user_list;
2325         	 ELSE
2326         	   -- We send mass notifications to one ADHOC role which has the list of
2327         	   -- contact's workflow users.
2328         	   l_adhoc_role := 'CS_WF_CONTACT_ROLE_DUMMY';
2329                    SetWorkflowAdhocRole(p_wf_rolename      => l_adhoc_role,
2330                                         p_user_list        => l_adhoc_user_list);
2331                    l_notify_recipient := l_adhoc_role;
2332 	         END IF;
2333 
2334               ELSE
2335                  l_notify_recipient := NULL;
2336               END IF;
2337 
2338 
2339               -- 11.5.10 enhancement to Create Interaction when email to contact sent.
2340               /* Roopa - begin bug 3360069 */
2341               /* Replacing the AddItemAttr() call with WF_Engine add, get and set calls
2342                  to factor in the scenario where this attr already exists */
2343 
2344               IF (l_contact_party_id_list IS NOT NULL) THEN
2345 
2346                   OPEN check_if_item_attr_exists_csr;
2347            	  FETCH check_if_item_attr_exists_csr into l_temp_contact_id_list;
2348 
2349 
2350         	  /****
2351            	  l_temp_contact_id_list := null;
2352         	  l_temp_contact_id_list := WF_ENGINE.GetItemAttrText( itemtype,
2353 				                itemkey,
2354 				   		'CONTACT_PARTY_ID_LIST',
2355 				   		TRUE);
2356 
2357 
2358  	          IF(l_temp_contact_id_list is NULL) THEN
2359         	  *****/
2360 
2361                   IF(check_if_item_attr_exists_csr%NOTFOUND) THEN
2362         	    WF_ENGINE.AddItemAttr( itemtype,
2363         	  			 itemkey,
2364         	  			 'CONTACT_PARTY_ID_LIST',
2365         				 l_contact_party_id_list);
2366         	  ELSE
2367 
2368                     WF_ENGINE.SetItemAttrText(
2369                               itemtype        => itemtype,
2370                               itemkey         => itemkey,
2371                               aname           => 'CONTACT_PARTY_ID_LIST',
2372                               avalue          => l_contact_party_id_list);
2373 
2374 
2375         	  END IF;
2376 
2377                   CLOSE check_if_item_attr_exists_csr;
2378 
2379               END IF;
2380               /* Roopa - End bug 3360069 */
2381 
2382       ELSE
2383         null;
2384       END IF;	/*** IF (l_linked_subject_list IS NOT NULL) ***/
2385 
2386 --    END LOOP;
2387 
2388               -- set the item attributes for html notification
2389 
2390         IF l_html_notification_flag = 'Y' THEN
2391                   WF_ENGINE.SetItemAttrText(
2392                                  itemtype  => itemtype,
2393                                  itemkey   => itemkey,
2394                                  AName  => 'ADHOC_USER_LIST',
2395                                  AValue => l_html_adhoc_user_list);
2396 
2397                   WF_ENGINE.SetItemAttrText(
2398                                  itemtype  => itemtype,
2399                                  itemkey   => itemkey,
2400                                  AName  => 'CONTACT_ID_LIST',
2401                                  AValue => l_contact_id_list);
2402 
2403                   WF_ENGINE.SetItemAttrText(
2404                                  itemtype  => itemtype,
2405                                  itemkey   => itemkey,
2406                                  AName  => 'CONTACT_EMAIL_LIST',
2407                                  AValue => l_html_contact_email_list);
2408 
2409                   WF_ENGINE.SetItemAttrText(
2410                                  itemtype  => itemtype,
2411                                  itemkey   => itemkey,
2412                                  AName  => 'CONTACT_TYPE_LIST',
2413                                  AValue => l_contact_type_list);
2414 
2415                   WF_ENGINE.SetItemAttrText(
2416                                  itemtype  => itemtype,
2417                                  itemkey   => itemkey,
2418                                  AName  => 'NOTIFICATION_PREFERENCE_LIST',
2419                                  AValue => l_notification_pref_list);
2420 
2421                   WF_ENGINE.SetItemAttrText(
2422                                  itemtype  => itemtype,
2423                                  itemkey   => itemkey,
2424                                  AName  => 'LANGUAGE_LIST',
2425                                  AValue => l_language_list);
2426         END IF;
2427 
2428    ELSIF (l_action_code = 'NOTIFY_ALL_CONTACTS_OF_REL_SR') THEN
2429 
2430           OPEN c_check_html_notification;
2431          FETCH c_check_html_notification INTO l_html_notification_flag ;
2432          CLOSE c_check_html_notification;
2433 
2434 	l_linked_subject_list := WF_ENGINE.GetItemAttrText(
2435                           		itemtype        => itemtype,
2436                           		itemkey         => itemkey,
2437                            		aname           => 'NTFY_LINKED_SUBJECT_LIST');
2438 
2439 
2440 --    LOOP
2441       IF (l_linked_subject_list IS NOT NULL) THEN
2442               pull_from_list(itemlist => l_linked_subject_list,
2443                              element  => l_element);
2444 
2445 	  l_notify_subject_id := TO_NUMBER(l_element);
2446 
2447 	  WF_ENGINE.SetItemAttrText(
2448               itemtype        => itemtype,
2449               itemkey         => itemkey,
2450               aname           => 'NTFY_LINKED_SUBJECT_LIST',
2451               avalue          => l_linked_subject_list );
2452 
2453 	  WF_ENGINE.SetItemAttrNumber(
2454               itemtype        => itemtype,
2455               itemkey         => itemkey,
2456               aname           => 'NTFY_SUBJECT_ID',
2457               avalue          => l_notify_subject_id );
2458 
2459 	  OPEN sel_link_csr;
2460 	  FETCH sel_link_csr INTO sel_link_rec;
2461 	  CLOSE sel_link_csr;
2462 
2463 	  OPEN sel_linked_sr_type_csr;
2464 	  FETCH sel_linked_sr_type_csr INTO sel_linked_sr_type_rec;
2465 	  CLOSE sel_linked_sr_type_csr;
2466 
2467           l_user_id := WF_ENGINE.GetItemAttrNumber(
2468                                      itemtype        => itemtype,
2469                                      itemkey         => itemkey,
2470                                      aname           => 'USER_ID' );
2471 
2472 	  WF_ENGINE.SetItemAttrText(
2473               itemtype        => itemtype,
2474               itemkey         => itemkey,
2475               aname           => 'NTFY_LINKED_INCIDENT_NUMBER',
2476               avalue          => sel_link_rec.incident_number );
2477 
2478 	  -- Get link Type name from link_type_id
2479 
2480 	  WF_ENGINE.SetItemAttrText(
2481               itemtype        => itemtype,
2482               itemkey         => itemkey,
2483               aname           => 'NTFY_LINK_TYPE',
2484               avalue          => sel_linked_sr_type_rec.link_type_name );
2485 
2486 
2487       l_request_id := l_notify_subject_id;
2488       OPEN sel_all_contacts_csr;
2489 	  LOOP
2490 
2491 	    FETCH sel_all_contacts_csr
2492 	    INTO l_contact_email, l_contact_party_id, l_person_id, l_contact_type, l_tmp_contact_point_id;
2493 	    EXIT WHEN sel_all_contacts_csr%NOTFOUND;
2494 
2495 	    -- Check for WF email list length not to exceed 2000
2496 	    IF (nvl(LENGTH(l_contact_email_list), 0) + nvl(LENGTH(l_contact_email),0) + 1) <= 2000 THEN
2497 
2498 
2499        	  IF (l_contact_email IS NOT NULL)THEN
2500               OPEN sel_contact_name_csr;
2501               FETCH sel_contact_name_csr INTO sel_contact_name_rec;
2502               CLOSE sel_contact_name_csr;
2503 
2504               l_notify_contact_name := sel_contact_name_rec.first_name || ' ' || sel_contact_name_rec.last_name;
2505               WF_ENGINE.SetItemAttrText(
2506                       itemtype        => itemtype,
2507                       itemkey         => itemkey,
2508                       aname           => 'NTFY_LINKED_SR_CONTACT',
2509                       avalue          => l_notify_contact_name );
2510 
2511               OPEN sel_jsp_name_csr;
2512 	      FETCH sel_jsp_name_csr INTO l_jsp_name;
2513 	      CLOSE sel_jsp_name_csr;
2514 
2515 	      l_serviceRequest_URL := FND_PROFILE.Value('JTF_BIS_OA_HTML') || '/' || l_jsp_name
2516 				      || '?srID=' || l_request_number;
2517 
2518             --l_serviceRequest_URL := FND_PROFILE.Value('JTF_BIS_OA_HTML') || '/ibuSRDetails.jsp?srID=' || l_request_number;
2519               WF_ENGINE.SetItemAttrText(
2520                       itemtype        => itemtype,
2521                       itemkey         => itemkey,
2522                       aname           => 'NTFY_REQUEST_NUMBER_URL',
2523                       avalue          => l_serviceRequest_URL);
2524 
2525     	      IF (l_contact_email_list IS NULL) THEN
2526 	              l_contact_email_list := l_contact_email ;
2527     	      ELSE
2528         	       l_contact_email_list := l_contact_email_list ||','||l_contact_email;
2529     	      END IF;
2530 
2531 
2532               IF (l_html_contact_email_list IS NULL) THEN
2533 	          l_html_contact_email_list := l_contact_email ;
2534 	      ELSE
2535 	          l_html_contact_email_list := l_html_contact_email_list ||' '||l_contact_email;
2536 	      END IF;
2537 
2538 	      -- This section gets the workflow user of the contact, if none exist create an adhoc.
2539               IF (l_contact_type = 'EMPLOYEE') THEN
2540                 l_orig_system := 'PER';
2541                 l_orig_system_id := l_person_id;
2542               ELSE
2543                 l_orig_system := 'HZ_PARTY';
2544                 l_orig_system_id := l_contact_party_id;
2545               END IF;
2546 
2547 	      /****
2548               WF_DIRECTORY.GetUserName( p_orig_system           => l_orig_system,
2549                                         p_orig_system_id        => l_orig_system_id,
2550                                         p_name                  => l_user,
2551                                         p_display_name          => l_user_display_name);
2552 	      ***/
2553 
2554 	      OPEN c_user(l_orig_system,l_orig_system_id);
2555 	      FETCH c_user INTO l_user, l_user_display_name, l_email_address, l_notification_preference,l_language;
2556 	      CLOSE c_user;
2557 
2558               -- If contact does not have workflow user, create an adhoc user.
2559               -- Adhoc user name is re-used for performance.
2560 	      -- The same party_id could have different contact info, but workflow schema
2561 	      -- only has one user/role which is the party in hz_party.
2562 
2563               IF (l_user IS NULL OR
2564 		  (l_user is not null and instr(l_adhoc_user_list,l_user) > 0) OR
2565 		  (l_user is not null and l_email_address <> l_contact_email) ) THEN
2566 
2567 		adhoc_count := adhoc_count + 1;
2568 
2569                 l_user := 'CS_WF_CONTACT_USER_DUMMY' || to_char(adhoc_count) ;
2570 
2571 		-- need to pass display name also, other wise the 'To' field in mail and ntfxn
2572 		-- will show the adhoc name.
2573                 l_user_display_name := sel_contact_name_rec.last_name || ', ' || sel_contact_name_rec.first_name;
2574 
2575                 SetWorkflowAdhocUser(p_wf_username      => l_user,
2576                                      p_email_address    => l_contact_email,
2577 				     p_display_name	=> l_user_display_name);
2578 
2579               END IF;
2580 
2581 
2582               IF (l_adhoc_user_list IS NULL) THEN
2583                l_adhoc_user_list := l_user ;
2584               ELSE
2585                l_adhoc_user_list := l_adhoc_user_list ||','||l_user;
2586               END IF;
2587 
2588 
2589               -- Added code for HTML Notification  (01/30/2006) release 12.0
2590 
2591               IF l_html_notification_flag = 'Y' THEN
2592 
2593 		 IF l_html_adhoc_user_list IS NULL THEN
2594 		    l_html_adhoc_user_list  := l_user;
2595 		 ELSE
2596 		    l_html_adhoc_user_list  := l_html_adhoc_user_list ||' ' ||l_user;
2597 		 END IF ;
2598 
2599                  IF l_contact_type_list IS NULL THEN
2600                     IF l_contact_type = 'EMPLOYEE' THEN
2601                        l_contact_type_list := 'EMP';
2602                     ELSE -- IF l_contact_type = 'HZ_PARTY' THEN
2603                        l_contact_type_list := 'PARTY';
2604                     END IF;
2605                  ELSE
2606                     IF l_contact_type = 'EMPLOYEE' THEN
2607                        l_contact_type_list := l_contact_type_list||' '||'EMP';
2608                     ELSE -- IF l_contact_type = 'HZ_PARTY' THEN
2609                        l_contact_type_list := l_contact_type_list||' '||'PARTY';
2610                     END IF;
2611                  END IF ;
2612 
2613                  IF l_contact_id_list IS NULL THEN
2614                      IF l_contact_type = 'EMPLOYEE' THEN
2615                         l_contact_id_list := l_person_id;
2616                      ELSE -- IF l_contact_type = 'HZ_PARTY' THEN
2617                         l_contact_id_list := l_contact_party_id;
2618                      END IF;
2619                  ELSE
2620                      IF l_contact_type = 'EMPLOYEE' THEN
2621                         l_contact_id_list := l_contact_id_list||' '||l_person_id;
2622                      ELSE -- IF l_contact_type = 'HZ_PARTY' THEN
2623                         l_contact_id_list := l_contact_id_list||' '||l_contact_party_id;
2624                      END IF;
2625                  END IF ;
2626 
2627                  IF l_notification_pref_list IS NULL THEN
2628                     l_notification_pref_list := NVL(l_notification_preference,'MAILTEXT');
2629                  ELSE
2630                     l_notification_pref_list := l_notification_pref_list||' '||NVL(l_notification_preference,'MAILTEXT');
2631                  END IF ;
2632 
2633                  IF l_language_list IS NULL THEN
2634                     l_language_list := NVL(l_language,'AMERICAN');
2635                  ELSE
2636                     l_language_list := l_language_list||' '||NVL(l_language,'AMERICAN');
2637                  END IF ;
2638 
2639                END IF ;   -- end if for l_html_notification_flag
2640 
2641 	      -- 11.5.10 enhancement for create interaction when email to contact sent.
2642 
2643     	      IF (l_contact_party_id_list IS NULL) THEN
2644         		l_contact_party_id_list := TO_CHAR(l_contact_party_id);
2645     	      ELSE
2646         		l_contact_party_id_list := l_contact_party_id_list || ' '
2647         		  || TO_CHAR(l_contact_party_id);
2648     	      END IF;
2649 	      -- end 11.5.10 enhancement.
2650           END IF;
2651 
2652 	    ELSE
2653 	      exit;
2654 	    END IF;
2655 	  END LOOP;
2656       CLOSE sel_all_contacts_csr;
2657 
2658       l_request_id := WF_ENGINE.GetItemAttrNumber(
2659                                 itemtype        => itemtype,
2660                                 itemkey         => itemkey,
2661                                 aname           => 'REQUEST_ID' );
2662 
2663       -- Fix for bug 3392429. Only create ADHOC role and notification if there
2664       -- is/are email addresses, since we send notifications for contacts via
2665       -- email only. rmanabat 02/12/04 .
2666 
2667       IF (l_contact_email_list IS NOT NULL) THEN
2668 
2669 	-- If there's just one user in the list, then make the recipient that user.
2670 
2671 	IF (instr(l_adhoc_user_list, ',') = 0) THEN
2672 	  l_notify_recipient := l_adhoc_user_list;
2673 	ELSE
2674 	  -- We send mass notifications to one ADHOC role which has the list of
2675 	  -- contact's workflow users.
2676 	  l_adhoc_role := 'CS_WF_CONTACT_ROLE_DUMMY';
2677           SetWorkflowAdhocRole(p_wf_rolename      => l_adhoc_role,
2678                                p_user_list        => l_adhoc_user_list);
2679           l_notify_recipient := l_adhoc_role;
2680 	END IF;
2681 
2682       ELSE
2683         l_notify_recipient := NULL;
2684       END IF;
2685 
2686       -- 11.5.10 enhancement to Create Interaction when email to contact sent.
2687       /* Roopa - begin bug 3360069 */
2688       /* Replacing the AddItemAttr() call with WF_Engine add, get and set calls
2689          to factor in the scenario where this attr already exists */
2690 
2691       IF (l_contact_party_id_list IS NOT NULL) THEN
2692 
2693         OPEN check_if_item_attr_exists_csr;
2694 	FETCH check_if_item_attr_exists_csr into l_temp_contact_id_list;
2695 
2696 	/****
2697             l_temp_contact_id_list := null;
2698 	    l_temp_contact_id_list := WF_ENGINE.GetItemAttrText( itemtype,
2699 				                itemkey,
2700 				   		'CONTACT_PARTY_ID_LIST',
2701 				   		TRUE);
2702 
2703 	   IF(l_temp_contact_id_list is NULL) THEN
2704 	*****/
2705 
2706 	IF(check_if_item_attr_exists_csr%NOTFOUND) THEN
2707 	  WF_ENGINE.AddItemAttr( itemtype,
2708 				 itemkey,
2709 				 'CONTACT_PARTY_ID_LIST',
2710 				 l_contact_party_id_list);
2711  	ELSE
2712           WF_ENGINE.SetItemAttrText(
2713                       itemtype        => itemtype,
2714                       itemkey         => itemkey,
2715                       aname           => 'CONTACT_PARTY_ID_LIST',
2716                       avalue          => l_contact_party_id_list);
2717         END IF;
2718 	CLOSE check_if_item_attr_exists_csr;
2719 
2720       END IF;
2721 
2722       /* Roopa - End bug 3360069 */
2723 
2724     ELSE
2725         null;
2726     END IF;	/*** IF (l_linked_subject_list IS NOT NULL) ***/
2727 
2728 --    END LOOP;
2729 
2730      -- set the item attributes for html notification
2731 
2732      IF l_html_notification_flag = 'Y' THEN
2733 
2734         WF_ENGINE.SetItemAttrText(
2735                      itemtype  => itemtype,
2736                      itemkey   => itemkey,
2737                      AName  => 'ADHOC_USER_LIST',
2738                      AValue => l_html_adhoc_user_list);
2739 
2740         WF_ENGINE.SetItemAttrText(
2741                      itemtype  => itemtype,
2742                      itemkey   => itemkey,
2743                      AName  => 'CONTACT_ID_LIST',
2744                      AValue => l_contact_id_list);
2745 
2746         WF_ENGINE.SetItemAttrText(
2747                      itemtype  => itemtype,
2748                      itemkey   => itemkey,
2749                      AName  => 'CONTACT_EMAIL_LIST',
2750                      AValue => l_html_contact_email_list);
2751 
2752         WF_ENGINE.SetItemAttrText(
2753                      itemtype  => itemtype,
2754                      itemkey   => itemkey,
2755                      AName  => 'CONTACT_TYPE_LIST',
2756                      AValue => l_contact_type_list);
2757 
2758         WF_ENGINE.SetItemAttrText(
2759                      itemtype  => itemtype,
2760                      itemkey   => itemkey,
2761                      AName  => 'NOTIFICATION_PREFERENCE_LIST',
2762                      AValue => l_notification_pref_list);
2763 
2764         WF_ENGINE.SetItemAttrText(
2765                      itemtype  => itemtype,
2766                      itemkey   => itemkey,
2767                      AName  => 'LANGUAGE_LIST',
2768                      AValue => l_language_list);
2769 
2770         END IF;
2771 
2772 /* END ROOPA - 11/12/03 */
2773 
2774 /*----- Start of Associated Party changes for Related SR -------*/
2775 
2776    ELSIF (l_action_code = 'NOTIFY_ALL_ASSOCIATED_PARTIES') THEN
2777 
2778 
2779       l_linked_subject_list := WF_ENGINE.GetItemAttrText(
2780                         		itemtype        => itemtype,
2781                           		itemkey         => itemkey,
2782                            		aname           => 'NTFY_LINKED_SUBJECT_LIST');
2783 
2784       l_notify_relsr_party_role_list := WF_ENGINE.GetItemAttrText(
2785                           		itemtype        => itemtype,
2786                           		itemkey         => itemkey,
2787                            		aname           => 'NOTIFY_RELSR_PARTY_ROLE_LIST');
2788 
2789       pull_from_list(itemlist => l_notify_relsr_party_role_list,
2790                        element  => l_element);
2791 
2792       l_party_role_code := l_element;
2793 
2794       IF (l_party_role_code IS NOT NULL) THEN
2795          OPEN c_party_role_csr (l_party_role_code);
2796          FETCH c_party_role_csr INTO l_party_role_name;
2797 
2798          IF c_party_role_csr%NOTFOUND THEN
2799             return;
2800          END IF;
2801          CLOSE c_party_role_csr;
2802       ELSE
2803          RETURN;
2804       END IF;
2805 
2806       WF_ENGINE.SetItemAttrText(
2807               itemtype        => itemtype,
2808               itemkey         => itemkey,
2809               aname           => 'PARTY_ROLE_NAME',
2810               avalue          => l_party_role_name );
2811 
2812       WF_ENGINE.SetItemAttrText(
2813               itemtype        => itemtype,
2814               itemkey         => itemkey,
2815               aname           => 'NOTIFY_RELSR_PARTY_ROLE_LIST',
2816               avalue          => l_notify_relsr_party_role_list);
2817 
2818 --    LOOP
2819       IF (l_linked_subject_list IS NOT NULL) THEN
2820          pull_from_list(itemlist => l_linked_subject_list,
2821                         element  => l_element);
2822 
2823 	 l_notify_subject_id := TO_NUMBER(l_element);
2824 
2825 	 WF_ENGINE.SetItemAttrText(
2826               itemtype        => itemtype,
2827               itemkey         => itemkey,
2828               aname           => 'NTFY_LINKED_SUBJECT_LIST',
2829               avalue          => l_linked_subject_list );
2830 
2831 	 WF_ENGINE.SetItemAttrNumber(
2832               itemtype        => itemtype,
2833               itemkey         => itemkey,
2834               aname           => 'NTFY_SUBJECT_ID',
2835               avalue          => l_notify_subject_id );
2836 
2837 	 OPEN sel_link_csr;
2838 	 FETCH sel_link_csr INTO sel_link_rec;
2839 	 CLOSE sel_link_csr;
2840 
2841 	 OPEN sel_linked_sr_type_csr;
2842 	 FETCH sel_linked_sr_type_csr INTO sel_linked_sr_type_rec;
2843 	 CLOSE sel_linked_sr_type_csr;
2844 
2845          l_user_id := WF_ENGINE.GetItemAttrNumber(
2846                                     itemtype        => itemtype,
2847                                     itemkey         => itemkey,
2848                                     aname           => 'USER_ID' );
2849 
2850 	 WF_ENGINE.SetItemAttrText(
2851              itemtype        => itemtype,
2852              itemkey         => itemkey,
2853              aname           => 'NTFY_LINKED_INCIDENT_NUMBER',
2854              avalue          => sel_link_rec.incident_number );
2855 
2856 	 -- Get link Type name from link_type_id
2857 
2858 	 WF_ENGINE.SetItemAttrText(
2859               itemtype        => itemtype,
2860               itemkey         => itemkey,
2861               aname           => 'NTFY_LINK_TYPE',
2862               avalue          => sel_linked_sr_type_rec.link_type_name );
2863 
2864          l_request_id := l_notify_subject_id;
2865 
2866          OPEN sel_linked_sr_details_csr;
2867         FETCH sel_linked_sr_details_csr into sel_linked_sr_details_rec;
2868         CLOSE sel_linked_sr_details_csr;
2869 
2870         WF_ENGINE.SetItemAttrText(
2871                   itemtype        => itemtype,
2872                   itemkey         => itemkey,
2873                   aname           => 'NTFY_LINKED_SR_STATUS',
2874                   avalue          => sel_linked_sr_details_rec.name );
2875 
2876 
2877 
2878         WF_ENGINE.SetItemAttrText(
2879                   itemtype        => itemtype,
2880                   itemkey         => itemkey,
2881                   aname           => 'NTFY_LINKED_SR_SUMMARY',
2882                   avalue          => sel_linked_sr_details_rec.summary );
2883 
2884          OPEN sel_party_role_contacts_csr;
2885 	 LOOP
2886 
2887 	    FETCH sel_party_role_contacts_csr
2888 	    INTO l_contact_email, l_contact_party_id, l_person_id, l_contact_type, l_tmp_contact_point_id;
2889 	    EXIT WHEN sel_party_role_contacts_csr%NOTFOUND;
2890 
2891 	    -- Check for WF email list length not to exceed 2000
2892 	    IF (nvl(LENGTH(l_contact_email_list), 0) + nvl(LENGTH(l_contact_email),0) + 1) <= 2000 THEN
2893 
2894                IF (l_contact_email IS NOT NULL)THEN
2895                   OPEN sel_contact_name_csr;
2896                   FETCH sel_contact_name_csr INTO sel_contact_name_rec;
2897                   CLOSE sel_contact_name_csr;
2898 
2899                   l_notify_contact_name := sel_contact_name_rec.first_name || ' ' || sel_contact_name_rec.last_name;
2900                   WF_ENGINE.SetItemAttrText(
2901                           itemtype        => itemtype,
2902                           itemkey         => itemkey,
2903                           aname           => 'NTFY_LINKED_SR_CONTACT',
2904                           avalue          => l_notify_contact_name );
2905 
2906                   OPEN sel_jsp_name_csr;
2907 	          FETCH sel_jsp_name_csr INTO l_jsp_name;
2908 	          CLOSE sel_jsp_name_csr;
2909 
2910                   l_serviceRequest_URL := FND_PROFILE.Value('JTF_BIS_OA_HTML') || '/' || l_jsp_name
2911                                            || '?srID=' || l_request_number;
2912 
2913                   WF_ENGINE.SetItemAttrText(
2914                           itemtype        => itemtype,
2915                           itemkey         => itemkey,
2916                           aname           => 'NTFY_REQUEST_NUMBER_URL',
2917                           avalue          => l_serviceRequest_URL);
2918 
2919                   IF (l_contact_email_list IS NULL) THEN
2920                      l_contact_email_list := l_contact_email ;
2921                   ELSE
2922                      l_contact_email_list := l_contact_email_list ||','||l_contact_email;
2923                   END IF;
2924 
2925                   IF (l_contact_type = 'EMPLOYEE') THEN
2926                     l_orig_system := 'PER';
2927                     l_orig_system_id := l_person_id;
2928                   ELSE
2929                     l_orig_system := 'HZ_PARTY';
2930                     l_orig_system_id := l_contact_party_id;
2931                   END IF;
2932 
2933 	          OPEN c_user(l_orig_system,l_orig_system_id);
2934 	          FETCH c_user INTO l_user, l_user_display_name, l_email_address, l_notification_preference,l_language;
2935 	          CLOSE c_user;
2936 
2937                   -- If contact does not have workflow user, create an adhoc user.
2938                   -- Adhoc user name is re-used for performance.
2939 	          -- The same party_id could have different contact info, but workflow schema
2940 	          -- only has one user/role which is the party in hz_party.
2941 
2942                   IF (l_user IS NULL OR
2943 		      (l_user is not null and instr(l_adhoc_user_list,l_user) > 0) OR
2944 		      (l_user is not null and l_email_address <> l_contact_email) ) THEN
2945 
2946 		     adhoc_count := adhoc_count + 1;
2947 
2948                      l_user := 'CS_WF_CONTACT_USER_DUMMY' || to_char(adhoc_count) ;
2949 
2950                      -- need to pass display name also, other wise the 'To' field in mail and ntfxn
2951                      -- will show the adhoc name.
2952                      l_user_display_name := sel_contact_name_rec.last_name || ', ' || sel_contact_name_rec.first_name;
2953 
2954                      SetWorkflowAdhocUser(p_wf_username      => l_user,
2955                                           p_email_address    => l_contact_email,
2956                                           p_display_name     => l_user_display_name);
2957 
2958                   END IF;
2959 
2960                   IF (l_adhoc_user_list IS NULL) THEN
2961                      l_adhoc_user_list := l_user ;
2962                   ELSE
2963                      l_adhoc_user_list := l_adhoc_user_list ||','||l_user;
2964                   END IF;
2965 
2966 	          -- 11.5.10 enhancement for create interaction when email to contact sent.
2967     	          IF (l_contact_party_id_list IS NULL) THEN
2968                      l_contact_party_id_list := TO_CHAR(l_contact_party_id);
2969     	          ELSE
2970                      l_contact_party_id_list := l_contact_party_id_list || ' '
2971                                                 || TO_CHAR(l_contact_party_id);
2972                   END IF;
2973                   -- end 11.5.10 enhancement.
2974                END IF;
2975 	    ELSE
2976 	       exit;
2977 	    END IF;
2978 	 END LOOP;
2979          CLOSE sel_party_role_contacts_csr;
2980 
2981          l_request_id := WF_ENGINE.GetItemAttrNumber(
2982                                 itemtype        => itemtype,
2983                                 itemkey         => itemkey,
2984                                 aname           => 'REQUEST_ID' );
2985 
2986       -- Fix for bug 3392429. Only create ADHOC role and notification if there
2987       -- is/are email addresses, since we send notifications for contacts via
2988       -- email only. rmanabat 02/12/04 .
2989          IF (l_contact_email_list IS NOT NULL) THEN
2990 
2991 	    -- If there's just one user in the list, then make the recipient that user.
2992             IF (instr(l_adhoc_user_list, ',') = 0) THEN
2993                l_notify_recipient := l_adhoc_user_list;
2994             ELSE
2995             -- We send mass notifications to one ADHOC role which has the list of
2996             -- contact's workflow users.
2997                l_adhoc_role := 'CS_WF_CONTACT_ROLE_DUMMY';
2998                SetWorkflowAdhocRole(p_wf_rolename      => l_adhoc_role,
2999                                     p_user_list        => l_adhoc_user_list);
3000                l_notify_recipient := l_adhoc_role;
3001             END IF;
3002 
3003          ELSE
3004             l_notify_recipient := NULL;
3005          END IF;
3006 
3007       ELSE
3008          null;
3009       END IF;	/*** IF (l_linked_subject_list IS NOT NULL) ***/
3010 /* END ROOPA - 11/12/03 */
3011 
3012 
3013 /*------------- End of Associated Party Related SR changes ----------------*/
3014 
3015      ELSIF (l_action_code = 'NOTIFY_ALL_CONTACTS') THEN
3016 
3017         /****************************************
3018          For contacts, we shall be creating a workflow Adhoc role
3019 	 when nececessary since not all contacts are employees or
3020 	 have workflow roles, e.g., customer contact.
3021          We will be sending just one notification message for all the contacts
3022          who has e-mail addresses. The recipient role would include the list of
3023 	 the contact's workflow user names. This is less expensive than
3024 	sending one e-mail for each contact.
3025         *****************************************/
3026 
3027         -- Check if the html_notification flag is set
3028 
3029 	  OPEN c_check_html_notification;
3030 	 FETCH c_check_html_notification INTO l_html_notification_flag ;
3031 	 CLOSE c_check_html_notification;
3032 
3033         OPEN sel_all_contacts_csr;
3034 
3035 	  LOOP
3036 
3037 	    FETCH sel_all_contacts_csr
3038 	    INTO l_contact_email, l_contact_party_id, l_person_id, l_contact_type, l_tmp_contact_point_id;
3039 	    EXIT WHEN sel_all_contacts_csr%NOTFOUND;
3040 
3041 	    -- Check for WF email list length not to exceed 2000
3042 	    IF (nvl(LENGTH(l_contact_email_list), 0) + nvl(LENGTH(l_contact_email),0) + 1) <= 2000 THEN
3043 
3044 	      IF (l_contact_email_list IS NULL) THEN
3045 	       l_contact_email_list := l_contact_email ;
3046 	      ELSE
3047 	       l_contact_email_list := l_contact_email_list ||','||l_contact_email;
3048 	      END IF;
3049 
3050        	      IF (l_html_contact_email_list IS NULL) THEN
3051 	          l_html_contact_email_list := l_contact_email ;
3052 	      ELSE
3053 	          l_html_contact_email_list := l_html_contact_email_list ||' '||l_contact_email;
3054 	      END IF;
3055 
3056 	      -- This section gets the workflow user of the contact, if none exist create an adhoc.
3057 
3058               IF (l_contact_type = 'EMPLOYEE') THEN
3059                 l_orig_system := 'PER';
3060                 l_orig_system_id := l_person_id;
3061               ELSE
3062                 l_orig_system := 'HZ_PARTY';
3063                 l_orig_system_id := l_contact_party_id;
3064               END IF;
3065 
3066 	      /****
3067               WF_DIRECTORY.GetUserName( p_orig_system           => l_orig_system,
3068                                         p_orig_system_id        => l_orig_system_id,
3069                                         p_name                  => l_user,
3070                                         p_display_name          => l_user_display_name);
3071 	      ***/
3072 
3073 	      OPEN c_user(l_orig_system,l_orig_system_id);
3074 	      FETCH c_user
3075                INTO l_user, l_user_display_name, l_email_address, l_notification_preference,l_language;
3076 	      CLOSE c_user;
3077 
3078               -- If contact does not have workflow user, create an adhoc user.
3079               -- Adhoc user name is re-used for performance.
3080 	      -- The same party_id could have different contact info, but workflow schema
3081 	      -- only has one user/role which is the party in hz_party.
3082 
3083               IF (l_user IS NULL OR
3084 		  (l_user is not null and instr(l_adhoc_user_list,l_user) > 0) OR
3085 		  (l_user is not null and l_email_address <> l_contact_email) ) THEN
3086 
3087 	 	  adhoc_count := adhoc_count + 1;
3088 
3089                   l_user := 'CS_WF_CONTACT_USER_DUMMY' || to_char(adhoc_count) ;
3090 
3091 		  -- need to pass display name also, other wise the 'To' field in mail and ntfxn
3092 		  -- will show the adhoc name.
3093 
3094                    OPEN sel_contact_name_csr;
3095                   FETCH sel_contact_name_csr INTO sel_contact_name_rec;
3096                         l_user_display_name := sel_contact_name_rec.last_name || ', ' || sel_contact_name_rec.first_name;
3097                   CLOSE sel_contact_name_csr;
3098 
3099                   SetWorkflowAdhocUser(p_wf_username      => l_user,
3100                                      p_email_address    => l_contact_email,
3101 				     p_display_name	=> l_user_display_name);
3102 
3103               END IF;
3104 
3105               IF (l_adhoc_user_list IS NULL) THEN
3106                  l_adhoc_user_list := l_user ;
3107               ELSE
3108                  l_adhoc_user_list := l_adhoc_user_list ||','||l_user;
3109               END IF;
3110 
3111               -- Added code for HTML Notification  (01/30/2006) release 12.0
3112 
3113               IF l_html_notification_flag = 'Y' THEN
3114 
3115                  IF l_html_adhoc_user_list IS NULL THEN
3116 		    l_html_adhoc_user_list  := l_user;
3117 		 ELSE
3118 		    l_html_adhoc_user_list  := l_html_adhoc_user_list ||' ' ||l_user;
3119 		 END IF ;
3120 
3121                  IF l_contact_type_list IS NULL THEN
3122                     IF l_contact_type = 'EMPLOYEE' THEN
3123                        l_contact_type_list := 'EMP';
3124                     ELSE -- IF l_contact_type = 'HZ_PARTY' THEN
3125                        l_contact_type_list := 'PARTY';
3126                     END IF;
3127                  ELSE
3128                     IF l_contact_type = 'EMPLOYEE' THEN
3129                        l_contact_type_list := l_contact_type_list||' '||'EMP';
3130                     ELSE -- IF l_contact_type = 'HZ_PARTY' THEN
3131                        l_contact_type_list := l_contact_type_list||' '||'PARTY';
3132                     END IF;
3133                  END IF ;
3134 
3135                  IF l_contact_id_list IS NULL THEN
3136                     IF l_contact_type = 'EMPLOYEE' THEN
3137                        l_contact_id_list := l_person_id;
3138                     ELSE -- IF l_contact_type = 'HZ_PARTY' THEN
3139                        l_contact_id_list := l_contact_party_id;
3140                     END IF;
3141                  ELSE
3142                     IF l_contact_type = 'EMPLOYEE' THEN
3143                        l_contact_id_list := l_contact_id_list||' '||l_person_id;
3144                     ELSE -- IF l_contact_type = 'HZ_PARTY' THEN
3145                        l_contact_id_list := l_contact_id_list||' '||l_contact_party_id;
3146                     END IF;
3147                  END IF ;
3148 
3149                  IF l_notification_pref_list IS NULL THEN
3150                     l_notification_pref_list := NVL(l_notification_preference,'MAILTEXT');
3151                  ELSE
3152                     l_notification_pref_list := l_notification_pref_list||' '||NVL(l_notification_preference,'MAILTEXT');
3153                  END IF ;
3154 
3155                  IF l_language_list IS NULL THEN
3156                     l_language_list := NVL(l_language,'AMERICAN');
3157                  ELSE
3158                     l_language_list := l_language_list||' '||NVL(l_language,'AMERICAN');
3159                  END IF ;
3160 
3161               END IF ;   -- end if for l_html_notification_flag
3162 
3163 	      -- 11.5.10 enhancement for create interaction when email to contact sent.
3164 
3165 	      IF (l_contact_party_id_list IS NULL) THEN
3166   		 l_contact_party_id_list := TO_CHAR(l_contact_party_id);
3167 	      ELSE
3168 		 l_contact_party_id_list := l_contact_party_id_list || ' '
3169 		  || TO_CHAR(l_contact_party_id);
3170 	      END IF;
3171 	      -- end 11.5.10 enhancement.
3172 
3173 	    ELSE
3174 	      EXIT;
3175 	    END IF;
3176 
3177 	  END LOOP;
3178 	CLOSE sel_all_contacts_csr;
3179 
3180         -- set the item attributes for html notification
3181 
3182         IF l_html_notification_flag = 'Y' THEN
3183 
3184            WF_ENGINE.SetItemAttrText
3185                       ( itemtype  => itemtype,
3186                         itemkey   => itemkey,
3187                         AName  => 'ADHOC_USER_LIST',
3188                         AValue => l_html_adhoc_user_list);
3189 
3190            WF_ENGINE.SetItemAttrText
3191                       ( itemtype  => itemtype,
3192                         itemkey   => itemkey,
3193                         AName  => 'CONTACT_ID_LIST',
3194                         AValue => l_contact_id_list);
3195 
3196           WF_ENGINE.SetItemAttrText
3197                       ( itemtype  => itemtype,
3198                         itemkey   => itemkey,
3199                         AName  => 'CONTACT_EMAIL_LIST',
3200                         AValue => l_html_contact_email_list);
3201 
3202           WF_ENGINE.SetItemAttrText
3203                       ( itemtype  => itemtype,
3204                         itemkey   => itemkey,
3205                         AName  => 'CONTACT_TYPE_LIST',
3206                         AValue => l_contact_type_list);
3207 
3208           WF_ENGINE.SetItemAttrText
3209                       ( itemtype  => itemtype,
3210                         itemkey   => itemkey,
3211                         AName  => 'NOTIFICATION_PREFERENCE_LIST',
3212                         AValue => l_notification_pref_list);
3213 
3214           WF_ENGINE.SetItemAttrText
3215                       ( itemtype  => itemtype,
3216                         itemkey   => itemkey,
3217                         AName  => 'LANGUAGE_LIST',
3218                         AValue => l_language_list);
3219 
3220         END IF;
3221 
3222         OPEN sel_jsp_name_csr;
3223 	FETCH sel_jsp_name_csr INTO l_jsp_name;
3224 	CLOSE sel_jsp_name_csr;
3225 
3226 	l_serviceRequest_URL := FND_PROFILE.Value('JTF_BIS_OA_HTML') || '/' || l_jsp_name
3227 				|| '?srID=' || l_request_number;
3228         WF_ENGINE.SetItemAttrText(
3229                       itemtype        => itemtype,
3230                       itemkey         => itemkey,
3231                       aname           => 'NTFY_REQUEST_NUMBER_URL',
3232                       avalue          => l_serviceRequest_URL);
3233 
3234         -- Fix for bug 3392429. Only create ADHOC role and notification if there
3235         -- is/are email addresses, since we send notifications for contacts via
3236         -- email . rmanabat 02/12/04 .
3237 
3238         IF (l_contact_email_list IS NOT NULL) THEN
3239 
3240 	  -- If there's just one user in the list, then make the recipient that user.
3241 
3242 	  IF (instr(l_adhoc_user_list, ',') = 0) THEN
3243 	    l_notify_recipient := l_adhoc_user_list;
3244 	  ELSE
3245 	    -- We send mass notifications to one ADHOC role which has the list of
3246 	    -- contact's workflow users.
3247 
3248 	    l_adhoc_role := 'CS_WF_CONTACT_ROLE_DUMMY';
3249 
3250             SetWorkflowAdhocRole(p_wf_rolename      => l_adhoc_role,
3251                                  p_user_list        => l_adhoc_user_list);
3252 
3253             l_notify_recipient := l_adhoc_role;
3254 
3255 	  END IF;
3256 
3257         ELSE
3258           l_notify_recipient := NULL;
3259         END IF;
3260 
3261 
3262 	-- 11.5.10 enhancement to Create Interaction when email to contact sent.
3263         /* Roopa - begin bug 3360069 */
3264 	/* Replacing the AddItemAttr() call with WF_Engine add, get and set calls
3265            to factor in the scenario where this attr already exists */
3266 
3267 	IF (l_contact_party_id_list IS NOT NULL) THEN
3268 
3269           OPEN check_if_item_attr_exists_csr;
3270 	  FETCH check_if_item_attr_exists_csr into l_temp_contact_id_list;
3271 
3272 	  IF(check_if_item_attr_exists_csr%NOTFOUND) THEN
3273 	    WF_ENGINE.AddItemAttr( itemtype,
3274 				   itemkey,
3275 				   'CONTACT_PARTY_ID_LIST',
3276 				   l_contact_party_id_list);
3277 	  ELSE
3278             WF_ENGINE.SetItemAttrText(
3279                       itemtype        => itemtype,
3280                       itemkey         => itemkey,
3281                       aname           => 'CONTACT_PARTY_ID_LIST',
3282                       avalue          => l_contact_party_id_list);
3283           END IF;
3284 	    CLOSE check_if_item_attr_exists_csr;
3285 
3286 	END IF;
3287 /* Roopa - End bug 3360069 */
3288 
3289 -- Start of code for Associated Party Notification --
3290      ELSIF (l_action_code = 'NOTIFY_ASSOCIATED_PARTIES') THEN
3291 
3292 
3293         l_notify_party_role_list := WF_ENGINE.GetItemAttrText(
3294                            		itemtype        => itemtype,
3295                           		itemkey         => itemkey,
3296                            		aname           => 'NOTIFY_PARTY_ROLE_LIST');
3297 
3298         pull_from_list(itemlist => l_notify_party_role_list,
3299                        element  => l_element);
3300 
3301         l_party_role_code := l_element;
3302 
3303         IF (l_party_role_code IS NOT NULL) THEN
3304            OPEN c_party_role_csr (l_party_role_code);
3305            FETCH c_party_role_csr INTO l_party_role_name;
3306 
3307            IF c_party_role_csr%NOTFOUND THEN
3308               return;
3309            END IF;
3310            CLOSE c_party_role_csr;
3311         ELSE
3312            RETURN;
3313         END IF;
3314 
3315         WF_ENGINE.SetItemAttrText(
3316                 itemtype        => itemtype,
3317                 itemkey         => itemkey,
3318                 aname           => 'PARTY_ROLE_NAME',
3319                 avalue          => l_party_role_name );
3320 
3321         WF_ENGINE.SetItemAttrText(
3322                 itemtype        => itemtype,
3323                 itemkey         => itemkey,
3324                 aname           => 'NOTIFY_PARTY_ROLE_LIST',
3325                 avalue          => l_notify_party_role_list);
3326 
3327 
3328         OPEN sel_party_role_contacts_csr;
3329         LOOP
3330 
3331            FETCH sel_party_role_contacts_csr
3332            INTO l_contact_email, l_contact_party_id, l_person_id, l_contact_type, l_tmp_contact_point_id;
3333            EXIT WHEN sel_party_role_contacts_csr%NOTFOUND;
3334            -- Check for WF email list length not to exceed 2000
3335            IF (nvl(LENGTH(l_contact_email_list), 0) + nvl(LENGTH(l_contact_email),0) + 1) <= 2000 THEN
3336 
3337               IF (l_contact_email_list IS NULL) THEN
3338                  l_contact_email_list := l_contact_email ;
3339               ELSE
3340                  l_contact_email_list := l_contact_email_list ||','||l_contact_email;
3341               END IF;
3342 
3343               IF (l_contact_type = 'EMPLOYEE') THEN
3344                 l_orig_system := 'PER';
3345                 l_orig_system_id := l_person_id;
3346               ELSE
3347                 l_orig_system := 'HZ_PARTY';
3348                 l_orig_system_id := l_contact_party_id;
3349               END IF;
3350 
3351               OPEN c_user(l_orig_system,l_orig_system_id);
3352               FETCH c_user INTO l_user, l_user_display_name, l_email_address, l_notification_preference,l_language;
3353               CLOSE c_user;
3354 
3355                -- If contact does not have workflow user, create an adhoc user.
3356                -- Adhoc user name is re-used for performance.
3357                -- The same party_id could have different contact info, but workflow schema
3358                -- only has one user/role which is the party in hz_party.
3359 
3360               IF (l_user IS NULL OR
3361                    (l_user is not null and instr(l_adhoc_user_list,l_user) > 0) OR
3362                    (l_user is not null and l_email_address <> l_contact_email) ) THEN
3363 
3364                  adhoc_count := adhoc_count + 1;
3365 
3366                  l_user := 'CS_WF_CONTACT_USER_DUMMY' || to_char(adhoc_count) ;
3367 
3368                  -- need to pass display name also, other wise the 'To' field in mail and ntfxn
3369                  -- will show the adhoc name.
3370                  OPEN sel_contact_name_csr;
3371                  FETCH sel_contact_name_csr INTO sel_contact_name_rec;
3372                  l_user_display_name := sel_contact_name_rec.last_name || ', ' || sel_contact_name_rec.first_name;
3373                  CLOSE sel_contact_name_csr;
3374 
3375                  SetWorkflowAdhocUser(p_wf_username      => l_user,
3376                                       p_email_address    => l_contact_email,
3377                                       p_display_name     => l_user_display_name);
3378 
3379               END IF;
3380 
3381               IF (l_adhoc_user_list IS NULL) THEN
3382                  l_adhoc_user_list := l_user ;
3383               ELSE
3384                  l_adhoc_user_list := l_adhoc_user_list ||','||l_user;
3385               END IF;
3386 
3387 	       -- 11.5.10 enhancement for create interaction when email to contact sent.
3388               IF (l_contact_party_id_list IS NULL) THEN
3389                  l_contact_party_id_list := TO_CHAR(l_contact_party_id);
3390               ELSE
3391                  l_contact_party_id_list := l_contact_party_id_list || ' '
3392                                             || TO_CHAR(l_contact_party_id);
3393               END IF;
3394               -- end 11.5.10 enhancement.
3395 
3396            ELSE
3397               EXIT;
3398 
3399            END IF;
3400 
3401         END LOOP;
3402         CLOSE sel_party_role_contacts_csr;
3403 
3404         OPEN sel_jsp_name_csr;
3405         FETCH sel_jsp_name_csr INTO l_jsp_name;
3406         CLOSE sel_jsp_name_csr;
3407 
3408         l_serviceRequest_URL := FND_PROFILE.Value('JTF_BIS_OA_HTML') || '/' || l_jsp_name
3409                                 || '?srID=' || l_request_number;
3410         WF_ENGINE.SetItemAttrText(
3411                       itemtype        => itemtype,
3412                       itemkey         => itemkey,
3413                       aname           => 'NTFY_REQUEST_NUMBER_URL',
3414                       avalue          => l_serviceRequest_URL);
3415 
3416         -- Fix for bug 3392429. Only create ADHOC role and notification if there
3417         -- is/are email addresses, since we send notifications for contacts via
3418         -- email . rmanabat 02/12/04 .
3419         IF (l_contact_email_list IS NOT NULL) THEN
3420 
3421            -- If there's just one user in the list, then make the recipient that user.
3422            IF (instr(l_adhoc_user_list, ',') = 0) THEN
3423               l_notify_recipient := l_adhoc_user_list;
3424            ELSE
3425               -- We send mass notifications to one ADHOC role which has the list of
3426               -- contact's workflow users.
3427               l_adhoc_role := 'CS_WF_CONTACT_ROLE_DUMMY';
3428               SetWorkflowAdhocRole(p_wf_rolename      => l_adhoc_role,
3429                                   p_user_list        => l_adhoc_user_list);
3430               l_notify_recipient := l_adhoc_role;
3431            END IF;
3432 
3433         ELSE
3434            l_notify_recipient := NULL;
3435         END IF;
3436 
3437      ELSIF (l_action_code = 'NOTIFY_PRIMARY_CONTACT') THEN
3438 
3439         -- Get the primary contact's information
3440 
3441           OPEN get_primary_contact_info;
3442          FETCH get_primary_contact_info
3443           INTO l_contact_party_id,l_contact_type,l_tmp_contact_point_id,l_sr_contact_point_type,l_sr_contact_point_id;
3444 
3445         -- get the email address from the CS schema if l_contact_type is not EMAIL.
3446 
3447            -- Get primary contact point ID from cs schema
3448 
3449            IF l_sr_contact_point_type <> 'EMAIL' THEN
3450 
3451                OPEN get_primary_email_info_CS (l_contact_party_id) ;
3452               FETCH get_primary_email_info_CS INTO l_pri_sr_contact_point_id, l_pri_email_cont_pt_id ;
3453               CLOSE get_primary_email_info_CS;
3454 
3455               -- get the email address if contact point id found in CS schema.
3456 
3457               IF l_pri_email_cont_pt_id IS NOT NULL THEN
3458 
3459                  IF (l_contact_type = 'PERSON' OR l_contact_type = 'PARTY_RELATIONSHIP') THEN
3460 
3461                      OPEN get_primary_email_info_HZ (l_pri_sr_contact_point_id,l_contact_party_id);
3462                     FETCH get_primary_email_info_HZ INTO l_contact_email;
3463                     CLOSE get_primary_email_info_HZ;
3464                  ELSIF l_contact_type = 'EMPLOYEE' THEN
3465                      OPEN get_primary_email_info_HR (l_contact_party_id);
3466                     FETCH get_primary_email_info_HR INTO l_contact_email;
3467                     CLOSE get_primary_email_info_HR;
3468                  END IF;
3469               END IF;
3470 
3471            END IF ;
3472 
3473         -- Check if the html_notification flag is set
3474 
3475 	  OPEN c_check_html_notification;
3476 	 FETCH c_check_html_notification INTO l_html_notification_flag ;
3477          CLOSE c_check_html_notification;
3478 
3479         IF (get_primary_contact_info%FOUND) THEN
3480 
3481 	  -- This section gets the workflow user of the contact, if none exist create an adhoc.
3482 
3483           IF (l_contact_type = 'EMPLOYEE') THEN
3484             l_orig_system := 'PER';
3485             l_orig_system_id := l_contact_party_id; --l_person_id;
3486           ELSE
3487             l_orig_system := 'HZ_PARTY';
3488             l_orig_system_id := l_contact_party_id;
3489           END IF;
3490 
3491 	  OPEN c_user(l_orig_system,l_orig_system_id);
3492 	  FETCH c_user INTO l_user, l_user_display_name, l_email_address, l_notification_preference,l_language;
3493 	  CLOSE c_user;
3494 
3495           -- If contact does not have workflow user, create an adhoc user.
3496           -- Adhoc user name is re-used for performance.
3497 	  -- The same party_id could have different contact info, but workflow schema
3498 	  -- only has one user/role which is the party in hz_party.
3499 
3500 
3501           IF (l_user IS NULL OR
3502 	      (l_user is not null and instr(l_adhoc_user_list,l_user) > 0) OR
3503 	      (l_user is not null and l_email_address <> l_contact_email) ) THEN
3504 
3505  	     adhoc_count := adhoc_count + 1;
3506 
3507              l_user := 'CS_WF_CONTACT_USER_DUMMY' || to_char(adhoc_count) ;
3508 
3509              -- Check if the email address is available.If not then get any customer email address.
3510 
3511              IF ((l_email_address IS NULL) AND (l_contact_email IS NULL) AND (l_contact_type <> 'EMPLOYEE')) THEN
3512 
3513                 FOR get_any_cust_email_address_rec IN get_any_cust_email_address(l_contact_party_id)
3514                     LOOP
3515                        l_contact_email := get_any_cust_email_address_rec.email_address;
3516                        EXIT WHEN get_any_cust_email_address%FOUND;
3517                     END LOOP;
3518              END IF ;
3519 
3520 	    -- need to pass display name also, other wise the 'To' field in mail and ntfxn
3521 	    -- will show the adhoc name.
3522 
3523                -- Get the contact name details
3524 
3525                IF l_contact_type = 'PERSON' THEN
3526 
3527                    OPEN get_prim_cont_party_info_per(l_contact_party_id) ;
3528                   FETCH get_prim_cont_party_info_per INTO l_contact_first_name , l_contact_last_name;
3529                   CLOSE get_prim_cont_party_info_per;
3530 
3531                ELSIF l_contact_type = 'PARTY_RELATIONSHIP' THEN
3532 
3533                    OPEN get_prim_cont_party_info_reln(l_contact_party_id) ;
3534                   FETCH get_prim_cont_party_info_reln INTO l_contact_first_name , l_contact_last_name;
3535                   CLOSE get_prim_cont_party_info_reln;
3536 
3537                ELSIF l_contact_type = 'EMPLOYEE' THEN
3538 
3539                    OPEN get_prim_cont_party_info_empl(l_contact_party_id) ;
3540                   FETCH get_prim_cont_party_info_empl INTO l_contact_first_name , l_contact_last_name;
3541                   CLOSE get_prim_cont_party_info_empl;
3542 
3543                END IF;
3544 
3545 
3546                l_user_display_name := l_contact_last_name || ', ' || l_contact_first_name;
3547 
3548                SetWorkflowAdhocUser(p_wf_username      => l_user,
3549                                  p_email_address    => NVL(l_contact_email,l_email_address),
3550 				 p_display_name	    => l_user_display_name);
3551 
3552           END IF;
3553 
3554           -- Added code for HTML Notification  (01/30/2006) release 12.0
3555 
3556 
3557           IF l_html_notification_flag = 'Y' THEN
3558 
3559              IF (l_html_contact_email_list IS NULL) THEN
3560 	        l_html_contact_email_list := NVL(l_contact_email,l_email_address) ;
3561 	     ELSE
3562 	        l_html_contact_email_list := l_html_contact_email_list ||' '||NVL(l_contact_email,l_email_address);
3563 	     END IF;
3564 
3565              IF l_html_adhoc_user_list IS NULL THEN
3566                 l_html_adhoc_user_list := l_user;
3567              ELSE
3568                 l_html_adhoc_user_list := l_html_adhoc_user_list||' '||l_user;
3569              END IF;
3570 
3571              IF l_contact_type_list IS NULL THEN
3572                 IF l_contact_type = 'EMPLOYEE' THEN
3573                    l_contact_type_list := 'EMP';
3574                 ELSE -- IF l_contact_type = 'HZ_PARTY' THEN
3575                    l_contact_type_list := 'PARTY';
3576                 END IF;
3577              ELSE
3578                 IF l_contact_type = 'EMPLOYEE' THEN
3579                    l_contact_type_list := l_contact_type_list||' '||'EMP';
3580                 ELSE -- IF l_contact_type = 'HZ_PARTY' THEN
3581                    l_contact_type_list := l_contact_type_list||' '||'PARTY';
3582                 END IF;
3583              END IF ;
3584 
3585              IF l_contact_id_list IS NULL THEN
3586                 IF l_contact_type = 'EMPLOYEE' THEN
3587                    l_contact_id_list := l_contact_party_id; --l_person_id;
3588                 ELSE -- IF l_contact_type = 'HZ_PARTY' THEN
3589                    l_contact_id_list := l_contact_party_id;
3590                 END IF;
3591              ELSE
3592                 IF l_contact_type = 'EMPLOYEE' THEN
3593                    l_contact_id_list := l_contact_id_list||' '||l_contact_party_id; --l_person_id;
3594                 ELSE -- IF l_contact_type = 'HZ_PARTY' THEN
3595                    l_contact_id_list := l_contact_id_list||' '||l_contact_party_id;
3596                 END IF;
3597              END IF ;
3598 
3599              IF l_notification_pref_list IS NULL THEN
3600                 l_notification_pref_list := NVL(l_notification_preference,'MAILTEXT');
3601              ELSE
3602                 l_notification_pref_list := l_notification_pref_list||' '||NVL(l_notification_preference,'MAILTEXT');
3603              END IF ;
3604 
3605              IF l_language_list IS NULL THEN
3606                 l_language_list := NVL(l_language,'AMERICAN');
3607              ELSE
3608                 l_language_list := l_language_list||' '||NVL(l_language,'AMERICAN');
3609              END IF ;
3610 
3611           END IF ;   -- end if for l_html_notification_flag
3612 
3613               IF l_html_notification_flag = 'Y' THEN
3614 
3615                   WF_ENGINE.SetItemAttrText(
3616                                  itemtype  => itemtype,
3617                                  itemkey   => itemkey,
3618                                  AName  => 'ADHOC_USER_LIST',
3619                                  AValue => l_html_adhoc_user_list);
3620 
3621                   WF_ENGINE.SetItemAttrText(
3622                                  itemtype  => itemtype,
3623                                  itemkey   => itemkey,
3624                                  AName  => 'CONTACT_ID_LIST',
3625                                  AValue => l_contact_id_list);
3626 
3627                   WF_ENGINE.SetItemAttrText(
3628                                  itemtype  => itemtype,
3629                                  itemkey   => itemkey,
3630                                  AName  => 'CONTACT_EMAIL_LIST',
3631                                  AValue => l_html_contact_email_list);
3632 
3633                   WF_ENGINE.SetItemAttrText(
3634                                  itemtype  => itemtype,
3635                                  itemkey   => itemkey,
3636                                  AName  => 'CONTACT_TYPE_LIST',
3637                                  AValue => l_contact_type_list);
3638 
3639                   WF_ENGINE.SetItemAttrText(
3640                                  itemtype  => itemtype,
3641                                  itemkey   => itemkey,
3642                                  AName  => 'NOTIFICATION_PREFERENCE_LIST',
3643                                  AValue => l_notification_pref_list);
3644 
3645                   WF_ENGINE.SetItemAttrText(
3646                                  itemtype  => itemtype,
3647                                  itemkey   => itemkey,
3648                                  AName  => 'LANGUAGE_LIST',
3649                                  AValue => l_language_list);
3650 
3651               END IF;
3652 
3653           --l_notify_recipient := l_adhoc_role;
3654             l_notify_recipient := l_user;
3655 
3656 	  -- 11.5.10 enhancement to Create Interaction when email to contact sent.
3657 	  /* Roopa - begin bug 3360069 */
3658 	  /* Replacing the AddItemAttr() call with WF_Engine add, get and set calls
3659 	     to factor in the scenario where this attr already exists */
3660 
3661 	  IF ((l_contact_party_id IS NOT NULL) AND (l_contact_type <> 'EMPLOYEE')) THEN
3662 
3663             OPEN check_if_item_attr_exists_csr;
3664 	    FETCH check_if_item_attr_exists_csr into l_temp_contact_id_list;
3665 
3666 	    IF(check_if_item_attr_exists_csr%NOTFOUND) THEN
3667 		    WF_ENGINE.AddItemAttr( itemtype,
3668 					   itemkey,
3669 				  	   'CONTACT_PARTY_ID_LIST',
3670 				   	   l_contact_party_id);
3671 	    ELSE
3672               WF_ENGINE.SetItemAttrText(
3673                       itemtype        => itemtype,
3674                       itemkey         => itemkey,
3675                       aname           => 'CONTACT_PARTY_ID_LIST',
3676                       avalue          => l_contact_party_id);
3677             END IF;
3678 	    CLOSE check_if_item_attr_exists_csr;
3679 
3680 	  END IF;
3681 
3682           OPEN sel_jsp_name_csr;
3683 	  FETCH sel_jsp_name_csr INTO l_jsp_name;
3684 	  CLOSE sel_jsp_name_csr;
3685 
3686 	  l_serviceRequest_URL := FND_PROFILE.Value('JTF_BIS_OA_HTML') || '/' || l_jsp_name
3687 				|| '?srID=' || l_request_number;
3688           WF_ENGINE.SetItemAttrText(
3689                       itemtype        => itemtype,
3690                       itemkey         => itemkey,
3691                       aname           => 'NTFY_REQUEST_NUMBER_URL',
3692                       avalue          => l_serviceRequest_URL);
3693 
3694 	END IF;		-- IF (get_primary_contact_info%FOUND
3695 
3696         CLOSE get_primary_contact_info;
3697 
3698 -- End of code for Associated Party Notification
3699 
3700       ELSIF (l_action_code = 'NOTIFY_NEW_CONTACT') THEN
3701 
3702         -- Check if the html_notification flag is set
3703 
3704 	  OPEN c_check_html_notification;
3705 	 FETCH c_check_html_notification INTO l_html_notification_flag ;
3706 	 CLOSE c_check_html_notification;
3707 
3708         l_contact_point_id_list := WF_ENGINE.GetItemAttrText(
3709                                   itemtype        => itemtype,
3710                                   itemkey         => itemkey,
3711                                   aname           => 'NEW_CONTACT_POINT_ID_LIST' );
3712 
3713         pull_from_list(itemlist => l_contact_point_id_list,
3714                        element  => l_element);
3715 
3716         WHILE l_element IS NOT NULL LOOP
3717 
3718 	  l_contact_point_id := TO_NUMBER(l_element);
3719 
3720           OPEN sel_new_contact_csr;
3721 	  FETCH sel_new_contact_csr
3722 	  INTO l_contact_email, l_contact_party_id, l_person_id, l_contact_type, l_tmp_contact_point_id;
3723 	  IF (sel_new_contact_csr%FOUND OR l_contact_email IS NOT NULL)THEN
3724 
3725 	    -- Check for WF email list length not to exceed 2000
3726 	    IF (nvl(LENGTH(l_contact_email_list), 0) + nvl(LENGTH(l_contact_email),0) + 1) <= 2000 THEN
3727 
3728 	      IF (l_contact_email_list IS NULL) THEN
3729 	          l_contact_email_list := l_contact_email ;
3730 	      ELSE
3731 	          l_contact_email_list := l_contact_email_list ||','||l_contact_email;
3732 	      END IF;
3733 
3734        	      IF (l_html_contact_email_list IS NULL) THEN
3735   	          l_html_contact_email_list := l_contact_email ;
3736 	      ELSE
3737 	          l_html_contact_email_list := l_html_contact_email_list ||' '||l_contact_email;
3738 	      END IF;
3739 
3740 	      -- This section gets the workflow user of the contact, if none exist create an adhoc.
3741 
3742               IF (l_contact_type = 'EMPLOYEE') THEN
3743                 l_orig_system := 'PER';
3744                 l_orig_system_id := l_person_id;
3745               ELSE
3746                 l_orig_system := 'HZ_PARTY';
3747                 l_orig_system_id := l_contact_party_id;
3748               END IF;
3749 
3750 	      OPEN c_user(l_orig_system,l_orig_system_id);
3751 	      FETCH c_user INTO l_user, l_user_display_name, l_email_address, l_notification_preference,l_language;
3752 	      CLOSE c_user;
3753 
3754               -- If contact does not have workflow user, create an adhoc user.
3755               -- Adhoc user name is re-used for performance.
3756 	      -- The same party_id could have different contact info, but workflow schema
3757 	      -- only has one user/role which is the party in hz_party.
3758 
3759               IF (l_user IS NULL OR
3760 	          (l_user is not null and instr(l_adhoc_user_list,l_user) > 0) OR
3761 	          (l_user is not null and l_email_address <> l_contact_email) ) THEN
3762 
3763 	        adhoc_count := adhoc_count + 1;
3764 
3765                 l_user := 'CS_WF_CONTACT_USER_DUMMY' || to_char(adhoc_count) ;
3766 
3767 	        -- need to pass display name also, other wise the 'To' field in mail and ntfxn
3768 	        -- will show the adhoc name.
3769                 OPEN sel_contact_name_csr;
3770                 FETCH sel_contact_name_csr INTO sel_contact_name_rec;
3771                 l_user_display_name := sel_contact_name_rec.last_name || ', ' || sel_contact_name_rec.first_name;
3772                 CLOSE sel_contact_name_csr;
3773 
3774                 SetWorkflowAdhocUser(p_wf_username      => l_user,
3775                                      p_email_address    => l_contact_email,
3776 				     p_display_name	=> l_user_display_name);
3777 
3778               END IF;
3779 
3780               IF (l_adhoc_user_list IS NULL) THEN
3781                l_adhoc_user_list := l_user ;
3782               ELSE
3783                l_adhoc_user_list := l_adhoc_user_list ||','||l_user;
3784               END IF;
3785 
3786               IF l_html_adhoc_user_list IS NULL THEN
3787 		 l_html_adhoc_user_list  := l_user;
3788 	      ELSE
3789 	         l_html_adhoc_user_list  := l_html_adhoc_user_list ||' ' ||l_user;
3790 	      END IF ;
3791 
3792 	      -- 11.5.10 enhancement for create interaction when email to contact sent.
3793 	      IF (l_contact_party_id_list IS NULL) THEN
3794 		l_contact_party_id_list := TO_CHAR(l_contact_party_id);
3795 	      ELSE
3796 		l_contact_party_id_list := l_contact_party_id_list || ' '
3797 		  || TO_CHAR(l_contact_party_id);
3798 	      END IF;
3799 
3800 	      -- end 11.5.10 enhancement.
3801 
3802 	    ELSE
3803 	      EXIT;
3804 
3805 	    END IF;
3806 
3807             -- Added code for HTML Notification  (01/30/2006) release 12.0
3808 
3809             IF l_html_notification_flag = 'Y' THEN
3810 
3811                IF l_contact_type_list IS NULL THEN
3812                   IF l_contact_type = 'EMPLOYEE' THEN
3813                      l_contact_type_list := 'EMP';
3814                   ELSE --IF l_contact_type = 'HZ_PARTY' THEN
3815                      l_contact_type_list := 'PARTY';
3816                   END IF;
3817                 ELSE
3818                   IF l_contact_type = 'EMPLOYEE' THEN
3819                      l_contact_type_list := l_contact_type_list||' '||'EMP';
3820                   ELSE --IF l_contact_type = 'HZ_PARTY' THEN
3821                      l_contact_type_list := l_contact_type_list||' '||'PARTY';
3822                   END IF;
3823                 END IF ;
3824 
3825                 IF l_contact_id_list IS NULL THEN
3826                    IF l_contact_type = 'EMPLOYEE' THEN
3827                       l_contact_id_list := l_person_id;
3828                    ELSE --IF l_contact_type = 'HZ_PARTY' THEN
3829                       l_contact_id_list := l_contact_party_id;
3830                    END IF;
3831                  ELSE
3832                     IF l_contact_type = 'EMPLOYEE' THEN
3833                        l_contact_id_list := l_contact_id_list||' '||l_person_id;
3834                     ELSE --IF l_contact_type = 'HZ_PARTY' THEN
3835                        l_contact_id_list := l_contact_id_list||' '||l_contact_party_id;
3836                     END IF;
3837                  END IF ;
3838 
3839                  IF l_notification_pref_list IS NULL THEN
3840                     l_notification_pref_list := NVL(l_notification_preference,'MAILTEXT');
3841                  ELSE
3842                     l_notification_pref_list := l_notification_pref_list||' '||NVL(l_notification_preference,'MAILTEXT');
3843                  END IF ;
3844 
3845                  IF l_language_list IS NULL THEN
3846                     l_language_list := NVL(l_language,'AMERICAN');
3847                  ELSE
3848                     l_language_list := l_language_list||' '||NVL(l_language,'AMERICAN');
3849                  END IF ;
3850 
3851               END IF ;   -- end if for l_html_notification_flag
3852 
3853               IF l_html_notification_flag = 'Y' THEN
3854 
3855                   WF_ENGINE.SetItemAttrText(
3856                                  itemtype  => itemtype,
3857                                  itemkey   => itemkey,
3858                                  AName  => 'ADHOC_USER_LIST',
3859                                  AValue => l_html_adhoc_user_list);
3860 
3861                   WF_ENGINE.SetItemAttrText(
3862                                  itemtype  => itemtype,
3863                                  itemkey   => itemkey,
3864                                  AName  => 'CONTACT_ID_LIST',
3865                                  AValue => l_contact_id_list);
3866 
3867                   WF_ENGINE.SetItemAttrText(
3868                                  itemtype  => itemtype,
3869                                  itemkey   => itemkey,
3870                                  AName  => 'CONTACT_EMAIL_LIST',
3871                                  AValue => l_html_contact_email_list);
3872 
3873                   WF_ENGINE.SetItemAttrText(
3874                                  itemtype  => itemtype,
3875                                  itemkey   => itemkey,
3876                                  AName  => 'CONTACT_TYPE_LIST',
3877                                  AValue => l_contact_type_list);
3878 
3879                   WF_ENGINE.SetItemAttrText(
3880                                  itemtype  => itemtype,
3881                                  itemkey   => itemkey,
3882                                  AName  => 'NOTIFICATION_PREFERENCE_LIST',
3883                                  AValue => l_notification_pref_list);
3884 
3885                   WF_ENGINE.SetItemAttrText(
3886                                  itemtype  => itemtype,
3887                                  itemkey   => itemkey,
3888                                  AName  => 'LANGUAGE_LIST',
3889                                  AValue => l_language_list);
3890 
3891               END IF;
3892 
3893           END IF;
3894 	  CLOSE sel_new_contact_csr;
3895 
3896           pull_from_list(itemlist => l_contact_point_id_list,
3897                          element  => l_element);
3898 
3899         END LOOP;
3900 
3901 
3902         -- Fix for bug 3392429. Only create ADHOC role and notification if there
3903         -- is/are email addresses, since we send notifications for contacts via
3904         -- email . rmanabat 02/12/04 .
3905 
3906         IF (l_contact_email_list IS NOT NULL) THEN
3907 
3908 	  -- If there's just one user in the list, then make the recipient that user.
3909 	  IF (instr(l_adhoc_user_list, ',') = 0) THEN
3910 	    l_notify_recipient := l_adhoc_user_list;
3911 	  ELSE
3912 	    -- We send mass notifications to one ADHOC role which has the list of
3913 	    -- contact's workflow users.
3914 	    l_adhoc_role := 'CS_WF_CONTACT_ROLE_DUMMY';
3915             SetWorkflowAdhocRole(p_wf_rolename      => l_adhoc_role,
3916                                  p_user_list        => l_adhoc_user_list);
3917             l_notify_recipient := l_adhoc_role;
3918 	  END IF;
3919 
3920           OPEN sel_jsp_name_csr;
3921 	  FETCH sel_jsp_name_csr INTO l_jsp_name;
3922 	  CLOSE sel_jsp_name_csr;
3923 
3924 	  l_serviceRequest_URL := FND_PROFILE.Value('JTF_BIS_OA_HTML') || '/' || l_jsp_name
3925 				|| '?srID=' || l_request_number;
3926           WF_ENGINE.SetItemAttrText(
3927                       itemtype        => itemtype,
3928                       itemkey         => itemkey,
3929                       aname           => 'NTFY_REQUEST_NUMBER_URL',
3930                       avalue          => l_serviceRequest_URL);
3931 
3932 	ELSE
3933           l_notify_recipient := NULL;
3934 	END IF;
3935 
3936 
3937 	-- 11.5.10 enhancement to Create Interaction when email to contact sent.
3938 	/* Roopa - begin bug 3360069 */
3939 	/* Replacing the AddItemAttr() call with WF_Engine add, get and set calls
3940 	   to factor in the scenario where this attr already exists */
3941 	IF (l_contact_party_id_list IS NOT NULL) THEN
3942 
3943           OPEN check_if_item_attr_exists_csr;
3944 	  FETCH check_if_item_attr_exists_csr into l_temp_contact_id_list;
3945 
3946 	  IF(check_if_item_attr_exists_csr%NOTFOUND) THEN
3947 	    WF_ENGINE.AddItemAttr( itemtype,
3948 				   itemkey,
3949 				   'CONTACT_PARTY_ID_LIST',
3950 				   l_contact_party_id_list);
3951 	  ELSE
3952             WF_ENGINE.SetItemAttrText(
3953                       itemtype        => itemtype,
3954                       itemkey         => itemkey,
3955                       aname           => 'CONTACT_PARTY_ID_LIST',
3956                       avalue          => l_contact_party_id_list);
3957           END IF;
3958 	  CLOSE check_if_item_attr_exists_csr;
3959 
3960 	END IF;
3961 /* Roopa - End bug 3360069 */
3962 
3963 -- Start of code for Associated Party Notification -- Associated Party Added
3964       ELSIF (l_action_code = 'NOTIFY_NEW_ASSOCIATE_PARTY_ADD') THEN
3965 
3966         l_contact_point_id_list := WF_ENGINE.GetItemAttrText(
3967                                   itemtype        => itemtype,
3968                                   itemkey         => itemkey,
3969                                   aname           => 'NEW_ASSOCIATED_PARTY_ID_LIST' );
3970 
3971         pull_from_list(itemlist => l_contact_point_id_list,
3972                        element  => l_element);
3973 
3974         l_contact_email_list:= null;
3975 
3976         WHILE l_element IS NOT NULL LOOP
3977 
3978 	  l_contact_point_id := TO_NUMBER(l_element);
3979 
3980           OPEN sel_new_party_role_contact_csr;
3981 	  FETCH sel_new_party_role_contact_csr
3982 	  INTO l_contact_email, l_contact_party_id, l_person_id, l_contact_type, l_tmp_contact_point_id,
3983                l_party_role_name, l_party_role_code;
3984 
3985           WF_ENGINE.SetItemAttrText(
3986                       itemtype        => itemtype,
3987                       itemkey         => itemkey,
3988                       aname           => 'PARTY_ROLE_CODE',
3989                       avalue          => l_party_role_code);
3990 
3991           WF_ENGINE.SetItemAttrText(
3992                       itemtype        => itemtype,
3993                       itemkey         => itemkey,
3994                       aname           => 'PARTY_ROLE_NAME',
3995                       avalue          => l_party_role_name);
3996 
3997 	  IF (sel_new_party_role_contact_csr%FOUND OR l_contact_email IS NOT NULL)THEN
3998 
3999 	    -- Check for WF email list length not to exceed 2000
4000 	    IF (nvl(LENGTH(l_contact_email_list), 0) + nvl(LENGTH(l_contact_email),0) + 1) <= 2000 THEN
4001 
4002 	      IF (l_contact_email_list IS NULL) THEN
4003 	       l_contact_email_list := l_contact_email ;
4004 	      ELSE
4005 	       l_contact_email_list := l_contact_email_list ||','||l_contact_email;
4006 	      END IF;
4007 
4008 
4009 	      -- This section gets the workflow user of the contact, if none exist create an adhoc.
4010               IF (l_contact_type = 'EMPLOYEE') THEN
4011                 l_orig_system := 'PER';
4012                 l_orig_system_id := l_person_id;
4013               ELSE
4014                 l_orig_system := 'HZ_PARTY';
4015                 l_orig_system_id := l_contact_party_id;
4016               END IF;
4017 
4018 	      OPEN c_user(l_orig_system,l_orig_system_id);
4019 	      FETCH c_user INTO l_user, l_user_display_name, l_email_address, l_notification_preference,l_language;
4020 	      CLOSE c_user;
4021 
4022               -- If contact does not have workflow user, create an adhoc user.
4023               -- Adhoc user name is re-used for performance.
4024 	      -- The same party_id could have different contact info, but workflow schema
4025 	      -- only has one user/role which is the party in hz_party.
4026 
4027               IF (l_user IS NULL OR
4028 	          (l_user is not null and instr(l_adhoc_user_list,l_user) > 0) OR
4029 	          (l_user is not null and l_email_address <> l_contact_email) ) THEN
4030 
4031 	        adhoc_count := adhoc_count + 1;
4032 
4033                 l_user := 'CS_WF_CONTACT_USER_DUMMY' || to_char(adhoc_count) ;
4034 
4035 	        -- need to pass display name also, other wise the 'To' field in mail and ntfxn
4036 	        -- will show the adhoc name.
4037                 OPEN sel_contact_name_csr;
4038                 FETCH sel_contact_name_csr INTO sel_contact_name_rec;
4039                 l_user_display_name := sel_contact_name_rec.last_name || ', ' || sel_contact_name_rec.first_name;
4040                 CLOSE sel_contact_name_csr;
4041 
4042                 SetWorkflowAdhocUser(p_wf_username      => l_user,
4043                                      p_email_address    => l_contact_email,
4044 				     p_display_name	=> l_user_display_name);
4045 
4046               END IF;
4047 
4048               IF (l_adhoc_user_list IS NULL) THEN
4049                l_adhoc_user_list := l_user ;
4050               ELSE
4051                l_adhoc_user_list := l_adhoc_user_list ||','||l_user;
4052               END IF;
4053 
4054 	      -- 11.5.10 enhancement for create interaction when email to contact sent.
4055 	      IF (l_contact_party_id_list IS NULL) THEN
4056 		l_contact_party_id_list := TO_CHAR(l_contact_party_id);
4057 	      ELSE
4058 		l_contact_party_id_list := l_contact_party_id_list || ' '
4059 		  || TO_CHAR(l_contact_party_id);
4060 	      END IF;
4061 	      -- end 11.5.10 enhancement.
4062 
4063 	    ELSE
4064 	      EXIT;
4065 
4066 	    END IF;
4067           END IF;
4068 	  CLOSE sel_new_party_role_contact_csr;
4069 
4070           pull_from_list(itemlist => l_contact_point_id_list,
4071                          element  => l_element);
4072 
4073         END LOOP;
4074 
4075         -- Fix for bug 3392429. Only create ADHOC role and notification if there
4076         -- is/are email addresses, since we send notifications for contacts via
4077         -- email . rmanabat 02/12/04 .
4078         IF (l_contact_email_list IS NOT NULL) THEN
4079 
4080 	  -- If there's just one user in the list, then make the recipient that user.
4081 	  IF (instr(l_adhoc_user_list, ',') = 0) THEN
4082 	    l_notify_recipient := l_adhoc_user_list;
4083 	  ELSE
4084 	    -- We send mass notifications to one ADHOC role which has the list of
4085 	    -- contact's workflow users.
4086 	    l_adhoc_role := 'CS_WF_CONTACT_ROLE_DUMMY';
4087             SetWorkflowAdhocRole(p_wf_rolename      => l_adhoc_role,
4088                                  p_user_list        => l_adhoc_user_list);
4089             l_notify_recipient := l_adhoc_role;
4090 	  END IF;
4091 
4092           OPEN sel_jsp_name_csr;
4093 	  FETCH sel_jsp_name_csr INTO l_jsp_name;
4094 	  CLOSE sel_jsp_name_csr;
4095 
4096 	  l_serviceRequest_URL := FND_PROFILE.Value('JTF_BIS_OA_HTML') || '/' || l_jsp_name
4097 				|| '?srID=' || l_request_number;
4098           WF_ENGINE.SetItemAttrText(
4099                       itemtype        => itemtype,
4100                       itemkey         => itemkey,
4101                       aname           => 'NTFY_REQUEST_NUMBER_URL',
4102                       avalue          => l_serviceRequest_URL);
4103 
4104 	ELSE
4105           l_notify_recipient := NULL;
4106 	END IF;
4107 
4108 -- End of code for Associated Party Notification -- Associated Party Added
4109 
4110       END IF;	-- IF (l_action_code = 'NOTIFY_OWNER' OR .......
4111 
4112       -- Get the message template to be used for the notification
4113       l_notification_template_id := WF_ENGINE.GetItemAttrText(
4114                         		itemtype        => itemtype,
4115                         		itemkey         => itemkey,
4116                         		aname           => 'NTFY_MESSAGE_NAME');
4117 
4118       IF (l_notify_recipient IS NOT NULL AND l_notification_template_id IS NOT NULL)THEN
4119 
4120          IF l_html_notification_flag = 'Y' THEN
4121             result := 'COMPLETE:SET_HTML';
4122          ELSE
4123             result := 'COMPLETE:SET';
4124          END IF;
4125 
4126         -- Set the recipient role for the notification
4127         WF_ENGINE.SetItemAttrText(
4128                           itemtype        => itemtype,
4129                           itemkey         => itemkey,
4130                           aname           => 'NTFY_RECIPIENT',
4131                           avalue          => l_notify_recipient);
4132 
4133       ELSE
4134         result := 'COMPLETE:UNSET';
4135       END IF;
4136 
4137     ELSIF (funmode = 'CANCEL') THEN
4138       result := 'COMPLETE';
4139     END IF;
4140 
4141   EXCEPTION
4142     WHEN OTHERS THEN
4143       WF_CORE.Context('CS_WF_AUTO_NTFY_UPDATE_PKG', 'Set_Notification_Details',
4144 		      itemtype, itemkey, actid, funmode);
4145       RAISE;
4146 
4147   END Set_Notification_Details;
4148 
4149 
4150 
4151   /******************************************************************
4152   -- Create_Contact_Interaction
4153   --
4154   ******************************************************************/
4155 
4156   PROCEDURE Create_Contact_Interaction( itemtype   IN  VARCHAR2,
4157                              itemkey    IN  VARCHAR2,
4158                              actid      IN  NUMBER,
4159                              funmode    IN  VARCHAR2,
4160                              result     OUT NOCOPY VARCHAR2 ) IS
4161 
4162     l_contact_party_id_list	VARCHAR2(2000);
4163     l_contact_party_id		NUMBER;
4164     l_contact_party_name	VARCHAR2(360);
4165     l_element			VARCHAR2(100);
4166     l_request_id		NUMBER;
4167     l_request_number		VARCHAR2(64);
4168     l_user_id			NUMBER;
4169     l_resp_appl_id		NUMBER;
4170     l_login_id			NUMBER;
4171     l_resp_id			NUMBER;
4172 
4173     l_owner_role		VARCHAR2(320);
4174     l_error_role		VARCHAR2(320);
4175 
4176     l_msg_index_OUT		NUMBER;
4177     l_error_text		VARCHAR2(2000);
4178     NL				VARCHAR2(1) := fnd_global.Local_Chr(10);
4179     x_return_status		VARCHAR2(1);
4180     x_resource_id		NUMBER;
4181     x_resource_type		VARCHAR2(50);
4182     x_msg_count			NUMBER;
4183     x_msg_data			VARCHAR2(2000);
4184 
4185 
4186     CURSOR sel_contact_party_name_csr IS
4187       SELECT PARTY_NAME
4188       FROM HZ_PARTIES
4189       WHERE
4190           PARTY_ID = l_contact_party_id;
4191 
4192   BEGIN
4193 
4194     IF (funmode = 'RUN') THEN
4195 
4196       l_contact_party_id_list := WF_ENGINE.GetItemAttrText(
4197                                           itemtype        => itemtype,
4198                                           itemkey         => itemkey,
4199                                           aname           => 'CONTACT_PARTY_ID_LIST',
4200 	  				  ignore_notfound => TRUE);
4201 
4202       IF (l_contact_party_id_list IS NULL) THEN
4203 
4204         result := 'COMPLETE:NA';
4205 
4206       ELSE
4207 
4208         pull_from_list(itemlist => l_contact_party_id_list,
4209                        element  => l_element);
4210 
4211         WF_ENGINE.SetItemAttrText(
4212                 itemtype        => itemtype,
4213                 itemkey         => itemkey,
4214                 aname           => 'CONTACT_PARTY_ID_LIST',
4215                 avalue          => l_contact_party_id_list );
4216 
4217         l_contact_party_id := TO_NUMBER(l_element);
4218 
4219         l_request_id := WF_ENGINE.GetItemAttrNumber(
4220                                 itemtype        => itemtype,
4221                                 itemkey         => itemkey,
4222                                 aname           => 'REQUEST_ID' );
4223         l_request_number := WF_ENGINE.GetItemAttrText(
4224                                         itemtype        => itemtype,
4225                                         itemkey         => itemkey,
4226                                         aname           => 'REQUEST_NUMBER' );
4227         l_user_id := WF_ENGINE.GetItemAttrNumber(
4228                                 itemtype        => itemtype,
4229                                 itemkey         => itemkey,
4230                                 aname           => 'USER_ID' );
4231         l_resp_appl_id := WF_ENGINE.GetItemAttrNumber(
4232                                 itemtype        => itemtype,
4233                                 itemkey         => itemkey,
4234                                 aname           => 'RESP_APPL_ID' );
4235         l_resp_id := WF_ENGINE.GetItemAttrNumber(
4236                                 itemtype        => itemtype,
4237                                 itemkey         => itemkey,
4238                                 aname           => 'RESP_ID' );
4239 
4240 	l_owner_role := WF_ENGINE.GetItemAttrText(
4241                                         itemtype        => itemtype,
4242                                         itemkey         => itemkey,
4243                                         aname           => 'OWNER_ROLE' );
4244 
4245 
4246 	IF (FND_GLOBAL.LOGIN_ID NOT IN (-1,0)) THEN
4247           l_login_id := FND_GLOBAL.LOGIN_ID;
4248         ELSE
4249           l_login_id := NULL;
4250         END IF;
4251 
4252 
4253         --CS_WF_CONTACT_ACT_PKG.Create_Interaction_Activity(
4254         Create_Interaction_Activity(
4255   		  	p_api_revision		=> 1.0,
4256 			p_incident_id		=> l_request_id,
4257 			p_incident_number	=> l_request_number,
4258                         p_party_id		=> l_contact_party_id,
4259                         p_user_id		=> l_user_id,
4260                         p_resp_appl_id		=> l_resp_appl_id,
4261 			p_resp_id		=> l_resp_id,
4262                         p_login_id		=> l_login_id,
4263                         x_return_status		=> x_return_status,
4264                         x_resource_id		=> x_resource_id,
4265                         x_resource_type		=> x_resource_type,
4266                         x_msg_count		=> x_msg_count,
4267                         x_msg_data		=> x_msg_data);
4268 
4269         IF (x_return_status <> FND_API.G_RET_STS_SUCCESS) THEN
4270 
4271           result := 'COMPLETE:NOTCREATED';
4272 
4273 	  IF ( FND_MSG_PUB.Count_Msg > 0) THEN
4274               FOR i IN 1..FND_MSG_PUB.Count_Msg    LOOP
4275                 FND_MSG_PUB.Get(p_msg_index     => i,
4276                                 p_encoded       => 'F',
4277                                 p_data          => x_msg_data,
4278                                 p_msg_index_OUT => l_msg_index_OUT );
4279                 l_error_text := l_error_text || x_msg_data ||NL;
4280               END LOOP;
4281           END IF;
4282 
4283           WF_ENGINE.SetItemAttrText(
4284                 itemtype        => itemtype,
4285                 itemkey         => itemkey,
4286                 aname           => 'INTERACTION_ERR_DATA',
4287                 avalue          => l_error_text );
4288 
4289           OPEN sel_contact_party_name_csr;
4290 	  FETCH sel_contact_party_name_csr INTO l_contact_party_name;
4291 	  CLOSE sel_contact_party_name_csr;
4292 
4293           WF_ENGINE.SetItemAttrText(
4294                 itemtype        => itemtype,
4295                 itemkey         => itemkey,
4296                 aname           => 'INTERACTION_PARTY_NAME',
4297                 avalue          => l_contact_party_name );
4298 
4299 	  -- Part of fix for bug 3360069. rmanabat 01/19/04.
4300 	  IF (l_owner_role is NOT NULL) THEN
4301 	    l_error_role := l_owner_role;
4302 	  ELSE
4303 	    l_error_role := 'SYSADMIN';
4304 	  END IF;
4305 	  WF_ENGINE.SetItemAttrText(
4306                 itemtype        => itemtype,
4307                 itemkey         => itemkey,
4308                 aname           => 'INTERACTION_ERROR_ROLE',
4309                 avalue          => l_error_role );
4310 
4311         ELSE
4312           result := 'COMPLETE:CREATED';
4313         END IF;
4314 
4315 
4316       END IF;   -- IF (l_contact_party_id_list IS NOT NULL)
4317 
4318 
4319     ELSIF (funmode = 'CANCEL') THEN
4320       result := 'COMPLETE';
4321     END IF;
4322 
4323 
4324   EXCEPTION
4325     WHEN OTHERS THEN
4326       WF_CORE.Context('CS_WF_AUTO_NTFY_UPDATE_PKG', 'Create_Contact_Interaction',
4327 		      itemtype, itemkey, actid, funmode);
4328       RAISE;
4329 
4330   END Create_Contact_Interaction;
4331 
4332 
4333   /******************************************************************
4334   -- All_Interactions_Created
4335   --
4336   ******************************************************************/
4337 
4338   PROCEDURE All_Interactions_Created( itemtype   IN  VARCHAR2,
4339                              itemkey    IN  VARCHAR2,
4340                              actid      IN  NUMBER,
4341                              funmode    IN  VARCHAR2,
4342                              result     OUT NOCOPY VARCHAR2 ) IS
4343 
4344     l_contact_party_id_list	VARCHAR2(2000);
4345 
4346 
4347   BEGIN
4348 
4349     IF (funmode = 'RUN') THEN
4350 
4351       l_contact_party_id_list := WF_ENGINE.GetItemAttrText(
4352                                         itemtype        => itemtype,
4353                                         itemkey         => itemkey,
4354                                         aname           => 'CONTACT_PARTY_ID_LIST',
4355 					ignore_notfound => TRUE);
4356 
4357       IF (l_contact_party_id_list IS NULL) THEN
4358         result := 'COMPLETE:Y';
4359       ELSE
4360         result := 'COMPLETE:N';
4361       END IF;
4362 
4363 
4364     ELSIF (funmode = 'CANCEL') THEN
4365       result := 'COMPLETE';
4366     END IF;
4367 
4368 
4369   EXCEPTION
4370     WHEN OTHERS THEN
4371       WF_CORE.Context('CS_WF_AUTO_NTFY_UPDATE_PKG', 'All_Interactions_Created',
4372                       itemtype, itemkey, actid, funmode);
4373       RAISE;
4374 
4375   END All_Interactions_Created;
4376 
4377 
4378   /******************************************************************
4379   -- All_Recipients_Notified
4380   --
4381   --   This procedure corresponds to the VRFY_NTFY_LIST_DONE function
4382   --   activity.
4383   --
4384   --   This procedure checks if all the recipients of the related
4385   --   service requests have been notified. If all the related SRs
4386   --   did not fit in the initial list, then we will have to build
4387   --   another list here to determine the recipients of the ntfxn.
4388   ******************************************************************/
4389 
4390   PROCEDURE All_Recipients_Notified( itemtype   IN  VARCHAR2,
4391                              itemkey    IN  VARCHAR2,
4392                              actid      IN  NUMBER,
4393                              funmode    IN  VARCHAR2,
4394                              result     OUT NOCOPY VARCHAR2 ) IS
4395 
4396     l_action_code		CS_SR_ACTION_DETAILS.action_code%TYPE;
4397     l_linked_subject_list       VARCHAR2(4000);
4398     l_overflow_flag		VARCHAR2(1);
4399     l_relationship_type_id	CS_SR_ACTION_DETAILS.relationship_type_id%TYPE;
4400     l_event_condition_id        CS_SR_ACTION_TRIGGERS.event_condition_id%TYPE;
4401     l_char_subject_id        	VARCHAR2(30);
4402     l_subject_id		NUMBER;
4403     l_request_number            VARCHAR2(64);
4404     l_request_id		NUMBER;
4405 
4406     l_notify_relsr_party_role_list  VARCHAR2(2000);
4407     l_notify_party_role_list        VARCHAR2(2000);
4408 
4409 
4410     CURSOR sel_event_action_csr IS
4411       SELECT csad.event_condition_id,
4412              csad.action_code,
4413              csad.notification_template_id,	/** this is the WF message name  **/
4414 	     csad.relationship_type_id detail_link_type
4415         FROM CS_SR_ACTION_DETAILS csad
4416        WHERE csad.event_condition_id = l_event_condition_id
4417 	  and csad.action_code = l_action_code
4418           and TRUNC(SYSDATE) BETWEEN TRUNC(NVL(csad.start_date_active, SYSDATE))
4419               and TRUNC(NVL(csad.end_date_active, SYSDATE))
4420           and csad.notification_template_id IS NOT NULL
4421           and csad.action_code like 'NOTIFY%';
4422 
4423     sel_event_action_rec	sel_event_action_csr%ROWTYPE;
4424 
4425 
4426     CURSOR sel_link_csr_1 IS
4427       SELECT
4428         cil.object_id,
4429         cil.link_id,
4430 	inc.incident_owner_id
4431       FROM cs_incident_links cil,
4432 	   cs_incidents_all_b inc
4433       WHERE cil.subject_id = l_request_id
4434 	and inc.incident_id = cil.object_id
4435 	and cil.object_id > l_subject_id
4436       ORDER BY cil.object_id;
4437 
4438     CURSOR sel_link_csr_2 IS
4439       SELECT
4440         cil.object_id,
4441         cil.link_id,
4442 	inc.incident_owner_id
4443       FROM cs_incident_links cil,
4444 	   cs_incidents_all_b inc
4445       WHERE cil.subject_id = l_request_id
4446         AND cil.link_type_id = l_relationship_type_id
4447 	AND inc.incident_id = cil.object_id
4448 	and cil.object_id > l_subject_id
4449       ORDER BY cil.object_id;
4450 
4451     sel_link_rec        sel_link_csr_1%ROWTYPE;
4452 
4453   BEGIN
4454 
4455     IF (funmode = 'RUN') THEN
4456 
4457       l_action_code := WF_ENGINE.GetItemAttrText(
4458                                   itemtype        => itemtype,
4459                                   itemkey         => itemkey,
4460                                   aname           => 'NTFY_ACTION_CODE' );
4461 
4462       IF (l_action_code = 'NOTIFY_OWNER_OF_RELATED_SR') OR
4463          (l_action_code = 'NOTIFY_PRIM_CONTACT_OF_REL_SR') OR
4464          (l_action_code = 'NOTIFY_ALL_CONTACTS_OF_REL_SR') THEN
4465 
4466 	l_linked_subject_list := WF_ENGINE.GetItemAttrText(
4467                           		itemtype        => itemtype,
4468                           		itemkey         => itemkey,
4469                           		aname           => 'NTFY_LINKED_SUBJECT_LIST');
4470 
4471         IF (l_linked_subject_list IS NOT NULL) THEN
4472 
4473           result := 'COMPLETE:N';
4474 
4475 	ELSE
4476 
4477 	  l_overflow_flag := WF_ENGINE.GetItemAttrText(
4478           			itemtype        => itemtype,
4479               			itemkey         => itemkey,
4480               			aname           => 'MORE_NTFY_LINKED_SUBJECT_LIST');
4481 
4482           IF (l_overflow_flag = 'Y') THEN
4483 
4484 	    -- Reset the Overflow flag
4485             WF_ENGINE.SetItemAttrText(
4486                       itemtype        => itemtype,
4487                       itemkey         => itemkey,
4488                       aname           => 'MORE_NTFY_LINKED_SUBJECT_LIST',
4489                       avalue          => '' );
4490 
4491             l_event_condition_id := WF_ENGINE.GetItemAttrNumber(
4492                                         itemtype        => itemtype,
4493                                         itemkey         => itemkey,
4494                                         aname           => 'NTFY_EVENT_CONDITION_ID' );
4495 
4496 	    l_subject_id := WF_ENGINE.GetItemAttrNumber(
4497               				itemtype        => itemtype,
4498               				itemkey         => itemkey,
4499               				aname           => 'NTFY_SUBJECT_ID');
4500 
4501             l_request_number := WF_ENGINE.GetItemAttrText(
4502                                         itemtype        => itemtype,
4503                                         itemkey         => itemkey,
4504                                         aname           => 'REQUEST_NUMBER' );
4505 
4506             l_request_id := WF_ENGINE.GetItemAttrNumber(
4507                                         itemtype        => itemtype,
4508                                         itemkey         => itemkey,
4509                                         aname           => 'REQUEST_ID' );
4510 
4511 	    IF (sel_event_action_rec.detail_link_type IS NOT NULL) THEN
4512 	      l_relationship_type_id := sel_event_action_rec.detail_link_type;
4513 	      OPEN sel_link_csr_2;
4514             ELSE
4515 	      OPEN sel_link_csr_1;
4516 	    END IF;
4517 
4518             LOOP
4519 	      IF (sel_event_action_rec.detail_link_type IS NOT NULL) THEN
4520 	        FETCH sel_link_csr_2 INTO sel_link_rec;
4521 	        EXIT WHEN sel_link_csr_2%NOTFOUND;
4522 	      ELSE
4523 	        FETCH sel_link_csr_1 INTO sel_link_rec;
4524 	        EXIT WHEN sel_link_csr_1%NOTFOUND;
4525 	      END IF;
4526 
4527 	      l_char_subject_id := TO_CHAR(sel_link_rec.object_id);
4528 
4529 	      IF ((nvl(LENGTH(l_linked_subject_list),0) +
4530 		   nvl(LENGTH(l_char_subject_id),0) + 1) <= 4000) THEN
4531 	        IF l_linked_subject_list IS NULL THEN
4532 	          l_linked_subject_list := l_char_subject_id;
4533                 ELSE
4534 	          l_linked_subject_list := l_linked_subject_list || ' ' || l_char_subject_id;
4535                 END IF;
4536 	      ELSE	/**** Will need to build another list later since it won't fit here ***/
4537 	         WF_ENGINE.SetItemAttrText(
4538               		itemtype        => itemtype,
4539               		itemkey         => itemkey,
4540               		aname           => 'MORE_NTFY_LINKED_SUBJECT_LIST',
4541               		avalue          => 'Y' );
4542 	         EXIT;
4543 	      END IF;
4544 
4545             END LOOP;
4546 
4547 	    IF sel_link_csr_2%ISOPEN THEN
4548 	      CLOSE sel_link_csr_2;
4549 	    ELSIF sel_link_csr_1%ISOPEN THEN
4550 	      CLOSE sel_link_csr_1;
4551 	    END IF;
4552 
4553 	    IF (l_linked_subject_list IS NOT NULL) THEN
4554 
4555 	      WF_ENGINE.SetItemAttrText(
4556                                 itemtype        => itemtype,
4557                                 itemkey         => itemkey,
4558                                 aname           => 'NTFY_LINKED_SUBJECT_LIST',
4559                                 avalue          => l_linked_subject_list );
4560 
4561 	      result := 'COMPLETE:N';
4562 	    ELSE
4563               result := 'COMPLETE:Y';
4564 	    END IF;
4565           ELSE	/*** IF (l_overflow_flag = 'Y') ***/
4566               result := 'COMPLETE:Y';
4567           END IF;
4568 
4569         END IF;	/*** IF (l_linked_subject_list IS NOT NULL) ***/
4570 
4571       ELSIF (l_action_code = 'NOTIFY_ALL_ASSOCIATED_PARTIES') THEN
4572 
4573         l_notify_relsr_party_role_list := WF_ENGINE.GetItemAttrText(
4574                           		itemtype        => itemtype,
4575                           		itemkey         => itemkey,
4576                            		aname           => 'NOTIFY_RELSR_PARTY_ROLE_LIST');
4577 
4578 	l_linked_subject_list := WF_ENGINE.GetItemAttrText(
4579                           		itemtype        => itemtype,
4580                           		itemkey         => itemkey,
4581                           		aname           => 'NTFY_LINKED_SUBJECT_LIST');
4582 
4583         IF (l_linked_subject_list IS NOT NULL or l_notify_relsr_party_role_list IS NOT NULL) THEN
4584 
4585           result := 'COMPLETE:N';
4586 
4587 	ELSE
4588 
4589 	  l_overflow_flag := WF_ENGINE.GetItemAttrText(
4590           			itemtype        => itemtype,
4591               			itemkey         => itemkey,
4592               			aname           => 'MORE_NTFY_LINKED_SUBJECT_LIST');
4593 
4594           IF (l_overflow_flag = 'Y') THEN
4595 
4596 	    -- Reset the Overflow flag
4597             WF_ENGINE.SetItemAttrText(
4598                       itemtype        => itemtype,
4599                       itemkey         => itemkey,
4600                       aname           => 'MORE_NTFY_LINKED_SUBJECT_LIST',
4601                       avalue          => '' );
4602 
4603             l_event_condition_id := WF_ENGINE.GetItemAttrNumber(
4604                                         itemtype        => itemtype,
4605                                         itemkey         => itemkey,
4606                                         aname           => 'NTFY_EVENT_CONDITION_ID' );
4607 
4608 	    l_subject_id := WF_ENGINE.GetItemAttrNumber(
4609               				itemtype        => itemtype,
4610               				itemkey         => itemkey,
4611               				aname           => 'NTFY_SUBJECT_ID');
4612 
4613             l_request_number := WF_ENGINE.GetItemAttrText(
4614                                         itemtype        => itemtype,
4615                                         itemkey         => itemkey,
4616                                         aname           => 'REQUEST_NUMBER' );
4617 
4618             l_request_id := WF_ENGINE.GetItemAttrNumber(
4619                                         itemtype        => itemtype,
4620                                         itemkey         => itemkey,
4621                                         aname           => 'REQUEST_ID' );
4622 
4623 	    IF (sel_event_action_rec.detail_link_type IS NOT NULL) THEN
4624 	      l_relationship_type_id := sel_event_action_rec.detail_link_type;
4625 	      OPEN sel_link_csr_2;
4626             ELSE
4627 	      OPEN sel_link_csr_1;
4628 	    END IF;
4629 
4630             LOOP
4631 	      IF (sel_event_action_rec.detail_link_type IS NOT NULL) THEN
4632 	        FETCH sel_link_csr_2 INTO sel_link_rec;
4633 	        EXIT WHEN sel_link_csr_2%NOTFOUND;
4634 	      ELSE
4635 	        FETCH sel_link_csr_1 INTO sel_link_rec;
4636 	        EXIT WHEN sel_link_csr_1%NOTFOUND;
4637 	      END IF;
4638 
4639 	      l_char_subject_id := TO_CHAR(sel_link_rec.object_id);
4640 
4641 	      IF ((nvl(LENGTH(l_linked_subject_list),0) +
4642 		   nvl(LENGTH(l_char_subject_id),0) + 1) <= 4000) THEN
4643 	        IF l_linked_subject_list IS NULL THEN
4644 	          l_linked_subject_list := l_char_subject_id;
4645                 ELSE
4646 	          l_linked_subject_list := l_linked_subject_list || ' ' || l_char_subject_id;
4647                 END IF;
4648 	      ELSE	/**** Will need to build another list later since it won't fit here ***/
4649 	         WF_ENGINE.SetItemAttrText(
4650               		itemtype        => itemtype,
4651               		itemkey         => itemkey,
4652               		aname           => 'MORE_NTFY_LINKED_SUBJECT_LIST',
4653               		avalue          => 'Y' );
4654 	         EXIT;
4655 	      END IF;
4656 
4657             END LOOP;
4658 
4659 	    IF sel_link_csr_2%ISOPEN THEN
4660 	      CLOSE sel_link_csr_2;
4661 	    ELSIF sel_link_csr_1%ISOPEN THEN
4662 	      CLOSE sel_link_csr_1;
4663 	    END IF;
4664 
4665 	    IF (l_linked_subject_list IS NOT NULL) THEN
4666 
4667 	      WF_ENGINE.SetItemAttrText(
4668                                 itemtype        => itemtype,
4669                                 itemkey         => itemkey,
4670                                 aname           => 'NTFY_LINKED_SUBJECT_LIST',
4671                                 avalue          => l_linked_subject_list );
4672 
4673 	      result := 'COMPLETE:N';
4674 	    ELSE
4675               result := 'COMPLETE:Y';
4676 	    END IF;
4677           ELSE	/*** IF (l_overflow_flag = 'Y') ***/
4678             result := 'COMPLETE:Y';
4679           END IF;
4680 
4681         END IF;	/*** IF (l_linked_subject_list IS NOT NULL) ***/
4682 
4683       ELSIF (l_action_code = 'NOTIFY_ASSOCIATED_PARTIES') THEN
4684         l_notify_party_role_list := WF_ENGINE.GetItemAttrText(
4685                            		itemtype        => itemtype,
4686                           		itemkey         => itemkey,
4687                            		aname           => 'NOTIFY_PARTY_ROLE_LIST');
4688 
4689         IF (l_notify_party_role_list IS NOT NULL) THEN
4690           result := 'COMPLETE:N';
4691 	ELSE
4692           result := 'COMPLETE:Y';
4693         END IF;
4694 
4695       ELSE	/*** IF (l_action_code = 'NOTIFY_OWNER_OF_RELATED_SR') ***/
4696         result := 'COMPLETE:Y';
4697       END IF;
4698 
4699     ELSIF (funmode = 'CANCEL') THEN
4700       result := 'COMPLETE';
4701     END IF;
4702 
4703   EXCEPTION
4704     WHEN OTHERS THEN
4705       WF_CORE.Context('CS_WF_AUTO_NTFY_UPDATE_PKG', 'All_Recipients_Notified',
4706 		      itemtype, itemkey, actid, funmode);
4707       RAISE;
4708 
4709   END All_Recipients_Notified;
4710 
4711 
4712   /***************************************************************************
4713   -- Verify_Notify_Rules_Done
4714   --
4715   --   This procedure corresponds to the VRFY_NEW_STATUS function
4716   --   activity.
4717   --
4718   --   This procedure checks if all the notification rules has been executed.
4719   --   We will have to build a new rules list if there was an overflow
4720   --   (as indicated by the MORE_NTFY_ACTION_LIST flag) because there are
4721   --   more rules in the query.
4722   **************************************************************************/
4723 
4724   PROCEDURE Verify_Notify_Rules_Done( itemtype   IN  VARCHAR2,
4725                              itemkey    IN  VARCHAR2,
4726                              actid      IN  NUMBER,
4727                              funmode    IN  VARCHAR2,
4728                              result     OUT NOCOPY VARCHAR2 ) IS
4729 
4730     l_event_name                VARCHAR2(240);
4731     l_event_condition_id        CS_SR_ACTION_TRIGGERS.event_condition_id%TYPE;
4732     l_char_event_condition_id   VARCHAR2(30);
4733 
4734     l_action_code               CS_SR_ACTION_DETAILS.action_code%TYPE;
4735     l_notify_conditions_list    VARCHAR2(4000);
4736     l_notify_actions_list       VARCHAR2(4000);
4737     l_overflow_flag             VARCHAR2(1);
4738 
4739 
4740     CURSOR sel_action_csr IS
4741       SELECT csad.event_condition_id,
4742 	     csad.action_code,
4743 	     csad.notification_template_id
4744       FROM CS_SR_ACTION_TRIGGERS csat,
4745            CS_SR_ACTION_DETAILS csad,
4746 	   CS_SR_EVENT_CODES_B cec
4747       WHERE
4748 	  cec.WF_BUSINESS_EVENT_ID = l_event_name
4749           and csat.EVENT_CODE = cec.EVENT_CODE
4750           and TRUNC(SYSDATE) BETWEEN TRUNC(NVL(csat.start_date_active, SYSDATE))
4751               and TRUNC(NVL(csat.end_date_active, SYSDATE))
4752           and csad.event_condition_id = csat.event_condition_id
4753           and TRUNC(SYSDATE) BETWEEN TRUNC(NVL(csad.start_date_active, SYSDATE))
4754               and TRUNC(NVL(csad.end_date_active, SYSDATE))
4755           and csad.notification_template_id IS NOT NULL
4756           and csad.action_code like 'NOTIFY%'
4757 	  AND TO_CHAR(csad.event_condition_id) || csad.action_code >
4758 		TO_CHAR(l_event_condition_id) || l_action_code
4759       ORDER BY TO_CHAR(1) || 2;
4760 
4761       sel_action_rec	sel_action_csr%ROWTYPE;
4762 
4763 
4764   BEGIN
4765 
4766     IF (funmode = 'RUN') THEN
4767 
4768       l_notify_conditions_list := WF_ENGINE.GetItemAttrText(
4769                                   itemtype        => itemtype,
4770                                   itemkey         => itemkey,
4771                                   aname           => 'NTFY_CONDITION_LIST' );
4772 
4773       l_notify_actions_list := WF_ENGINE.GetItemAttrText(
4774                                   itemtype        => itemtype,
4775                                   itemkey         => itemkey,
4776                                   aname           => 'NTFY_ACTION_LIST' );
4777 
4778       IF (l_notify_conditions_list IS NOT NULL AND l_notify_actions_list IS NOT NULL) THEN
4779         result := 'COMPLETE:N';
4780       ELSE
4781 
4782         l_overflow_flag := WF_ENGINE.GetItemAttrText(
4783                                 itemtype        => itemtype,
4784                                 itemkey         => itemkey,
4785                                 aname           => 'MORE_NTFY_ACTION_LIST');
4786 
4787 	IF (l_overflow_flag = 'Y') THEN
4788 
4789           -- Reset the Overflow flag
4790 	  WF_ENGINE.SetItemAttrText(
4791                       itemtype        => itemtype,
4792                       itemkey         => itemkey,
4793                       aname           => 'MORE_NTFY_ACTION_LIST',
4794                       avalue          => '' );
4795 
4796           l_event_condition_id := WF_ENGINE.GetItemAttrNumber(
4797                                           itemtype        => itemtype,
4798                                           itemkey         => itemkey,
4799                                           aname           => 'NTFY_EVENT_CONDITION_ID');
4800           l_action_code := WF_ENGINE.GetItemAttrText(
4801                                           itemtype        => itemtype,
4802                                           itemkey         => itemkey,
4803                                           aname           => 'NTFY_ACTION_CODE');
4804           l_event_name := WF_ENGINE.GetItemAttrText(
4805                                           itemtype        => itemtype,
4806                                           itemkey         => itemkey,
4807                                           aname           => 'EVENTNAME' );
4808 
4809 	  OPEN sel_action_csr;
4810           LOOP
4811           FETCH sel_action_csr INTO sel_action_rec;
4812           EXIT WHEN sel_action_csr%NOTFOUND;
4813           l_char_event_condition_id := TO_CHAR(sel_action_rec.event_condition_id);
4814 
4815           IF (nvl(LENGTH(l_notify_conditions_list),0) +
4816               nvl(LENGTH(l_char_event_condition_id),0) + 1) <= 4000  OR
4817              (nvl(LENGTH(l_notify_actions_list),0) +
4818               nvl(LENGTH(sel_action_rec.action_code),0) + 1 ) <= 4000 THEN
4819 
4820             IF l_notify_conditions_list IS NULL THEN
4821               l_notify_conditions_list := l_char_event_condition_id;
4822               l_notify_actions_list := sel_action_rec.action_code;
4823             ELSE
4824               l_notify_conditions_list := l_notify_conditions_list || ' ' || l_char_event_condition_id;
4825               l_notify_actions_list := l_notify_actions_list || ' ' || sel_action_rec.action_code;
4826             END IF;
4827           ELSE
4828 
4829 	    WF_ENGINE.SetItemAttrText(
4830                         itemtype        => itemtype,
4831                         itemkey         => itemkey,
4832                         aname           => 'MORE_NTFY_ACTION_LIST',
4833                         avalue          => 'Y' );
4834             EXIT;
4835 
4836           END IF;
4837         END LOOP;
4838         CLOSE sel_action_csr;
4839 
4840         IF (l_notify_conditions_list IS NOT NULL AND l_notify_actions_list IS NOT NULL) THEN
4841 
4842           WF_ENGINE.SetItemAttrText(
4843                   itemtype        => itemtype,
4844                   itemkey         => itemkey,
4845                   aname           => 'NTFY_CONDITION_LIST',
4846                   avalue          => l_notify_conditions_list );
4847           WF_ENGINE.SetItemAttrText(
4848                   itemtype        => itemtype,
4849                   itemkey         => itemkey,
4850                   aname           => 'NTFY_ACTION_LIST',
4851                   avalue          => l_notify_actions_list );
4852 
4853           result := 'COMPLETE:N';
4854 	ELSE
4855           result := 'COMPLETE:Y';
4856 	END IF;
4857 
4858 
4859         ELSE	/** IF (l_overflow_flag = 'Y') **/
4860 
4861           result := 'COMPLETE:Y';
4862 
4863         END IF;
4864 
4865 
4866       /** IF (l_update_conditions_list IS NOT NULL AND l_update_actions_list IS NOT NULL) **/
4867       END IF;
4868 
4869 
4870     ELSIF (funmode = 'CANCEL') THEN
4871       result := 'COMPLETE';
4872     END IF;
4873 
4874   EXCEPTION
4875     WHEN OTHERS THEN
4876       WF_CORE.Context('CS_WF_AUTO_NTFY_UPDATE_PKG', 'Verify_Notify_Rules_Done',
4877 		      itemtype, itemkey, actid, funmode);
4878       RAISE;
4879 
4880   END Verify_Notify_Rules_Done;
4881 
4882 
4883   /*****************************************************************************
4884   -- Check_Status_Rules
4885   --
4886   --   This procedure corresponds to the CHECK_STATUS_RULES function
4887   --   activity.
4888   --
4889   --   The procedure checks if there are update rules defined by checking
4890   --   the conditions/actions list. If the list is not empty, then we pull the
4891   --   first item on the lists to process. Otherwise, we end the sub-process.
4892   --
4893   *****************************************************************************/
4894 
4895   PROCEDURE Check_Status_Rules( itemtype   IN  VARCHAR2,
4896                              itemkey    IN  VARCHAR2,
4897                              actid      IN  NUMBER,
4898                              funmode    IN  VARCHAR2,
4899                              result     OUT NOCOPY VARCHAR2 ) IS
4900 
4901     l_event_action	VARCHAR2(50);
4902     l_actions_list	VARCHAR2(4000);
4903     --l_link_id		CS_INCIDENT_LINKS.LINK_ID%TYPE;
4904     --l_subject_id	CS_INCIDENT_LINKS.SUBJECT_ID%TYPE;
4905     --l_link_type_code	CS_INCIDENT_LINKS.LINK_TYPE_CODE%TYPE;
4906 
4907     l_update_conditions_list    VARCHAR2(4000);
4908     l_update_actions_list       VARCHAR2(4000);
4909     l_event_condition_id	CS_SR_ACTION_TRIGGERS.event_condition_id%TYPE;
4910     l_action_code		CS_SR_ACTION_DETAILS.action_code%TYPE;
4911 
4912     l_space_pos		NUMBER;
4913     l_element		VARCHAR2(100);
4914 
4915 
4916   BEGIN
4917 
4918     IF (funmode = 'RUN') THEN
4919 
4920       l_update_conditions_list := WF_ENGINE.GetItemAttrText(
4921                                   itemtype        => itemtype,
4922                                   itemkey         => itemkey,
4923                                   aname           => 'UPDATE_CONDITION_LIST' );
4924 
4925       l_update_actions_list := WF_ENGINE.GetItemAttrText(
4926                                   itemtype        => itemtype,
4927                                   itemkey         => itemkey,
4928                                   aname           => 'UPDATE_ACTION_LIST' );
4929 
4930       IF (l_update_conditions_list IS NOT NULL AND l_update_actions_list IS NOT NULL) THEN
4931 
4932         pull_from_list(itemlist	=> l_update_conditions_list,
4933 		       element	=> l_element);
4934 	l_event_condition_id := TO_NUMBER(l_element);
4935 
4936         pull_from_list(itemlist	=> l_update_actions_list,
4937 		       element	=> l_action_code);
4938 
4939         IF (l_event_condition_id IS NOT NULL AND l_action_code IS NOT NULL) THEN
4940           WF_ENGINE.SetItemAttrText(
4941                 itemtype        => itemtype,
4942                 itemkey         => itemkey,
4943                 aname           => 'UPDATE_CONDITION_LIST',
4944                 avalue          => l_update_conditions_list );
4945           WF_ENGINE.SetItemAttrText(
4946                   itemtype        => itemtype,
4947                   itemkey         => itemkey,
4948                   aname           => 'UPDATE_ACTION_LIST',
4949                   avalue          => l_update_actions_list );
4950           WF_ENGINE.SetItemAttrNumber(
4951                   itemtype        => itemtype,
4952                   itemkey         => itemkey,
4953                   aname           => 'UPDATE_EVENT_CONDITION_ID',
4954                   avalue          => l_event_condition_id );
4955           WF_ENGINE.SetItemAttrText(
4956                   itemtype        => itemtype,
4957                   itemkey         => itemkey,
4958                   aname           => 'UPDATE_ACTION_CODE',
4959                   avalue          => l_action_code );
4960 
4961           result := 'COMPLETE:Y';
4962 
4963         ELSE
4964           result := 'COMPLETE:N';
4965 
4966         END IF;
4967 
4968       ELSE
4969           result := 'COMPLETE:N';
4970       END IF;
4971 
4972     ELSIF (funmode = 'CANCEL') THEN
4973       result := 'COMPLETE';
4974     END IF;
4975 
4976   EXCEPTION
4977     WHEN OTHERS THEN
4978       WF_CORE.Context('CS_WF_AUTO_NTFY_UPDATE_PKG', 'Check_Status_Rules',
4979 		      itemtype, itemkey, actid, funmode);
4980       RAISE;
4981 
4982   END Check_Status_Rules;
4983 
4984 
4985   /****************************************************************************
4986   -- Get_Links_For_Rule
4987   --
4988   --   This procedure corresponds to the GET_STATUS_PROPAGATION_RULES function
4989   --   activity.
4990   --
4991   --   This procedure builds a list of related service requests that satisfy
4992   --   the update rule being processed.
4993   ****************************************************************************/
4994 
4995   PROCEDURE Get_Links_For_Rule( itemtype   IN  VARCHAR2,
4996                              itemkey    IN  VARCHAR2,
4997                              actid      IN  NUMBER,
4998                              funmode    IN  VARCHAR2,
4999                              result     OUT NOCOPY VARCHAR2 ) IS
5000 
5001     l_event_name	VARCHAR2(240);
5002     l_event_action	VARCHAR2(50);
5003     l_actions_list	VARCHAR2(4000);
5004     --l_link_id		VARCHAR2(20);
5005 
5006     --l_subject_id	CS_INCIDENT_LINKS.SUBJECT_ID%TYPE;
5007     l_subject_id	VARCHAR2(30);
5008     --l_link_type_code	CS_INCIDENT_LINKS.LINK_TYPE_CODE%TYPE;
5009     l_event_condition_id	CS_SR_ACTION_TRIGGERS.event_condition_id%TYPE;
5010     l_action_code	CS_SR_ACTION_DETAILS.action_code%TYPE;
5011 
5012     l_new_request_status	VARCHAR2(30);
5013     l_old_request_status        VARCHAR2(30);
5014     l_request_status	        VARCHAR2(30);
5015     l_request_status_temp	VARCHAR2(30);
5016     l_request_number		VARCHAR2(64);
5017     l_relationship_type_id	CS_SR_ACTION_DETAILS.relationship_type_id%TYPE;
5018     l_relationship_type_name	VARCHAR2(240);
5019     l_linked_subject_list	VARCHAR2(4000);
5020 
5021     l_request_id		NUMBER;
5022 
5023 /* 03/01/2004 - RHUNGUND - Bug fix for 3412852
5024    Changed the order of the FROM clause since the optimization rules indicate that
5025    a cartesian join occurs if :
5026    1) If a join condition is missing for  the FROM clause tables
5027    2) If the FROM clause is not listing the tables in the proper order
5028 
5029 */
5030     CURSOR sel_event_action_csr IS
5031       SELECT csat.relationship_type_id trigger_link_type,
5032              csat.from_to_status_code,
5033              csat.incident_status_id trigger_incident_status_id,
5034              csad.relationship_type_id detail_link_type,
5035 	     csad.incident_status_id detail_incident_status_id,
5036 	     csad.resolution_code
5037       FROM CS_SR_EVENT_CODES_B cec,
5038            CS_SR_ACTION_TRIGGERS csat,
5039            CS_SR_ACTION_DETAILS csad
5040       WHERE
5041   	  cec.WF_BUSINESS_EVENT_ID =  l_event_name
5042           and cec.EVENT_CODE = csat.EVENT_CODE
5043           and TRUNC(SYSDATE) BETWEEN TRUNC(NVL(csat.start_date_active, SYSDATE))
5044               and TRUNC(NVL(csat.end_date_active, SYSDATE))
5045           and csat.event_condition_id = csad.event_condition_id
5046           and csad.event_condition_id = l_event_condition_id
5047           and csad.action_code = l_action_code
5048           and csad.action_code NOT like 'NOTIFY%'
5049           and TRUNC(SYSDATE) BETWEEN TRUNC(NVL(csad.start_date_active, SYSDATE))
5050               and TRUNC(NVL(csad.end_date_active, SYSDATE));
5051 
5052 
5053 
5054 
5055     sel_event_action_rec	sel_event_action_csr%ROWTYPE;
5056 
5057     CURSOR sel_link_csr IS
5058       SELECT cil.link_type_id,
5059      	cil.object_id,
5060         cil.link_id
5061       FROM cs_incident_links cil
5062       WHERE cil.subject_id = l_request_id
5063 	AND cil.link_type_id = l_relationship_type_id
5064       ORDER BY cil.object_id;
5065 
5066     sel_link_rec	sel_link_csr%ROWTYPE;
5067 
5068   BEGIN
5069 
5070     IF (funmode = 'RUN') THEN
5071 
5072       l_request_number := WF_ENGINE.GetItemAttrText(
5073                                 itemtype        => itemtype,
5074                                 itemkey         => itemkey,
5075                                 aname           => 'REQUEST_NUMBER' );
5076 
5077       l_request_id := WF_ENGINE.GetItemAttrNumber(
5078                                 itemtype        => itemtype,
5079                                 itemkey         => itemkey,
5080                                 aname           => 'REQUEST_ID' );
5081 
5082       l_event_name := WF_ENGINE.GetItemAttrText(
5083                                   itemtype        => itemtype,
5084                                   itemkey         => itemkey,
5085                                   aname           => 'EVENTNAME' );
5086 
5087       l_event_condition_id := WF_ENGINE.GetItemAttrNumber(
5088                                   itemtype        => itemtype,
5089                                   itemkey         => itemkey,
5090                                   aname           => 'UPDATE_EVENT_CONDITION_ID' );
5091 
5092       l_action_code := WF_ENGINE.GetItemAttrText(
5093                                   itemtype        => itemtype,
5094                                   itemkey         => itemkey,
5095                                   aname           => 'UPDATE_ACTION_CODE' );
5096 
5097       OPEN sel_event_action_csr;
5098       FETCH sel_event_action_csr INTO sel_event_action_rec;
5099       CLOSE sel_event_action_csr;
5100 
5101       IF (sel_event_action_rec.from_to_status_code IS NOT NULL) THEN
5102 --bug 11679421
5103 --Replaced REQUEST_STATUS with REQUEST_STATUS_NEW
5104         IF (sel_event_action_rec.from_to_status_code = 'STATUS_CHANGED_TO') THEN
5105           l_request_status := WF_ENGINE.GetItemAttrText(
5106                                  itemtype        => itemtype,
5107                                  itemkey         => itemkey,
5108                                  aname           => 'REQUEST_STATUS_NEW' );--bug 11679421
5109 
5110         ELSIF (sel_event_action_rec.from_to_status_code = 'STATUS_CHANGED_FROM') THEN
5111           l_request_status := WF_ENGINE.GetItemAttrText(
5112                                  itemtype        => itemtype,
5113                                  itemkey         => itemkey,
5114                                  aname           => 'REQUEST_STATUS_OLD' );
5115         END IF;
5116 
5117 	SELECT name
5118 	INTO l_request_status_temp
5119 	FROM CS_INCIDENT_STATUSES_VL
5120 	WHERE INCIDENT_STATUS_ID = sel_event_action_rec.trigger_incident_status_id;
5121 
5122         IF (l_request_status = l_request_status_temp) THEN
5123           WF_ENGINE.SetItemAttrNumber(
5124   	  	itemtype	=> itemtype,
5125   	    	itemkey		=> itemkey,
5126   	  	aname		=> 'SUBJECT_STATUS_ID',
5127   	  	avalue		=> sel_event_action_rec.detail_incident_status_id );
5128 
5129         ELSE
5130           WF_ENGINE.SetItemAttrText(
5131   	  	itemtype	=> itemtype,
5132   	  	itemkey		=> itemkey,
5133   		aname		=> 'SUBJECT_STATUS_ID',
5134   		avalue		=> NULL);
5135         END IF;
5136 
5137         l_relationship_type_id := sel_event_action_rec.detail_link_type;
5138 
5139 
5140       ELSIF (sel_event_action_rec.trigger_link_type IS NOT NULL) THEN
5141         WF_ENGINE.SetItemAttrNumber(
5142   		itemtype	=> itemtype,
5143   	    	itemkey		=> itemkey,
5144   	  	aname		=> 'SUBJECT_STATUS_ID',
5145   	  	avalue		=> sel_event_action_rec.detail_incident_status_id );
5146 
5147         WF_ENGINE.SetItemAttrText(
5148   	  	itemtype	=> itemtype,
5149   	    	itemkey		=> itemkey,
5150   	  	aname		=> 'SUBJECT_RESOLUTION_CODE',
5151   	  	avalue		=> sel_event_action_rec.resolution_code );
5152 
5153         l_relationship_type_id := sel_event_action_rec.trigger_link_type;
5154 
5155       END IF;
5156 
5157       -- Obtain link type name from relationship_type_id
5158       SELECT name
5159       INTO l_relationship_type_name
5160       FROM CS_SR_LINK_TYPES_VL
5161       WHERE link_type_id = l_relationship_type_id;
5162 
5163       WF_ENGINE.SetItemAttrText(
5164 		itemtype	=> itemtype,
5165 		itemkey		=> itemkey,
5166 		aname		=> 'UPDATE_RELATIONSHIP_TYPE',
5167 		avalue		=> l_relationship_type_name);
5168 
5169       -- Select All Links with the link_type_code and create a subject_id list in workflow.
5170       -- It is guaranteed that every Rule in the list has existing Link SR, this was taken care of
5171       -- when building the initial Rule list in Check_Rules_For_Event().
5172       OPEN sel_link_csr;
5173       LOOP
5174         FETCH sel_link_csr INTO sel_link_rec;
5175         EXIT WHEN sel_link_csr%NOTFOUND;
5176 	l_subject_id := TO_CHAR(sel_link_rec.object_id);
5177 
5178         IF ((nvl(LENGTH(l_linked_subject_list),0) + nvl(LENGTH(l_subject_id),0) + 1) <= 4000) THEN
5179 
5180           IF l_linked_subject_list IS NULL THEN
5181             l_linked_subject_list := l_subject_id;
5182           ELSE
5183             l_linked_subject_list := l_linked_subject_list || ' ' || l_subject_id;
5184           END IF;
5185 	ELSE
5186 
5187 	  WF_ENGINE.SetItemAttrText(
5188                         itemtype        => itemtype,
5189                         itemkey         => itemkey,
5190                         aname           => 'MORE_UPDT_LINKED_SUBJECT_LIST',
5191                         avalue          => 'Y' );
5192 	  EXIT;
5193 
5194 	END IF;
5195 
5196       END LOOP;
5197       CLOSE sel_link_csr;
5198 
5199       IF (l_linked_subject_list IS NOT NULL) THEN
5200         WF_ENGINE.SetItemAttrText(
5201                 itemtype        => itemtype,
5202                 itemkey         => itemkey,
5203                 aname           => 'UPDATE_LINKED_SUBJECT_LIST',
5204                 avalue          => l_linked_subject_list );
5205 
5206         result := 'COMPLETE:Y';
5207 
5208       ELSE
5209         result := 'COMPLETE:N';
5210       END IF;
5211 
5212     ELSIF (funmode = 'CANCEL') THEN
5213       result := 'COMPLETE';
5214     END IF;
5215 
5216   EXCEPTION
5217     WHEN OTHERS THEN
5218       WF_CORE.Context('CS_WF_AUTO_NTFY_UPDATE_PKG', 'Get_Links_For_Rule',
5219 		      itemtype, itemkey, actid, funmode);
5220       RAISE;
5221 
5222   END Get_Links_For_Rule;
5223 
5224 
5225 
5226   /****************************************************************************
5227   -- Execute_Rules_Per_SR
5228   --
5229   --   This procedure corresponds to the GET_STATUS_PROPAGATION_RULES function
5230   --   activity.
5231   --
5232   --   This procedure iterates through the list of related service requests and
5233   --   sets the first item on the list to be processed.
5234   ****************************************************************************/
5235 
5236   PROCEDURE Execute_Rules_Per_SR( itemtype   IN  VARCHAR2,
5237                              itemkey    IN  VARCHAR2,
5238                              actid      IN  NUMBER,
5239                              funmode    IN  VARCHAR2,
5240                              result     OUT NOCOPY VARCHAR2 ) IS
5241 
5242     --l_subject_id	CS_INCIDENT_LINKS.SUBJECT_ID%TYPE;
5243     --l_subject_id	VARCHAR2(30);
5244 
5245     l_linked_subject_list	VARCHAR2(4000);
5246     l_element		VARCHAR2(100);
5247 
5248   BEGIN
5249 
5250     IF (funmode = 'RUN') THEN
5251 
5252       l_linked_subject_list := WF_ENGINE.GetItemAttrText(
5253                                   itemtype        => itemtype,
5254                                   itemkey         => itemkey,
5255                                   aname           => 'UPDATE_LINKED_SUBJECT_LIST' );
5256 
5257       pull_from_list(itemlist	=> l_linked_subject_list,
5258 			element	=> l_element);
5259 
5260       WF_ENGINE.SetItemAttrNumber(
5261               itemtype        => itemtype,
5262               itemkey         => itemkey,
5263               aname           => 'UPDATE_SUBJECT_ID',
5264               avalue          => TO_NUMBER(l_element) );
5265 
5266       WF_ENGINE.SetItemAttrText(
5267               itemtype        => itemtype,
5268               itemkey         => itemkey,
5269               aname           => 'UPDATE_LINKED_SUBJECT_LIST',
5270               avalue          => l_linked_subject_list );
5271 
5272       result := 'COMPLETE';
5273 
5274     ELSIF (funmode = 'CANCEL') THEN
5275       result := 'COMPLETE';
5276     END IF;
5277 
5278   EXCEPTION
5279     WHEN OTHERS THEN
5280       WF_CORE.Context('CS_WF_AUTO_NTFY_UPDATE_PKG', 'Execute_Rules_Per_SR',
5281 		      itemtype, itemkey, actid, funmode);
5282       RAISE;
5283 
5284   END Execute_Rules_Per_SR;
5285 
5286 
5287   /****************************************************************************
5288   -- Verify_Update_Valid
5289   --
5290   --   This procedure corresponds to the VRFY_NEW_STATUS function
5291   --   activity.
5292   --
5293   --   This procedure checks if the update being done is valid. The rules are:
5294   --     1.) If the SR which needs to be updated has a status with
5295   --         'closed flag' ON, DO NOT update the status of the service request.
5296   --     2.) During automatic update, workflow process should not close a
5297   --         service request if it has outgoing links of type
5298   --         duplicate of/caused by to open service requests.
5299   --
5300   --  Modification History:
5301   --
5302   --  Date        Name       Desc
5303   --  ----------  ---------  ---------------------------------------------
5304   --  05/11/04	  RMANABAT   Fix for bug 3582873. Validate if the incoming status
5305   --			     is either 'Clear' or 'Close' as per SRD.
5306   --  06/15/04	  RMANABAT   Fix for bug 3690121. Changed cursor sel_related_sr_cur
5307   --			     to look at inc.status_flag instead of the status
5308   --			     close_flag. Also validated just 'Caused By' link
5309   --			     to allow close of SR when 'Duplicate Of' link is
5310   --			     created.
5311   ****************************************************************************/
5312 
5313   PROCEDURE Verify_Update_Valid( itemtype   IN  VARCHAR2,
5314                              itemkey    IN  VARCHAR2,
5315                              actid      IN  NUMBER,
5316                              funmode    IN  VARCHAR2,
5317                              result     OUT NOCOPY VARCHAR2 ) IS
5318 
5319     l_subject_id	CS_INCIDENT_LINKS.SUBJECT_ID%TYPE;
5320     --l_link_type_code	CS_INCIDENT_LINKS.LINK_TYPE_CODE%TYPE;
5321     l_subject_status_id	CS_SR_ACTION_DETAILS.incident_status_id%TYPE;
5322     l_resolution_code	CS_SR_ACTION_DETAILS.resolution_code%TYPE;
5323     l_close_flag	VARCHAR2(1);
5324     l_link_id		CS_INCIDENT_LINKS.LINK_ID%TYPE;
5325 
5326     l_action_code       CS_SR_ACTION_DETAILS.action_code%TYPE;
5327     l_sr_updated        NUMBER;
5328     l_incident_status_id        NUMBER;
5329     l_UNHANDLED_ACTION          EXCEPTION;
5330 
5331     --l_err_txt		VARCHAR2(30);
5332 
5333     CURSOR sel_related_sr_cur(lv_incident_id IN NUMBER) IS
5334       SELECT cil.link_id
5335       FROM cs_incident_links cil,
5336 	   cs_incidents_all_b inc
5337       WHERE cil.subject_id = lv_incident_id
5338         -- Hard coded values for 'CAUSED BY' (2) and 'DUPLICATE OF' (3).
5339 	AND cil.link_type_id = 2
5340 	AND cil.object_id = inc.incident_id
5341 	AND inc.status_flag = 'O' ;
5342 
5343     CURSOR sel_close_flag_csr(lv_incident_id IN NUMBER) IS
5344       SELECT nvl(status.close_flag,'N'),
5345 	     inc.incident_status_id
5346       FROM   cs_incident_statuses status, cs_incidents_all_b inc
5347       WHERE  inc.incident_id = lv_incident_id
5348         AND inc.incident_status_id = status.incident_status_id;
5349 
5350   BEGIN
5351 
5352     IF (funmode = 'RUN') THEN
5353 
5354       -- SUBJECT_STATUS_ID can be null when updating resolution code . Update Rule #3.
5355       l_subject_status_id := WF_ENGINE.GetItemAttrNumber(
5356                                   itemtype        => itemtype,
5357                                   itemkey         => itemkey,
5358                                   aname           => 'SUBJECT_STATUS_ID' );
5359 
5360       --l_err_txt := 'Valid1';
5361 
5362       l_resolution_code := WF_ENGINE.GetItemAttrText(
5363                                   itemtype        => itemtype,
5364                                   itemkey         => itemkey,
5365                                   aname           => 'SUBJECT_RESOLUTION_CODE' );
5366 
5367       IF (l_subject_status_id IS NULL AND l_resolution_code IS NULL) THEN
5368         result := 'COMPLETE:N';
5369 
5370       ELSE
5371 
5372         l_subject_id := WF_ENGINE.GetItemAttrNumber(
5373                                     itemtype        => itemtype,
5374                                     itemkey         => itemkey,
5375                                     aname           => 'UPDATE_SUBJECT_ID' );
5376 
5377 	l_action_code := WF_ENGINE.GetItemAttrText(
5378                                     itemtype        => itemtype,
5379                                     itemkey         => itemkey,
5380                                     aname           => 'UPDATE_ACTION_CODE' );
5381 
5382         IF (l_action_code = 'CHANGE_SR_STATUS' OR l_action_code = 'CHANGE_SR_RESOLUTION') THEN
5383           l_sr_updated := WF_ENGINE.GetItemAttrNumber(
5384                                       itemtype        => itemtype,
5385                                       itemkey         => itemkey,
5386                                       aname           => 'REQUEST_ID' );
5387         ELSIF (l_action_code = 'CHANGE_RELATED_SR_STATUS') THEN
5388           l_sr_updated := l_subject_id;
5389         ELSE
5390           raise l_UNHANDLED_ACTION;
5391         END IF;
5392 
5393         --l_err_txt := 'Valid2';
5394 
5395         OPEN sel_close_flag_csr(l_sr_updated);
5396         FETCH sel_close_flag_csr
5397 	INTO   l_close_flag, l_incident_status_id;
5398         CLOSE sel_close_flag_csr;
5399 
5400         /**********************************************
5401 	  If the SR which needs to be updated has a status
5402           with 'closed flag' ON, DO NOT update the status
5403           of the service request.
5404         **********************************************/
5405         IF (l_close_flag = 'Y' OR l_incident_status_id = l_subject_status_id) THEN
5406           result := 'COMPLETE:N';
5407         ELSE
5408 
5409           /********************************************
5410 	    During automatic update, workflow process should
5411             not close a service request if it has outgoing
5412             links of type duplicate of/caused by to open
5413             service requests.
5414           ********************************************/
5415 
5416 	  IF (l_subject_status_id IS NULL) THEN  -- Updating resolution code only, not status.
5417 
5418 	    result := 'COMPLETE:Y';
5419 
5420 	  ELSE	-- Updating Status
5421 
5422 
5423 	    IF (l_subject_status_id = 101) THEN
5424               l_close_flag := 'Y';
5425             ELSE
5426               SELECT nvl(status.close_flag,'N')
5427               INTO   l_close_flag
5428               FROM   cs_incident_statuses status
5429               WHERE  status.incident_status_id = l_subject_status_id;
5430             END IF;
5431 
5432             IF (l_close_flag = 'Y') THEN
5433               OPEN sel_related_sr_cur(l_sr_updated);
5434               FETCH sel_related_sr_cur INTO l_link_id;
5435               IF (sel_related_sr_cur%FOUND) THEN
5436                 result := 'COMPLETE:N';
5437               ELSE
5438                 result := 'COMPLETE:Y';
5439               END IF;
5440               CLOSE sel_related_sr_cur;
5441             ELSE
5442               result := 'COMPLETE:Y';
5443             END IF;
5444 
5445           END IF;	-- IF (l_subject_status_id IS NULL)
5446 
5447         END IF;
5448 
5449       END IF;
5450 
5451 
5452     ELSIF (funmode = 'CANCEL') THEN
5453       result := 'COMPLETE';
5454     END IF;
5455 
5456   EXCEPTION
5457     WHEN l_UNHANDLED_ACTION THEN
5458       WF_CORE.Context('CS_WF_AUTO_NTFY_UPDATE_PKG', 'Verify_Update_Valid',
5459                       itemtype, itemkey, actid, funmode);
5460       RAISE;
5461     WHEN OTHERS THEN
5462       WF_CORE.Context('CS_WF_AUTO_NTFY_UPDATE_PKG', 'Verify_Update_Valid',
5463 		      itemtype, itemkey, actid, funmode);
5464       RAISE;
5465 
5466   END Verify_Update_Valid;
5467 
5468 
5469   /*************************************************************************
5470   -- Update_SR
5471   --
5472   --   This procedure corresponds to the VRFY_NEW_STATUS function
5473   --   activity.
5474   --
5475   --   This procedure makes the call to Update_Sericerequest() api to update
5476   --   the status or resolution code of the SR, as defined in the update
5477   --   rule.
5478   --   Depending on the result of the update api call, we set the transition :
5479   --     1.  FND_API.G_RET_STS_SUCCESS - we set the renult to 'UPDATED'.
5480   --         We then move on and process the next related service request.
5481   --     2.  'L'- we set the result to 'LOCKED'. If the record is locked, We
5482   --         then wait for 10 secs and re-try updating the record. We attempt
5483   --         the update 5 times, each time waiting 10 secs. if the record is
5484   --         still locked. If after 5 tries we still can't update, then we set
5485   --         the result to 'FAILED' (item 3).
5486   --     3.  Otherwise (update failed) - we set the result to 'FAILED'. We
5487   --         then try to send a notification to the owner of the SR we are
5488   --         updating with the error message stack.
5489   --   Modification History:
5490   --
5491   --  Date        Name        Desc
5492   --  --------    ----------  --------------------------------------
5493   --  05/25/2004  RMANABAT    Fix for bug 3612904. Passed resp_id and
5494   --                          rep_appL_id to update_servicerequest() api
5495   --                          for security validation.
5496   *************************************************************************/
5497 
5498   PROCEDURE Update_SR( itemtype   IN  VARCHAR2,
5499                              itemkey    IN  VARCHAR2,
5500                              actid      IN  NUMBER,
5501                              funmode    IN  VARCHAR2,
5502                              result     OUT NOCOPY VARCHAR2 ) IS
5503 
5504     l_request_number    	VARCHAR2(64);
5505     l_request_id		NUMBER;
5506     l_subject_id		CS_INCIDENT_LINKS.SUBJECT_ID%TYPE;
5507     l_subject_status_id		CS_SR_ACTION_DETAILS.incident_status_id%TYPE;
5508     l_resolution_code		CS_SR_ACTION_DETAILS.resolution_code%TYPE;
5509     l_service_request_rec	CS_ServiceRequest_PVT.service_request_rec_type;
5510     l_object_version_number	NUMBER;
5511     l_action_code       CS_SR_ACTION_DETAILS.action_code%TYPE;
5512 
5513     --l_request_id		NUMBER;
5514     l_subject_owner_id		NUMBER;
5515     l_close_flag        	VARCHAR2(1);
5516     l_return_status     	VARCHAR2(1);
5517     l_msg_count         	NUMBER;
5518     l_msg_data          	VARCHAR2(2000);
5519     l_user_id           	NUMBER;
5520     l_notes     		CS_SERVICEREQUEST_PVT.notes_table;
5521     l_contacts  		CS_SERVICEREQUEST_PVT.contacts_table;
5522     out_interaction_id		number;
5523     out_wf_process_id		number;
5524 
5525     l_UNHANDLED_ACTION		EXCEPTION;
5526 
5527     l_error_text		VARCHAR2(2000);
5528     NL				VARCHAR2(1);
5529     l_msg_index_OUT		NUMBER;
5530     l_wf_process_id		NUMBER;
5531 
5532     l_resp_appl_id		NUMBER;
5533     l_resp_id			NUMBER;
5534 
5535     CURSOR sel_incident_from_number_csr IS
5536       SELECT object_version_number, incident_owner_id, incident_id
5537       FROM cs_incidents_all_b
5538       WHERE incident_number = l_request_number;
5539 
5540 
5541     CURSOR sel_incident_from_id_csr IS
5542       SELECT object_version_number, incident_owner_id
5543       FROM cs_incidents_all_b
5544       WHERE incident_id = l_subject_id;
5545 
5546 
5547   BEGIN
5548 
5549     NL := fnd_global.Local_Chr(10);
5550 
5551     IF (funmode = 'RUN') THEN
5552 
5553 
5554       CS_ServiceRequest_PVT.initialize_rec(l_service_request_rec);
5555 
5556       l_action_code := WF_ENGINE.GetItemAttrText(
5557                                   itemtype        => itemtype,
5558                                   itemkey         => itemkey,
5559                                   aname           => 'UPDATE_ACTION_CODE' );
5560 
5561       l_request_number := WF_ENGINE.GetItemAttrText(
5562                                 itemtype        => itemtype,
5563                                 itemkey         => itemkey,
5564                                 aname           => 'REQUEST_NUMBER' );
5565 
5566       l_user_id := WF_ENGINE.GetItemAttrNumber(
5567                                 itemtype        => itemtype,
5568                                 itemkey         => itemkey,
5569                                 aname           => 'USER_ID' );
5570 
5571       l_subject_status_id := WF_ENGINE.GetItemAttrNumber(
5572                                   itemtype        => itemtype,
5573                                   itemkey         => itemkey,
5574                                   aname           => 'SUBJECT_STATUS_ID' );
5575 
5576       l_resolution_code := WF_ENGINE.GetItemAttrText(
5577                                   itemtype        => itemtype,
5578                                   itemkey         => itemkey,
5579                                   aname           => 'SUBJECT_RESOLUTION_CODE' );
5580 
5581       l_subject_id := WF_ENGINE.GetItemAttrNumber(
5582                                   itemtype        => itemtype,
5583                                   itemkey         => itemkey,
5584                                   aname           => 'UPDATE_SUBJECT_ID' );
5585 
5586       IF (l_action_code = 'CHANGE_SR_STATUS' OR l_action_code = 'CHANGE_SR_RESOLUTION') THEN
5587 
5588         OPEN sel_incident_from_number_csr;
5589         FETCH sel_incident_from_number_csr
5590         INTO l_object_version_number, l_subject_owner_id, l_request_id;
5591         CLOSE sel_incident_from_number_csr;
5592 
5593       ELSIF (l_action_code = 'CHANGE_RELATED_SR_STATUS') THEN
5594 
5595         OPEN sel_incident_from_id_csr;
5596         FETCH sel_incident_from_id_csr
5597         INTO l_object_version_number, l_subject_owner_id;
5598         CLOSE sel_incident_from_id_csr;
5599 
5600         l_request_id := l_subject_id;
5601 
5602       ELSE
5603 	RAISE l_UNHANDLED_ACTION;
5604       END IF;
5605 
5606       IF (l_subject_status_id IS NOT NULL) THEN
5607         l_service_request_rec.status_id := l_subject_status_id;
5608 
5609         SELECT nvl(status.close_flag,'N')
5610         INTO   l_close_flag
5611         FROM   cs_incident_statuses status
5612         WHERE  status.incident_status_id = l_subject_status_id;
5613 
5614         IF (l_close_flag = 'Y') THEN
5615           l_service_request_rec.closed_date := sysdate;
5616         END IF;
5617 
5618       END IF;
5619 
5620       IF (l_resolution_code IS NOT NULL) THEN
5621         l_service_request_rec.resolution_code := l_resolution_code;
5622       END IF;
5623 
5624       l_service_request_rec.last_update_program_code := 'SUPPORT.WF';
5625 
5626       --l_wf_process_id := TO_NUMBER(substr(itemkey, instr(itemkey,'-')+1));
5627 
5628       l_resp_appl_id := WF_ENGINE.GetItemAttrNumber(
5629                                 itemtype        => itemtype,
5630                                 itemkey         => itemkey,
5631                                 aname           => 'RESP_APPL_ID' );
5632       l_resp_id := WF_ENGINE.GetItemAttrNumber(
5633                                 itemtype        => itemtype,
5634                                 itemkey         => itemkey,
5635                                 aname           => 'RESP_ID' );
5636 
5637       CS_ServiceRequest_PVT.Update_ServiceRequest
5638        ( p_api_version		=> 3.0,
5639          p_init_msg_list	=> fnd_api.g_false,
5640          p_commit		=> fnd_api.g_true,
5641          p_validation_level	=> fnd_api.g_valid_level_full,
5642          x_return_status	=> l_return_status,
5643          x_msg_count		=> l_msg_count,
5644          x_msg_data		=> l_msg_data,
5645          p_request_id		=> l_request_id,
5646          --p_request_id		=> l_subject_id,
5647          p_last_updated_by	=> l_user_id,
5648          p_last_update_date	=> sysdate,
5649          p_service_request_rec	=> l_service_request_rec,
5650          p_notes		=> l_notes,
5651          p_contacts		=> l_contacts,
5652          p_object_version_number=> l_object_version_number,
5653 	 p_resp_appl_id		=> l_resp_appl_id,
5654 	 p_resp_id		=> l_resp_id,
5655 	 --p_workflow_process_id  => l_wf_process_id,
5656          x_interaction_id	=> out_interaction_id,
5657          x_workflow_process_id	=> out_wf_process_id
5658         );
5659 
5660       -- Check for possible errors returned by the API
5661       IF (l_return_status = FND_API.G_RET_STS_SUCCESS) THEN
5662         result := 'COMPLETE:UPDATED';
5663       -- We need to have a separate return status for locked row exception.
5664       ELSIF (l_return_status = 'L') THEN
5665       --ELSIF (l_return_status = FND_API.G_RET_STS_UNEXP_ERROR) THEN
5666         result := 'COMPLETE:LOCKED';
5667       ELSE
5668         -- Obtain the owner role of the related SR being updated, and set-up the
5669 	-- the notification to be sent to the owner of the SR.
5670         result := 'COMPLETE:FAILED';
5671       END IF;
5672 
5673 
5674       -- IF (l_msg_data is not null) THEN
5675       IF (l_return_status <> FND_API.G_RET_STS_SUCCESS) THEN
5676 
5677 
5678         IF ( FND_MSG_PUB.Count_Msg > 0) THEN
5679           FOR i IN 1..FND_MSG_PUB.Count_Msg    LOOP
5680             FND_MSG_PUB.Get(p_msg_index     => i,
5681                             p_encoded       => 'F',
5682                             p_data          => l_msg_data,
5683                             p_msg_index_OUT => l_msg_index_OUT );
5684 	    l_error_text := l_error_text || l_msg_data ||NL;
5685           END LOOP;
5686         END IF;
5687 
5688 
5689         WF_ENGINE.SetItemAttrText(
5690                 itemtype        => itemtype,
5691                 itemkey         => itemkey,
5692                 aname           => 'UPDATE_ERROR_DATA',
5693                 avalue          => l_error_text );
5694       END IF;
5695 
5696 
5697 
5698         /*****************************************
5699         FND_MSG_PUB.Count_And_Get( p_count      => l_msg_count,
5700                                    p_data       => l_msg_data,
5701                                    p_encoded    => FND_API.G_FALSE );
5702         wf_core.context( pkg_name       =>  'CS_ServiceRequest_PUB',
5703                          proc_name      =>  'Update_Status',
5704                          arg1           =>  'p_user_id=>'||l_user_id,
5705                          arg2           =>  'p_org_id=>'||l_org_id,
5706                          arg3           =>  'p_request_number=>'||l_request_number,
5707                          arg4           =>  'p_status_id=>'||l_new_status_id,
5708                          arg5           =>  'p_msg_data=>'||l_msg_data );
5709         l_errmsg_name := 'CS_SR_CANT_UPDATE_STATUS';
5710         raise l_API_ERROR;
5711         ******************************************/
5712 
5713     ELSIF (funmode = 'CANCEL') THEN
5714       result := 'COMPLETE';
5715     END IF;
5716 
5717   EXCEPTION
5718     WHEN l_UNHANDLED_ACTION THEN
5719       WF_CORE.Context('CS_WF_AUTO_NTFY_UPDATE_PKG', 'Update_SR',
5720 		      itemtype, itemkey, actid, funmode);
5721       RAISE;
5722 
5723     WHEN OTHERS THEN
5724       WF_CORE.Context('CS_WF_AUTO_NTFY_UPDATE_PKG', 'Update_SR',
5725 		      itemtype, itemkey, actid, funmode);
5726       RAISE;
5727 
5728   END Update_SR;
5729 
5730 
5731 /********************************************************************
5732   -- Set_Notify_Error
5733   --   This procedure corresponds to the SET_NTFY_ERROR_DETAILS function
5734   --   activity.
5735   --   This procedure sets the notification details for sending the
5736   --   error message when the update activity failed.
5737 ********************************************************************/
5738 
5739   PROCEDURE Set_Notify_Error( itemtype   IN  VARCHAR2,
5740                              itemkey    IN  VARCHAR2,
5741                              actid      IN  NUMBER,
5742                              funmode    IN  VARCHAR2,
5743                              result     OUT NOCOPY VARCHAR2 ) IS
5744 
5745     l_request_number    	VARCHAR2(64);
5746     l_request_id		NUMBER;
5747     l_subject_id		CS_INCIDENT_LINKS.SUBJECT_ID%TYPE;
5748     l_subject_status_id		CS_SR_ACTION_DETAILS.incident_status_id%TYPE;
5749     l_action_code       CS_SR_ACTION_DETAILS.action_code%TYPE;
5750     l_subject_owner_id		NUMBER;
5751 
5752     l_return_status     VARCHAR2(1);
5753     l_msg_count         NUMBER;
5754     l_msg_data          VARCHAR2(2000);
5755     l_owner_role        VARCHAR2(100);
5756     l_owner_name        VARCHAR2(240);
5757 
5758     CURSOR l_cs_sr_get_empid_csr IS
5759       SELECT inc.incident_number, emp.source_id
5760       FROM jtf_rs_resource_extns emp ,
5761            cs_incidents_all_b inc
5762       WHERE emp.resource_id = inc.incident_owner_id
5763         AND inc.incident_id = l_subject_id;
5764 
5765   BEGIN
5766 
5767     IF (funmode = 'RUN') THEN
5768 
5769       l_action_code := WF_ENGINE.GetItemAttrText(
5770                                   itemtype        => itemtype,
5771                                   itemkey         => itemkey,
5772                                   aname           => 'UPDATE_ACTION_CODE' );
5773 
5774       l_request_number := WF_ENGINE.GetItemAttrText(
5775                                 itemtype        => itemtype,
5776                                 itemkey         => itemkey,
5777                                 aname           => 'REQUEST_NUMBER' );
5778 
5779       l_subject_id := WF_ENGINE.GetItemAttrNumber(
5780                                   itemtype        => itemtype,
5781                                   itemkey         => itemkey,
5782                                   aname           => 'UPDATE_SUBJECT_ID' );
5783 
5784       IF (l_action_code = 'CHANGE_SR_STATUS' OR l_action_code = 'CHANGE_SR_RESOLUTION') THEN
5785 
5786         l_owner_role := WF_ENGINE.GetItemAttrText(
5787                                   itemtype        => itemtype,
5788                                   itemkey         => itemkey,
5789                                   aname           => 'OWNER_ROLE' );
5790 
5791       ELSIF (l_action_code = 'CHANGE_RELATED_SR_STATUS') THEN
5792 
5793 	/*************
5794         SELECT incident_number, incident_owner_id
5795         INTO l_request_number, l_subject_owner_id
5796         FROM cs_incidents_all_b
5797         WHERE incident_id = l_subject_id;
5798 
5799         IF (l_subject_owner_id IS NULL)THEN
5800 	  l_owner_role := NULL;
5801 	ELSE
5802 	*************/
5803 
5804         OPEN l_cs_sr_get_empid_csr;
5805 	FETCH l_cs_sr_get_empid_csr
5806 	INTO l_request_number, l_subject_owner_id;
5807 
5808 	IF( l_cs_sr_get_empid_csr%NOTFOUND OR l_subject_owner_id IS NULL) THEN
5809 	  l_owner_role := NULL;
5810         ELSE
5811 
5812 	  -- Retrieve the role name for the request owner
5813           CS_WORKFLOW_PUB.Get_Employee_Role (
5814                     p_api_version           =>  1.0,
5815                     p_return_status         =>  l_return_status,
5816                     p_msg_count             =>  l_msg_count,
5817                     p_msg_data              =>  l_msg_data,
5818                     --p_employee_id           =>  l_subject_id,
5819                     p_employee_id           =>  l_subject_owner_id,
5820                     p_role_name             =>  l_owner_role,
5821                     p_role_display_name     =>  l_owner_name );
5822 	END IF;
5823 	CLOSE l_cs_sr_get_empid_csr;
5824 
5825       END IF;
5826 
5827       If (l_owner_role IS NOT NULL) THEN
5828 
5829         WF_ENGINE.SetItemAttrText(
5830                 itemtype        => itemtype,
5831                 itemkey         => itemkey,
5832                 aname           => 'UPDATE_REQUEST_ROLE',
5833                 avalue          => l_owner_role );
5834 
5835         WF_ENGINE.SetItemAttrText(
5836                 itemtype        => itemtype,
5837                 itemkey         => itemkey,
5838                 aname           => 'UPDATE_REQUEST_NUMBER',
5839                 avalue          => l_request_number );
5840 
5841         result := 'COMPLETE:SET';
5842 
5843       ELSE
5844         result := 'COMPLETE:UNSET';
5845       END IF;
5846 
5847 
5848     ELSIF (funmode = 'CANCEL') THEN
5849       result := 'COMPLETE';
5850     END IF;
5851 
5852   EXCEPTION
5853     WHEN OTHERS THEN
5854       WF_CORE.Context('CS_WF_AUTO_NTFY_UPDATE_PKG', 'Set_Notify_Error',
5855 		      itemtype, itemkey, actid, funmode);
5856       RAISE;
5857 
5858   END Set_Notify_Error;
5859 
5860 
5861 /***********************************************************************
5862   -- Verify_All_Links_Done
5863   --   This procedure corresponds to the VRFY_NEW_STATUS function
5864   --   activity.
5865   --   The procedure verifies if the Linked Service Request list has
5866   --   been exhausted. If not then the procedure returns an 'N' which
5867   --   tells the workflow to process the next rule on the list.
5868   --   If the list has been exhausted, we check if the list overload flag
5869   --   is set to 'Y' (which means the initial list did not fit all the
5870   --   queried rows), if it is not then we return a 'Y' telling the
5871   --   workflow that all Linked Service Requests has been processed.
5872   --   If the overflow flag is 'Y', then we build another list
5873   --   starting from the next linked incident_id (object_id) item of
5874   --   the last item on the previous list.
5875 **********************************************************************/
5876 
5877   PROCEDURE Verify_All_Links_Done( itemtype   IN  VARCHAR2,
5878                              itemkey    IN  VARCHAR2,
5879                              actid      IN  NUMBER,
5880                              funmode    IN  VARCHAR2,
5881                              result     OUT NOCOPY VARCHAR2 ) IS
5882 
5883     l_linked_subject_list       VARCHAR2(4000);
5884     --l_link_type_code    	CS_INCIDENT_LINKS.LINK_TYPE_CODE%TYPE;
5885     --l_event_condition_id        CS_SR_ACTION_TRIGGERS.event_condition_id%TYPE;
5886     --l_action_code       	CS_SR_ACTION_DETAILS.action_code%TYPE;
5887     l_relationship_type_id      CS_SR_ACTION_DETAILS.relationship_type_id%TYPE;
5888     l_relationship_type_name    VARCHAR2(240);
5889     l_subject_id		NUMBER;
5890     l_char_subject_id		VARCHAR2(30);
5891     l_request_number            VARCHAR2(64);
5892     l_request_id		NUMBER;
5893 
5894     l_overflow_flag             VARCHAR2(1);
5895 
5896     CURSOR sel_link_csr IS
5897       SELECT cil.link_type_id,
5898         cil.object_id,
5899         cil.link_id
5900       FROM cs_incident_links cil,
5901            CS_SR_LINK_TYPES_VL cilt
5902       WHERE cil.subject_id = l_request_id
5903         AND cil.link_type_id = cilt.link_type_id
5904         AND cilt.name = l_relationship_type_name
5905 	AND cil.object_id > l_subject_id
5906       ORDER BY cil.object_id;
5907 
5908     sel_link_rec        sel_link_csr%ROWTYPE;
5909 
5910   BEGIN
5911 
5912     IF (funmode = 'RUN') THEN
5913 
5914       l_linked_subject_list := WF_ENGINE.GetItemAttrText(
5915                                   itemtype        => itemtype,
5916                                   itemkey         => itemkey,
5917                                   aname           => 'UPDATE_LINKED_SUBJECT_LIST' );
5918 
5919       IF (l_linked_subject_list IS NOT NULL) THEN
5920         result := 'COMPLETE:N';
5921       ELSE
5922 
5923         l_overflow_flag := WF_ENGINE.GetItemAttrText(
5924                                 itemtype        => itemtype,
5925                                 itemkey         => itemkey,
5926                                 aname           => 'MORE_UPDT_LINKED_SUBJECT_LIST');
5927 
5928         IF (l_overflow_flag = 'Y') THEN
5929 
5930 	  -- Reset the Overflow flag
5931           WF_ENGINE.SetItemAttrText(
5932                         itemtype        => itemtype,
5933                         itemkey         => itemkey,
5934                         aname           => 'MORE_UPDT_LINKED_SUBJECT_LIST',
5935                         avalue          => '' );
5936 
5937           l_request_number := WF_ENGINE.GetItemAttrText(
5938                                   itemtype        => itemtype,
5939                                   itemkey         => itemkey,
5940                                   aname           => 'REQUEST_NUMBER' );
5941           l_request_id := WF_ENGINE.GetItemAttrNumber(
5942                                   itemtype        => itemtype,
5943                                   itemkey         => itemkey,
5944                                   aname           => 'REQUEST_ID' );
5945           l_relationship_type_name := WF_ENGINE.GetItemAttrText(
5946 	  			  itemtype	=> itemtype,
5947 				  itemkey	=> itemkey,
5948 				  aname		=> 'UPDATE_RELATIONSHIP_TYPE');
5949           l_subject_id := WF_ENGINE.GetItemAttrNumber(
5950               			  itemtype        => itemtype,
5951               			  itemkey         => itemkey,
5952               			  aname           => 'UPDATE_SUBJECT_ID');
5953 
5954 	  OPEN sel_link_csr;
5955 	  LOOP
5956 	    FETCH sel_link_csr INTO sel_link_rec;
5957 	    EXIT WHEN sel_link_csr%NOTFOUND;
5958 	    l_char_subject_id := TO_CHAR(sel_link_rec.object_id);
5959 
5960 	    IF ((nvl(LENGTH(l_linked_subject_list),0) +
5961 		 nvl(LENGTH(l_char_subject_id),0) + 1) <= 4000) THEN
5962 
5963               IF l_linked_subject_list IS NULL THEN
5964                 l_linked_subject_list := l_char_subject_id;
5965               ELSE
5966                 l_linked_subject_list := l_linked_subject_list || ' ' || l_char_subject_id;
5967               END IF;
5968 	    ELSE
5969               WF_ENGINE.SetItemAttrText(
5970                             itemtype        => itemtype,
5971                             itemkey         => itemkey,
5972                             aname           => 'MORE_UPDT_LINKED_SUBJECT_LIST',
5973                             avalue          => 'Y' );
5974 	      EXIT;
5975 
5976             END IF;
5977 
5978           END LOOP;
5979           CLOSE sel_link_csr;
5980 
5981 	  IF (l_linked_subject_list IS NOT NULL) THEN
5982             WF_ENGINE.SetItemAttrText(
5983                     itemtype        => itemtype,
5984                     itemkey         => itemkey,
5985                     aname           => 'UPDATE_LINKED_SUBJECT_LIST',
5986                     avalue          => l_linked_subject_list );
5987 
5988             result := 'COMPLETE:N';
5989 	  ELSE
5990             result := 'COMPLETE:Y';
5991 	  END IF;
5992         ELSE  /*** IF (l_overflow_flag = 'Y') ***/
5993               result := 'COMPLETE:Y';
5994         END IF;
5995 
5996 
5997       END IF;	/** IF (l_linked_subject_list IS NOT NULL) **/
5998 
5999     ELSIF (funmode = 'CANCEL') THEN
6000       result := 'COMPLETE';
6001     END IF;
6002 
6003   EXCEPTION
6004     WHEN OTHERS THEN
6005       WF_CORE.Context('CS_WF_AUTO_NTFY_UPDATE_PKG', 'Verify_All_Links_Done',
6006 		      itemtype, itemkey, actid, funmode);
6007       RAISE;
6008 
6009   END Verify_All_Links_Done;
6010 
6011 
6012 /*************************************************************************
6013   -- Verify_Update_Rules_Done
6014   --   This procedure corresponds to the VRFY_NEW_STATUS function
6015   --   activity.
6016   --   The procedure verifies if the Update Rules (conditions + actions is
6017   --   unique) list has been exhausted. If not then the procedure returns
6018   --   an 'N' which tells the workflow to process the next rule on the list.
6019   --   If the list has been exhausted, we check if the list overload flag
6020   --   is set to 'Y' (which means the initial list did not fit all the
6021   --   queried rows), if it is not then we return a 'Y' telling the
6022   --   workflow that all update rules has been processed. If the overflow
6023   --   flag is 'Y', then we build another list starting from the next
6024   --   condition+action item of the last item on the previous list.
6025 ************************************************************************/
6026 
6027   PROCEDURE Verify_Update_Rules_Done( itemtype   IN  VARCHAR2,
6028                              itemkey    IN  VARCHAR2,
6029                              actid      IN  NUMBER,
6030                              funmode    IN  VARCHAR2,
6031                              result     OUT NOCOPY VARCHAR2 ) IS
6032 
6033     l_update_conditions_list    VARCHAR2(4000);
6034     l_update_actions_list       VARCHAR2(4000);
6035     l_event_name        	VARCHAR2(240);
6036     --l_action_code       	VARCHAR2(30);
6037     l_request_number    	VARCHAR2(64);
6038     l_event_condition_id        CS_SR_ACTION_TRIGGERS.event_condition_id%TYPE;
6039     l_char_event_condition_id   VARCHAR2(30);
6040     l_action_code       	CS_SR_ACTION_DETAILS.action_code%TYPE;
6041 
6042     l_overflow_flag             VARCHAR2(1);
6043     l_request_id		NUMBER;
6044 
6045     CURSOR sel_action_csr IS
6046       SELECT csad.event_condition_id,
6047              csad.action_code,
6048 	     to_char(csad.event_condition_id) || csad.action_code index_cols
6049       FROM CS_SR_ACTION_TRIGGERS csat,
6050            CS_SR_ACTION_DETAILS csad,
6051 	   CS_SR_EVENT_CODES_B cec
6052       WHERE
6053 	  cec.WF_BUSINESS_EVENT_ID = l_event_name
6054           and csat.EVENT_CODE = cec.EVENT_CODE
6055           and TRUNC(SYSDATE) BETWEEN TRUNC(NVL(csat.start_date_active, SYSDATE))
6056               and TRUNC(NVL(csat.end_date_active, SYSDATE))
6057           and csad.event_condition_id = csat.event_condition_id
6058           and TRUNC(SYSDATE) BETWEEN TRUNC(NVL(csad.start_date_active, SYSDATE))
6059               and TRUNC(NVL(csad.end_date_active, SYSDATE))
6060           and csat.from_to_status_code IS NULL
6061           and csad.action_code NOT like 'NOTIFY%'
6062           and (csad.incident_status_id IS NOT NULL OR
6063                csad.resolution_code IS NOT NULL)
6064           and csat.relationship_type_id IN
6065 		( select cil.link_type_id
6066 	          FROM cs_incident_links cil
6067 	          WHERE cil.subject_id = l_request_id)
6068 	  AND TO_CHAR(csad.event_condition_id) || csad.action_code >
6069 		TO_CHAR(l_event_condition_id) || l_action_code
6070       UNION
6071       SELECT csad.event_condition_id,
6072              csad.action_code,
6073 	     to_char(csad.event_condition_id) || csad.action_code index_cols
6074       FROM CS_SR_ACTION_TRIGGERS csat,
6075            CS_SR_ACTION_DETAILS csad,
6076 	   CS_SR_EVENT_CODES_B cec
6077       WHERE
6078 	  cec.WF_BUSINESS_EVENT_ID = l_event_name
6079           and csat.EVENT_CODE = cec.EVENT_CODE
6080           and TRUNC(SYSDATE) BETWEEN TRUNC(NVL(csat.start_date_active, SYSDATE))
6081               and TRUNC(NVL(csat.end_date_active, SYSDATE))
6082           and csad.event_condition_id = csat.event_condition_id
6083           and TRUNC(SYSDATE) BETWEEN TRUNC(NVL(csad.start_date_active, SYSDATE))
6084               and TRUNC(NVL(csad.end_date_active, SYSDATE))
6085           and csat.from_to_status_code IS NOT NULL
6086           and csat.relationship_type_id IS NULL
6087           and csat.incident_status_id IS NOT NULL
6088           and csad.action_code NOT like 'NOTIFY%'
6089           and csad.relationship_type_id IN
6090 		( select cil.link_type_id
6091 	          FROM cs_incident_links cil
6092 	          WHERE cil.subject_id = l_request_id)
6093 	  AND TO_CHAR(csad.event_condition_id) || csad.action_code >
6094 		TO_CHAR(l_event_condition_id) || l_action_code
6095       ORDER BY index_cols;
6096 
6097     sel_action_rec    sel_action_csr%ROWTYPE;
6098 
6099   BEGIN
6100 
6101     IF (funmode = 'RUN') THEN
6102 
6103       l_update_conditions_list := WF_ENGINE.GetItemAttrText(
6104                                   itemtype        => itemtype,
6105                                   itemkey         => itemkey,
6106                                   aname           => 'UPDATE_CONDITION_LIST' );
6107 
6108       l_update_actions_list := WF_ENGINE.GetItemAttrText(
6109                                   itemtype        => itemtype,
6110                                   itemkey         => itemkey,
6111                                   aname           => 'UPDATE_ACTION_LIST' );
6112 
6113       IF (l_update_conditions_list IS NOT NULL AND l_update_actions_list IS NOT NULL) THEN
6114         result := 'COMPLETE:N';
6115       ELSE
6116 
6117         l_overflow_flag := WF_ENGINE.GetItemAttrText(
6118                                 itemtype        => itemtype,
6119                                 itemkey         => itemkey,
6120                                 aname           => 'MORE_UPDATE_ACTION_LIST');
6121 
6122 	IF (l_overflow_flag = 'Y') THEN
6123 
6124           -- Reset the Overflow flag
6125           WF_ENGINE.SetItemAttrText(
6126                       itemtype        => itemtype,
6127                       itemkey         => itemkey,
6128                       aname           => 'MORE_UPDATE_ACTION_LIST',
6129                       avalue          => '' );
6130 
6131 	  l_event_condition_id := WF_ENGINE.GetItemAttrNumber(
6132                     			itemtype        => itemtype,
6133                     			itemkey         => itemkey,
6134                     			aname           => 'UPDATE_EVENT_CONDITION_ID');
6135 	  l_action_code := WF_ENGINE.GetItemAttrText(
6136                     			itemtype        => itemtype,
6137                     			itemkey         => itemkey,
6138                     			aname           => 'UPDATE_ACTION_CODE');
6139           l_event_name := WF_ENGINE.GetItemAttrText(
6140                                       	itemtype        => itemtype,
6141                                       	itemkey         => itemkey,
6142                                       	aname           => 'EVENTNAME' );
6143           l_request_number := WF_ENGINE.GetItemAttrText(
6144                                   	itemtype        => itemtype,
6145                                   	itemkey         => itemkey,
6146                                   	aname           => 'REQUEST_NUMBER' );
6147           l_request_id := WF_ENGINE.GetItemAttrNumber(
6148                                   	itemtype        => itemtype,
6149                                   	itemkey         => itemkey,
6150                                   	aname           => 'REQUEST_ID' );
6151 
6152 	  OPEN sel_action_csr;
6153   	  LOOP
6154 	    FETCH sel_action_csr INTO sel_action_rec;
6155 	    EXIT WHEN sel_action_csr%NOTFOUND;
6156 	    l_char_event_condition_id := TO_CHAR(sel_action_rec.event_condition_id);
6157 
6158 	    IF (nvl(LENGTH(l_update_conditions_list),0) +
6159                 nvl(LENGTH(l_char_event_condition_id),0) + 1) <= 4000  OR
6160                (nvl(LENGTH(l_update_actions_list),0) +
6161                 nvl(LENGTH(sel_action_rec.action_code),0) + 1 ) <= 4000 THEN
6162 
6163               IF l_update_conditions_list IS NULL THEN
6164                 l_update_conditions_list := l_char_event_condition_id;
6165                 l_update_actions_list := sel_action_rec.action_code;
6166               ELSE
6167                 l_update_conditions_list := l_update_conditions_list || ' ' || l_char_event_condition_id;
6168                 l_update_actions_list := l_update_actions_list || ' ' || sel_action_rec.action_code;
6169               END IF;
6170 	    ELSE
6171 
6172               WF_ENGINE.SetItemAttrText(
6173                           itemtype        => itemtype,
6174                           itemkey         => itemkey,
6175                           aname           => 'MORE_UPDATE_ACTION_LIST',
6176                           avalue          => 'Y' );
6177 	      EXIT;
6178 
6179             END IF;
6180 	  END LOOP;
6181 	  CLOSE sel_action_csr;
6182 
6183 	  IF (l_update_conditions_list IS NOT NULL AND l_update_actions_list IS NOT NULL) THEN
6184 
6185             WF_ENGINE.SetItemAttrText(
6186                     itemtype        => itemtype,
6187                     itemkey         => itemkey,
6188                     aname           => 'UPDATE_CONDITION_LIST',
6189                     avalue          => l_update_conditions_list );
6190             WF_ENGINE.SetItemAttrText(
6191                     itemtype        => itemtype,
6192                     itemkey         => itemkey,
6193                     aname           => 'UPDATE_ACTION_LIST',
6194                     avalue          => l_update_actions_list );
6195 
6196             result := 'COMPLETE:N';
6197 	  ELSE
6198             result := 'COMPLETE:Y';
6199 	  END IF;
6200 
6201         ELSE    /** IF (l_overflow_flag = 'Y') **/
6202 
6203           result := 'COMPLETE:Y';
6204 
6205         END IF;
6206 
6207       END IF;
6208 
6209 
6210     ELSIF (funmode = 'CANCEL') THEN
6211       result := 'COMPLETE';
6212     END IF;
6213 
6214   EXCEPTION
6215     WHEN OTHERS THEN
6216       WF_CORE.Context('CS_WF_AUTO_NTFY_UPDATE_PKG', 'Verify_Update_Rules_Done',
6217 		      itemtype, itemkey, actid, funmode);
6218       RAISE;
6219 
6220   END Verify_Update_Rules_Done;
6221 
6222 
6223 /***********************************************************************
6224 -- Get_Request_Attributes
6225 --   This procedure initializes the item attributes that will remain constant
6226 --   over the duration of the Workflow.  These attributes include REQUEST_ID,
6227 --   REQUEST_NUMBER, REQUEST_DATE, and REQUEST_TYPE.
6228 --   Modification History:
6229 --
6230 --  Date        Name        Desc
6231 --  --------    ----------  --------------------------------------
6232 --  06/23/2004  RMANABAT    Fix for bug 3715297. set SENDER_ROLE
6233 --                          attribute when event is NOT raised from BES wrapper
6234 --			    api, like in Solutions Linked event owned by KM.
6235 --  06/08/2006  klou        Fix bug 5245018.  Added call to pass the attribute value
6236 --                          for problem code
6237 ***********************************************************************/
6238 
6239   PROCEDURE Get_Request_Attributes( itemtype        VARCHAR2,
6240                                 itemkey         VARCHAR2,
6241                                 actid           NUMBER,
6242                                 funmode         VARCHAR2,
6243                                 result          OUT NOCOPY VARCHAR2 ) IS
6244 
6245     l_return_status     VARCHAR2(1);
6246     l_msg_count         NUMBER;
6247     l_msg_data          VARCHAR2(2000);
6248     l_owner_role        VARCHAR2(100);
6249     l_owner_name        VARCHAR2(240);
6250     l_errmsg_name       VARCHAR2(30);
6251     l_old_incident_owner_id	NUMBER;
6252     l_new_contact_point_id	NUMBER;
6253     l_new_contact_point_name	VARCHAR2(360);
6254     l_API_ERROR         EXCEPTION;
6255 
6256     l_request_number            VARCHAR2(64);
6257 
6258     l_resource_id       NUMBER;
6259     l_role_display_name VARCHAR2(360);
6260     l_role_name         VARCHAR2(320);
6261 
6262     /******************
6263     CURSOR l_ServiceRequest_csr IS
6264         SELECT *
6265         FROM CS_INCIDENTS_WORKFLOW_V
6266         WHERE INCIDENT_NUMBER = l_request_number;
6267     ***************/
6268 
6269     /**
6270       Going directly to TL tables to solve performance issue of
6271       non-mergeable views and excessive shared memory usage.
6272       rmanabat 03/18/03. Bug 2857365 .
6273     **/
6274 
6275 /* 03/01/2004 - RHUNGUND - Bug fix for 3432700
6276 
6277    Performance fine-tuning.
6278    Removed outer joins from the following where conditions
6279    1)  AND INC.INCIDENT_TYPE_ID = TYPE.INCIDENT_TYPE_ID(+)
6280    2)  AND TYPE.LANGUAGE(+) = userenv('LANG')
6281    3)  AND INC.INCIDENT_URGENCY_ID = URGENCY.INCIDENT_URGENCY_ID(+)
6282    4) AND INC.INCIDENT_SEVERITY_ID = SEVERITY.INCIDENT_SEVERITY_ID(+)
6283 */
6284 
6285 /* 03/31/2004 - RHUNGUND - Bug fix for 3531540
6286    Re-introduce the following outer join :
6287 AND INC.INCIDENT_URGENCY_ID = URGENCY.INCIDENT_URGENCY_ID(+)
6288    since URGENCY is not a mandatory sr attribute and if a sr does not have an URGENCY value, the following SQL will not bring up any records if outer join is not present
6289 */
6290 
6291     CURSOR l_ServiceRequest_csr IS
6292       SELECT  INC.INCIDENT_ID,
6293         INC.SUMMARY,
6294         INC.INCIDENT_OWNER_ID,
6295         INC.INVENTORY_ITEM_ID,
6296         INC.EXPECTED_RESOLUTION_DATE,
6297         INC.INCIDENT_DATE,
6298         INC.CUSTOMER_PRODUCT_ID,
6299         TYPE.NAME INCIDENT_TYPE,
6300         SEVERITY.NAME SEVERITY,
6301         STATUS.NAME STATUS_CODE,
6302         URGENCY.NAME URGENCY,
6303         RA2.PARTY_NAME CUSTOMER_NAME,
6304         CSLKUP.DESCRIPTION PROBLEM_CODE_DESCRIPTION,
6305         MTL.DESCRIPTION PRODUCT_DESCRIPTION,
6306         INC.PROBLEM_CODE --5245018
6307       FROM    CS_INCIDENTS_ALL_VL INC,
6308         --CS_INCIDENT_TYPES_VL TYPE,
6309 	CS_INCIDENT_TYPES_TL TYPE,
6310         --CS_INCIDENT_SEVERITIES_VL SEVERITY,
6311 	CS_INCIDENT_SEVERITIES_TL SEVERITY,
6312         CS_INCIDENT_STATUSES_VL STATUS,
6313         --CS_INCIDENT_URGENCIES_VL URGENCY,
6314         CS_INCIDENT_URGENCIES_TL URGENCY,
6315         HZ_PARTIES RA2,
6316         CS_LOOKUPS CSLKUP,
6317         --MTL_SYSTEM_ITEMS_VL MTL
6318         MTL_SYSTEM_ITEMS_TL MTL
6319       WHERE INC.INCIDENT_NUMBER = l_request_number
6320         AND INC.INCIDENT_TYPE_ID = TYPE.INCIDENT_TYPE_ID
6321 	AND TYPE.LANGUAGE = userenv('LANG')
6322         AND INC.INCIDENT_STATUS_ID = STATUS.INCIDENT_STATUS_ID
6323         AND INC.INCIDENT_URGENCY_ID = URGENCY.INCIDENT_URGENCY_ID(+)
6324 	AND URGENCY.LANGUAGE(+) = userenv('LANG')
6325         AND INC.CUSTOMER_ID = RA2.PARTY_ID(+)
6326         AND INC.INCIDENT_SEVERITY_ID = SEVERITY.INCIDENT_SEVERITY_ID
6327 	AND SEVERITY.LANGUAGE(+) = userenv('LANG')
6328         AND INC.PROBLEM_CODE = CSLKUP.LOOKUP_CODE(+)
6329         AND CSLKUP.LOOKUP_TYPE(+) = 'REQUEST_PROBLEM_CODE'
6330         AND MTL.INVENTORY_ITEM_ID(+) = INC.INVENTORY_ITEM_ID
6331 	AND MTL.LANGUAGE(+) = userenv('LANG')
6332         AND (MTL.ORGANIZATION_ID = CS_STD.Get_Item_Valdn_Orgzn_ID OR MTL.ORGANIZATION_ID IS NULL);
6333         --AND NVL(MTL.ORGANIZATION_ID,CS_STD.Get_Item_Valdn_Orgzn_ID) = CS_STD.Get_Item_Valdn_Orgzn_ID;
6334 
6335       l_ServiceRequest_rec        l_ServiceRequest_csr%ROWTYPE;
6336 
6337 
6338     /***
6339       New 03/17/03. rmanabat
6340       Fix for bug 2837253.
6341     ***/
6342 
6343     l_incident_owner_id		NUMBER;
6344     --l_source_id			NUMBER;
6345 
6346     /***
6347     CURSOR l_get_source_id IS
6348       SELECT emp.source_id
6349       FROM jtf_rs_resource_extns emp
6350       WHERE emp.resource_id = l_incident_owner_id;
6351     ***/
6352 
6353     /*** end New 03/17/03. rmanabat ***/
6354 
6355     CURSOR l_get_source_id(p_resource_id IN NUMBER) IS
6356       select emp.source_id , emp.resource_name
6357       from jtf_rs_resource_extns_vl emp
6358       where emp.resource_id = p_resource_id;
6359     l_get_source_id_rec       l_get_source_id%ROWTYPE;
6360 
6361     CURSOR l_sel_adhocrole_csr(c_role_name IN VARCHAR2) IS
6362       SELECT display_name,expiration_date
6363       FROM wf_local_roles
6364       WHERE name = c_role_name;
6365     l_sel_adhocrole_rec l_sel_adhocrole_csr%ROWTYPE;
6366 
6367   BEGIN
6368 
6369     IF (funmode = 'RUN') THEN
6370 
6371       l_request_number := WF_ENGINE.GetItemAttrText(
6372                               itemtype        => itemtype,
6373                               itemkey         => itemkey,
6374                               aname           => 'REQUEST_NUMBER' );
6375 
6376       OPEN l_ServiceRequest_csr;
6377       FETCH l_ServiceRequest_csr INTO l_ServiceRequest_rec;
6378       CLOSE l_ServiceRequest_csr;
6379 
6380       IF (l_ServiceRequest_rec.incident_owner_id IS NOT NULL) THEN
6381 
6382         l_incident_owner_id := l_ServiceRequest_rec.incident_owner_id;
6383         OPEN l_get_source_id(l_incident_owner_id);
6384         FETCH l_get_source_id INTO l_get_source_id_rec;
6385 
6386         IF (l_get_source_id%FOUND AND l_get_source_id_rec.source_id IS NOT NULL) THEN
6387 
6388           -- Retrieve the role name for the request owner
6389           CS_WORKFLOW_PUB.Get_Employee_Role (
6390                     p_api_version           =>  1.0,
6391                     p_return_status         =>  l_return_status,
6392                     p_msg_count             =>  l_msg_count,
6393                     p_msg_data              =>  l_msg_data,
6394                     --p_employee_id           =>  l_ServiceRequest_rec.incident_owner_id,
6395                     p_employee_id           =>  l_get_source_id_rec.source_id,
6396                     p_role_name             =>  l_owner_role,
6397                     p_role_display_name     =>  l_owner_name );
6398 
6399           IF (l_return_status <> FND_API.G_RET_STS_SUCCESS) OR
6400              (l_owner_role is NULL) THEN
6401             CLOSE l_get_source_id;
6402             wf_core.context( pkg_name       =>  'CS_WORKFLOW_PUB',
6403                              proc_name      =>  'Get_Employee_Role',
6404                              arg1           =>  'p_employee_id=>'||l_get_source_id_rec.source_id);
6405             l_errmsg_name := 'CS_WF_SR_CANT_FIND_OWNER';
6406 
6407             raise l_API_ERROR;
6408           END IF;
6409 
6410         ELSE
6411           CLOSE l_get_source_id;
6412           wf_core.context( pkg_name       =>  'CS_WF_AUTO_NTFY_UPDATE_PKG',
6413                            proc_name      =>  'Get_Request_Attributes',
6414                            arg1           =>  'p_employee_id=>'|| l_get_source_id_rec.source_id);
6415           l_errmsg_name := 'CS_WF_SR_CANT_FIND_OWNER';
6416           raise l_API_ERROR;
6417 
6418         END IF;
6419 
6420         IF l_get_source_id%ISOPEN THEN
6421           CLOSE l_get_source_id;
6422         END IF;
6423 
6424 
6425       END IF;
6426 
6427 
6428 
6429       /*******
6430         Note: Ideally, these attributes should be set before the event is raised, in
6431         the BES wrapper API. Take the scenario when an SR is created and an event is
6432         raised, the event is placed in the WF_DEFERRED queue. If the SR is updated
6433         before the event is de-queued, these attributes would reflect the latest data,
6434         including those which were changed, and not those when the SR was created.
6435         The disadvantage of adding this as part of the parameter list is the per-
6436         formance cost. The more parameters are added when raising the event, the
6437         more theperformance deteriorates (as pointed out by the Workflow team. max.
6438         number of parameters in an event is 100).
6439       *******/
6440 
6441        -- Initialize item attributes that will remain constant
6442       WF_ENGINE.SetItemAttrDate(
6443                 itemtype        => 'SERVEREQ',
6444                 itemkey         => itemkey,
6445                 aname           => 'REQUEST_DATE',
6446                 avalue          => l_ServiceRequest_rec.incident_date );
6447 
6448       WF_ENGINE.SetItemAttrNumber(
6449                 itemtype        => 'SERVEREQ',
6450                 itemkey         => itemkey,
6451                 aname           => 'REQUEST_ID',
6452                 avalue          => l_ServiceRequest_rec.incident_id );
6453 
6454       /***** IF Request_id is given, request_number is derived in Check_Rules_For_Event()
6455       WF_ENGINE.SetItemAttrText(
6456                 itemtype        => 'SERVEREQ',
6457                 itemkey         => itemkey,
6458                 aname           => 'REQUEST_NUMBER',
6459                 avalue          => l_ServiceRequest_rec.incident_number );
6460       *****/
6461 
6462       WF_ENGINE.SetItemAttrText(
6463                 itemtype        => 'SERVEREQ',
6464                 itemkey         => itemkey,
6465                 aname           => 'REQUEST_TYPE',
6466                 avalue          => l_ServiceRequest_rec.incident_type );
6467 
6468 
6469       -- Update service request item attributes
6470       WF_ENGINE.SetItemAttrNumber(
6471                 itemtype        => 'SERVEREQ',
6472                 itemkey         => itemkey,
6473                 aname           => 'OWNER_ID',
6474                 avalue          => l_ServiceRequest_rec.incident_owner_id );
6475 
6476       WF_ENGINE.SetItemAttrText(
6477                 itemtype        => 'SERVEREQ',
6478                 itemkey         => itemkey,
6479                 aname           => 'OWNER_ROLE',
6480                 avalue          => l_owner_role );
6481 
6482       WF_ENGINE.SetItemAttrText(
6483                 itemtype        => 'SERVEREQ',
6484                 itemkey         => itemkey,
6485                 aname           => 'OWNER_NAME',
6486                 avalue          => l_owner_name );
6487 
6488       WF_ENGINE.SetItemAttrNumber(
6489                 itemtype        => 'SERVEREQ',
6490                 itemkey         => itemkey,
6491                 aname           => 'CUSTOMER_PRODUCT_ID',
6492                 avalue          => l_ServiceRequest_rec.customer_product_id );
6493 
6494       WF_ENGINE.SetItemAttrDate(
6495                 itemtype        => 'SERVEREQ',
6496                 itemkey         => itemkey,
6497                 aname           => 'EXPECTED_RESOLUTION_DATE',
6498                 avalue          => l_ServiceRequest_rec.expected_resolution_date );
6499 
6500       WF_ENGINE.SetItemAttrNumber(
6501                 itemtype        => 'SERVEREQ',
6502                 itemkey         => itemkey,
6503                 aname           => 'INVENTORY_ITEM_ID',
6504                 avalue          => l_ServiceRequest_rec.inventory_item_id );
6505 
6506       WF_ENGINE.SetItemAttrText(
6507                 itemtype        => 'SERVEREQ',
6508                 itemkey         => itemkey,
6509                 aname           => 'PROBLEM_DESCRIPTION',
6510                 avalue          => l_ServiceRequest_rec.problem_code_description );
6511 
6512       -- 5245018_start
6513       WF_ENGINE.SetItemAttrText(
6514                 itemtype        => 'SERVEREQ',
6515                 itemkey         => itemkey,
6516                 aname           => 'PROBLEM_CODE',
6517                 avalue          => l_ServiceRequest_rec.problem_code );
6518       -- 5245018_eof
6519 
6520       WF_ENGINE.SetItemAttrText(
6521                 itemtype        => 'SERVEREQ',
6522                 itemkey         => itemkey,
6523                 aname           => 'PRODUCT_DESCRIPTION',
6524                 avalue          => l_ServiceRequest_rec.product_description );
6525 
6526       WF_ENGINE.SetItemAttrText(
6527                 itemtype        => 'SERVEREQ',
6528                 itemkey         => itemkey,
6529                 aname           => 'REQUEST_CUSTOMER',
6530                 avalue          => l_ServiceRequest_rec.customer_name );
6531 
6532       WF_ENGINE.SetItemAttrText(
6533                 itemtype        => 'SERVEREQ',
6534                 itemkey         => itemkey,
6535                 aname           => 'REQUEST_SEVERITY',
6536                 avalue          => l_ServiceRequest_rec.severity );
6537 
6538       WF_ENGINE.SetItemAttrText(
6539                 itemtype        => 'SERVEREQ',
6540                 itemkey         => itemkey,
6541                 aname           => 'REQUEST_STATUS',
6542                 avalue          => l_ServiceRequest_rec.status_code );
6543 
6544       WF_ENGINE.SetItemAttrText(
6545                 itemtype        => 'SERVEREQ',
6546                 itemkey         => itemkey,
6547                 aname           => 'REQUEST_SUMMARY',
6548                 avalue          => l_ServiceRequest_rec.summary );
6549 
6550       WF_ENGINE.SetItemAttrText(
6551                 itemtype        => 'SERVEREQ',
6552                 itemkey         => itemkey,
6553                 aname           => 'REQUEST_URGENCY',
6554                 avalue          => l_ServiceRequest_rec.urgency );
6555 
6556 
6557       -- Get the old incident_owner role if old incident owner is given
6558       l_old_incident_owner_id := WF_ENGINE.GetItemAttrText(
6559                 itemtype        => itemtype,
6560                 itemkey         => itemkey,
6561                 aname           => 'PREV_OWNER_ID');
6562 
6563       IF (l_old_incident_owner_id IS NOT NULL) THEN
6564 
6565         CS_WORKFLOW_PUB.Get_Employee_Role (
6566                   p_api_version           =>  1.0,
6567                   p_return_status         =>  l_return_status,
6568                   p_msg_count             =>  l_msg_count,
6569                   p_msg_data              =>  l_msg_data,
6570                   p_employee_id           =>  l_old_incident_owner_id,
6571                   p_role_name             =>  l_owner_role,
6572                   p_role_display_name     =>  l_owner_name );
6573 
6574         IF (l_owner_role IS NOT NULL) THEN
6575           WF_ENGINE.SetItemAttrText(
6576                   itemtype        => itemtype,
6577                   itemkey         => itemkey,
6578                   aname           => 'PREV_OWNER_ROLE',
6579                   avalue          => l_owner_role );
6580           WF_ENGINE.SetItemAttrText(
6581                   itemtype        => itemtype,
6582                   itemkey         => itemkey,
6583                   aname           => 'PREV_OWNER_NAME',
6584                   avalue          => l_owner_name );
6585 	END IF;
6586 
6587 
6588       END IF;
6589 
6590 
6591       l_role_name := WF_ENGINE.GetItemAttrText(
6592                               itemtype        => itemtype,
6593                               itemkey         => itemkey,
6594                               aname           => 'SENDER_ROLE' );
6595 
6596       IF (l_role_name IS NULL) THEN
6597 
6598         l_resource_id :=  NVL(FND_PROFILE.VALUE('CS_SR_DEFAULT_SYSTEM_RESOURCE'), -1);
6599         OPEN l_get_source_id(l_resource_id);
6600         FETCH l_get_source_id INTO l_get_source_id_rec;
6601 
6602         l_role_name := 'CS_WF_ROLE_DUMMY';
6603         OPEN l_sel_adhocrole_csr(l_role_name);
6604         FETCH l_sel_adhocrole_csr INTO l_sel_adhocrole_rec;
6605 
6606         IF (l_sel_adhocrole_csr%FOUND) THEN
6607 
6608 	  -- expired adhoc role, renew expiration date.
6609           IF (nvl(l_sel_adhocrole_rec.EXPIRATION_DATE, SYSDATE) < sysdate) THEN
6610             wf_directory.SetAdHocRoleExpiration(role_name         => l_role_name,
6611                                                 expiration_date   => sysdate + 365);
6612           END IF;
6613 
6614 	  -- change display name if needed.
6615           IF (l_sel_adhocrole_rec.display_name <> l_get_source_id_rec.resource_name) THEN
6616             l_role_display_name := l_get_source_id_rec.resource_name;
6617             wf_directory.SetAdHocRoleAttr(role_name       => l_role_name,
6618                                           display_name    => l_role_display_name);
6619           END IF;
6620 
6621         ELSE
6622 
6623           wf_directory.CreateAdHocRole(role_name          => l_role_name,
6624                                        role_display_name  => l_role_display_name,
6625                                        expiration_date    => sysdate + 365);
6626 
6627           l_role_display_name := l_get_source_id_rec.resource_name;
6628 
6629           wf_directory.SetAdHocRoleAttr(role_name         => l_role_name,
6630                                         display_name      => l_role_display_name);
6631         END IF;
6632 
6633         IF l_sel_adhocrole_csr%ISOPEN THEN
6634           CLOSE l_sel_adhocrole_csr;
6635         END IF;
6636         IF l_get_source_id%ISOPEN THEN
6637           CLOSE l_get_source_id;
6638         END IF;
6639 
6640         WF_ENGINE.SetItemAttrText(
6641                 itemtype        => 'SERVEREQ',
6642                 itemkey         => itemkey,
6643                 aname           => 'SENDER_ROLE',
6644                 avalue          => l_role_name );
6645 
6646 
6647       END IF;	-- IF (l_role_name IS NULL)
6648 
6649       result := 'COMPLETE';
6650 
6651     ELSIF (funmode = 'CANCEL') THEN
6652       result := 'COMPLETE';
6653     END IF;
6654 
6655   EXCEPTION
6656     WHEN l_API_ERROR THEN
6657       IF (l_ServiceRequest_csr%ISOPEN) THEN
6658         CLOSE l_ServiceRequest_csr;
6659       END IF;
6660       WF_CORE.Raise(l_errmsg_name);
6661     WHEN OTHERS THEN
6662       WF_CORE.Context('CS_WF_AUTO_NTFY_UPDATE_PKG', 'Get_Request_Attributes',
6663                       itemtype, itemkey, actid, funmode);
6664       RAISE;
6665 
6666   END Get_Request_Attributes;
6667 
6668 
6669 /**************************************************************
6670 -- pull_from_list
6671 --   Pull an element from the list , and return the element
6672 --   and the updated list
6673 **************************************************************/
6674 
6675   PROCEDURE pull_from_list(itemlist	IN OUT	NOCOPY VARCHAR2,
6676 			    element	OUT	NOCOPY VARCHAR2) IS
6677 
6678     --l_out_list		VARCHAR2(4000);
6679     l_space_pos		NUMBER;
6680 
6681   BEGIN
6682 
6683     l_space_pos := instr(itemlist, ' ');
6684 
6685     IF l_space_pos <> 0 THEN
6686       element := substr(itemlist, 1, l_space_pos - 1);
6687       itemlist := substr(itemlist, l_space_pos + 1, nvl(length(itemlist),0) - l_space_pos);
6688     ELSE  -- No space, means only one record in list
6689       element := itemlist;
6690       itemlist := NULL;
6691     END IF;
6692 
6693   END pull_from_list;
6694 
6695 
6696 --  ---------------------------------------------------------------------------
6697 --  Procedure   : Get_Fnd_User_Role
6698 --  ---------------------------------------------------------------------------
6699 
6700   PROCEDURE Get_Fnd_User_Role
6701     ( p_fnd_user_id       IN      NUMBER,
6702       x_role_name         OUT     NOCOPY VARCHAR2,
6703       x_role_display_name OUT     NOCOPY VARCHAR2 ) IS
6704 
6705     l_employee_id      NUMBER;
6706 
6707   BEGIN
6708     -- map the FND user to employee ID
6709     SELECT employee_id INTO l_employee_id
6710     FROM fnd_user
6711     WHERE user_id = p_fnd_user_id;
6712 
6713     IF (l_employee_id IS NOT NULL) THEN
6714       wf_directory.getrolename
6715         ( p_orig_system         => 'PER',
6716           p_orig_system_id      => l_employee_id,
6717           p_name                => x_role_name,
6718           p_display_name        => x_role_display_name );
6719     ELSE
6720       wf_directory.getrolename
6721         ( p_orig_system         => 'FND_USR',
6722           p_orig_system_id      => p_fnd_user_id,
6723           p_name                => x_role_name,
6724           p_display_name        => x_role_display_name );
6725     END IF;
6726   END Get_Fnd_User_Role;
6727 
6728 
6729 
6730   /***********************************************************************
6731   -- Create_Interaction_Activity
6732   --
6733   -- Modification History:
6734   --  Date        Name        Desc
6735   --  --------    ----------  --------------------------------------
6736   --  07/14/2004  RMANABAT    Fix for bug 3766921. Removed literals used in
6737   --			      cursors.
6738   ***********************************************************************/
6739   PROCEDURE Create_Interaction_Activity(
6740                         p_api_revision  IN      NUMBER,
6741                         p_init_msg_list IN      VARCHAR2,
6742                         p_commit        IN      VARCHAR2,
6743                         p_incident_id   IN      NUMBER,
6744                         p_incident_number       IN VARCHAR2 DEFAULT NULL,
6745                         p_party_id      IN      NUMBER,
6746                         p_user_id       IN      NUMBER,
6747                         p_resp_appl_id  IN      NUMBER,
6748                         p_resp_id       IN      NUMBER,
6749                         p_login_id      IN      NUMBER,
6750                         x_return_status OUT     NOCOPY  VARCHAR2,
6751                         x_resource_id   OUT     NOCOPY  NUMBER,
6752                         x_resource_type OUT     NOCOPY  VARCHAR2,
6753                         x_msg_count     OUT     NOCOPY  NUMBER,
6754                         x_msg_data      OUT     NOCOPY  VARCHAR2) IS
6755 
6756 
6757     c_wrap_id           NUMBER;
6758     l_int_wrapup_id     NUMBER;
6759     l_act_wrapup_id     NUMBER;
6760     --l_return_status   VARCHAR2(1);
6761     --l_msg_count               NUMBER;
6762     --l_msg_data                VARCHAR2(2000);
6763     l_action_id         NUMBER;
6764     l_action_item_id    NUMBER;
6765     l_user_id           NUMBER;
6766 
6767     p_wrap_id           NUMBER;
6768     l_new_interaction_id        NUMBER;
6769     l_interaction_rec   JTF_IH_PUB.interaction_rec_type;
6770     l_activity_rec      JTF_IH_PUB.activity_rec_type;
6771 
6772     l_default_int_outcome_id    NUMBER;
6773     l_default_int_outcome       jtf_ih_wrap_ups_vl.outcome_short_desc%TYPE;
6774     l_result_reqd               jtf_ih_wrap_ups_vl.result_required%TYPE;
6775     l_default_int_result_id     NUMBER;
6776     l_default_int_result        jtf_ih_wrap_ups_vl.result_short_desc%TYPE;
6777     l_reason_reqd               jtf_ih_wrap_ups_vl.reason_required%TYPE;
6778     l_default_int_reason_id     jtf_ih_wrap_ups_vl.reason_id%TYPE;
6779     l_default_int_reason        jtf_ih_wrap_ups_vl.reason_short_desc%TYPE;
6780     l_incident_number           VARCHAR2(64);
6781     l_default_result_id         NUMBER;
6782     l_default_reason_id         NUMBER;
6783     l_default_result            jtf_ih_wrap_ups_vl.result_short_desc%TYPE;
6784     l_activity_id               NUMBER;
6785     l_default_reason            jtf_ih_wrap_ups_vl.reason_short_desc%TYPE;
6786 
6787     l_default_outcome_id        NUMBER;
6788     l_default_outcome           jtf_ih_wrap_ups_vl.outcome_short_desc%TYPE;
6789 
6790     l_action_item		VARCHAR2(80);
6791     l_action			VARCHAR2(80);
6792 
6793     l_NO_RESOURCE_FOUND		EXCEPTION;
6794 
6795     CURSOR is_interaction_active(c_incident_id number) IS
6796       select b.interaction_id
6797       from jtf_ih_activities a,
6798            jtf_ih_interactions b
6799       where a.doc_ref='SR'
6800             and a.doc_id = c_incident_id
6801             and a.interaction_id = b.interaction_id
6802             and b.active = 'Y';
6803 
6804     CURSOR wrap_cur IS
6805       select outcome_id,
6806              outcome_short_desc,
6807              result_required,
6808              result_id,
6809              result_short_desc,
6810              reason_required,
6811              reason_id,
6812              reason_short_desc
6813       from jtf_ih_wrap_ups_vl
6814       where wrap_id = c_wrap_id;
6815 
6816     CURSOR c_action_item_id IS
6817       SELECT nvl(action_item_id,0)
6818       FROM jtf_ih_action_items_vl
6819       --WHERE action_item = 'Sr'
6820       WHERE action_item = l_action_item
6821             AND  rownum < 2;
6822 
6823     CURSOR c_action_id IS
6824       SELECT nvl(action_id,0)
6825       FROM jtf_ih_actions_vl
6826       --WHERE action = 'Automated Email Sent'
6827       WHERE action = l_action
6828             AND  rownum < 2;
6829 
6830     CURSOR c_incident_number IS
6831       SELECT incident_number
6832       FROM cs_incidents_all_b
6833       WHERE incident_id = p_incident_id;
6834 
6835 
6836     CURSOR sel_contact_party_name_csr IS
6837       SELECT PARTY_NAME
6838       FROM HZ_PARTIES
6839       WHERE
6840           PARTY_ID = p_party_id;
6841 
6842     l_int_profile       varchar2(30);
6843     l_failed            NUMBER := 0;
6844     l_msg_index_OUT     NUMBER;
6845     l_error_text        VARCHAR2(2000);
6846     NL                  VARCHAR2(1);
6847     d_user_id		NUMBER;
6848     d_resp_appl_id	NUMBER;
6849     d_resp_id		NUMBER;
6850     d_login_id		NUMBER;
6851 
6852   BEGIN
6853 
6854     x_return_status := FND_API.G_RET_STS_SUCCESS;
6855 
6856     l_int_wrapup_id := FND_PROFILE.Value('CSC_CC_WRAPUP_INTERACTION_DEFAULTS');
6857 
6858     l_action_item := 'Sr';
6859     l_action := 'Automated Email Sent';
6860 
6861     l_interaction_rec := JTF_IH_PUB.INIT_INTERACTION_REC;
6862     l_activity_rec := JTF_IH_PUB.INIT_ACTIVITY_REC;
6863 
6864     l_int_profile := FND_PROFILE.Value('CS_SR_INTERACTION_LOGGING');
6865     NL := fnd_global.Local_Chr(10);
6866 
6867     IF ( l_int_profile is  null or  l_int_profile ='AUTO_INT_LOG' ) THEN
6868 
6869 
6870       IF (p_user_id is null) THEN
6871        fnd_profile.get('USER_ID', d_user_id);
6872       ELSE
6873 	d_user_id := p_user_id;
6874       END  IF;
6875 
6876       IF (p_login_id is null) THEN
6877         fnd_profile.get('LOGIN_ID', d_login_id);
6878       ELSE
6879 	d_login_id := p_login_id;
6880       END IF;
6881 
6882       IF (p_resp_id is null) THEN
6883         fnd_profile.get('RESP_ID', d_resp_id);
6884       ELSE
6885 	d_resp_id := p_resp_id;
6886       END IF;
6887 
6888       IF (p_resp_appl_id is null) THEN
6889         fnd_profile.get('RESP_APPL_ID', d_resp_appl_id);
6890       ELSE
6891 	d_resp_appl_id := p_resp_appl_id;
6892       END IF;
6893 
6894 
6895       IF ( l_int_wrapup_id is not null ) THEN
6896         c_wrap_id := l_int_wrapup_id;
6897 
6898         OPEN wrap_cur;
6899         fetch wrap_cur into
6900           l_default_int_outcome_id,
6901           l_default_int_outcome,
6902           l_result_reqd,
6903           l_default_int_result_id,
6904           l_default_int_result,
6905           l_reason_reqd,
6906           l_default_int_reason_id,
6907           l_default_int_reason;
6908         close wrap_cur;
6909 
6910 /* Roopa - Begin - Fix for bug 3360069 */
6911 /* Initializing the local vars with FND_GLOBAL values if the local vars are null in value */
6912 /* It's necessary to hard code the value of d_resp_appl_id to 170 since
6913    WF item attr RESP_APPL_ID has returned a null value and create and close
6914    interaction calls will fail if we pass a null value for resp_appl_id/handler_id */
6915        IF(d_resp_appl_id is NULL or d_resp_appl_id = -1) THEN
6916 		d_resp_appl_id := 170;
6917        END IF;
6918 
6919 	IF(d_resp_id is NULL) THEN
6920 		d_resp_id := FND_GLOBAL.RESP_ID;
6921 	END IF;
6922 
6923 	IF(d_user_id is NULL) THEN
6924 		d_user_id := FND_GLOBAL.USER_ID;
6925 	END IF;
6926 
6927 	IF(d_login_id is NULL) THEN
6928 		d_login_id := FND_GLOBAL.LOGIN_ID;
6929 	END IF;
6930 /* Roopa - End - Fix for bug 3360069 */
6931 
6932         l_interaction_rec.party_id := p_party_id;
6933         -- Get the application id AND pass that IN handler_id
6934         l_interaction_rec.handler_id := d_resp_appl_id;
6935         l_user_id := d_user_id;
6936 
6937         -- Get the corresponding RESOURCE_ID, fOR the l_user_id, FROM JTF Resource schema
6938 	/****
6939         SELECT  a.resource_id
6940         into    l_interaction_rec.resource_id
6941         FROM    jtf_rs_resource_extns a
6942         WHERE   a.user_id    = l_user_id;
6943 	****/
6944 
6945 	-- Get the resource from the profile instead. Part of fix for bug 3360069.
6946 	-- rmanabat 01/19/04.
6947         l_interaction_rec.resource_id := FND_PROFILE.VALUE('CS_SR_DEFAULT_SYSTEM_RESOURCE');
6948 	IF (l_interaction_rec.resource_id is NULL) THEN
6949 	  raise l_NO_RESOURCE_FOUND;
6950 	END IF;
6951 
6952         l_interaction_rec.outcome_id    := l_default_int_outcome_id;
6953         l_interaction_rec.result_id     := l_default_int_result_id;
6954         l_interaction_rec.reason_id     := l_default_int_reason_id;
6955          l_interaction_rec.start_date_time  := sysdate;
6956         l_interaction_rec.duration         := 0;
6957 
6958 
6959         -- Start the interaction
6960        jtf_ih_pub.open_interaction(
6961                         1.0,
6962                         p_init_msg_list,
6963                         p_commit,
6964                         d_resp_appl_id,
6965                         d_resp_id,
6966                         d_user_id,
6967                         d_login_id,
6968                         x_return_status,
6969                         x_msg_count,
6970                         x_msg_data,
6971                         l_interaction_rec,
6972                         l_new_interaction_id);
6973 
6974 
6975       END IF;  -- IF ( l_int_wrapup_id is not null )
6976 
6977 
6978       IF (l_new_interaction_id IS NULL OR x_return_status <> FND_API.G_RET_STS_SUCCESS) THEN
6979         RAISE FND_API.G_EXC_ERROR;
6980 
6981       ELSE
6982 
6983 
6984         l_act_wrapup_id := FND_PROFILE.Value('CSC_CC_WRAPUP_ACTIVITY_DEFAULTS');
6985 
6986         IF (l_act_wrapup_id is not null) THEN
6987         -- Get the Activity defaults
6988           p_wrap_id := l_act_wrapup_id;
6989 
6990           OPEN wrap_cur;
6991           FETCH wrap_cur INTO
6992             l_default_outcome_id,
6993             l_default_outcome,
6994             l_result_reqd,
6995             l_default_result_id,
6996             l_default_result,
6997             l_reason_reqd,
6998             l_default_reason_id,
6999             l_default_reason;
7000           CLOSE wrap_cur;
7001 
7002           OPEN c_action_item_id;
7003           FETCH c_action_item_id INTO
7004             l_action_item_id;
7005           CLOSE c_action_item_id;
7006 
7007           OPEN c_action_id;
7008           FETCH c_action_id INTO l_action_id;
7009           CLOSE c_action_id;
7010 
7011           IF (p_incident_number IS NULL) THEN
7012             OPEN c_incident_number;
7013             FETCH c_incident_number INTO l_incident_number;
7014             CLOSE c_incident_number;
7015           ELSE
7016             l_incident_number := p_incident_number;
7017           END IF;
7018 
7019           l_activity_rec.interaction_id  := l_new_interaction_id;
7020           l_activity_rec.action_id       := l_action_id;
7021           l_activity_rec.outcome_id      := l_default_outcome_id;
7022           l_activity_rec.result_id       := l_default_result_id;
7023           l_activity_rec.reason_id       := l_default_reason_id;
7024           l_activity_rec.action_item_id  := l_action_item_id;
7025           --l_activity_rec.cust_account_id := l_account_id;
7026           l_activity_rec.doc_id          := p_incident_id;
7027           l_activity_rec.doc_ref         := 'SR';
7028           l_activity_rec.doc_source_object_name := l_incident_number;
7029           l_activity_rec.start_date_time := sysdate;
7030           --l_activity_rec.end_date_time := fnd_standard.system_date;
7031           l_activity_rec.end_date_time := sysdate;
7032 
7033 
7034           jtf_ih_pub.add_activity( 1.0,
7035                                    p_init_msg_list,
7036                                    p_commit,
7037                                    d_resp_appl_id,
7038                                    d_resp_id,
7039                                    d_user_id,
7040                                    d_login_id,
7041                                    x_return_status,
7042                                    x_msg_count,
7043                                    x_msg_data,
7044                                    l_activity_rec,
7045                                    l_activity_id);
7046 
7047 
7048           IF (l_activity_id IS NULL OR x_return_status <> FND_API.G_RET_STS_SUCCESS) THEN
7049 
7050             raise FND_API.G_EXC_ERROR;
7051             /**********
7052             l_failed = 1;
7053 
7054             IF ( FND_MSG_PUB.Count_Msg > 0) THEN
7055               FOR i IN 1..FND_MSG_PUB.Count_Msg    LOOP
7056                 FND_MSG_PUB.Get(p_msg_index     => i,
7057                                 p_encoded       => 'F',
7058                                 p_data          => l_msg_data,
7059                                 p_msg_index_OUT => l_msg_index_OUT );
7060                 l_error_text := l_error_text || l_msg_data ||NL;
7061               END LOOP;
7062             END IF;
7063             **********/
7064 
7065           END IF;
7066 
7067 
7068         END IF; -- IF (l_act_wrapup_id is not null)
7069 
7070 /* Roopa - Begin - Fix for bug 3360069 */
7071 /* Initializing the local vars with FND_GLOBAL values if the local vars are null in value */
7072        IF(d_resp_appl_id is NULL or d_resp_appl_id = -1) THEN
7073 		d_resp_appl_id := 170;
7074        END IF;
7075 
7076 	IF(d_resp_id is NULL) THEN
7077 		d_resp_id := FND_GLOBAL.RESP_ID;
7078 	END IF;
7079 
7080 	IF(d_user_id is NULL) THEN
7081 		d_user_id := FND_GLOBAL.USER_ID;
7082 	END IF;
7083 
7084 	IF(d_login_id is NULL) THEN
7085 		d_login_id := FND_GLOBAL.LOGIN_ID;
7086 	END IF;
7087 /* Roopa - End - Fix for bug 3360069 */
7088 
7089 
7090         l_interaction_rec.handler_id     := d_resp_appl_id;
7091         l_interaction_rec.interaction_id := l_new_interaction_id;
7092         l_interaction_rec.outcome_id  := l_default_int_outcome_id;
7093         l_interaction_rec.result_id  := l_default_result_id;
7094         l_interaction_rec.reason_id  := l_default_reason_id;
7095         --l_interaction_rec.end_date_time  := fnd_standard.system_date;
7096         l_interaction_rec.end_date_time  := sysdate;
7097 
7098         jtf_ih_pub.close_interaction
7099                                    ( 1.0,
7100                                      p_init_msg_list,
7101                                      p_commit,
7102                                      d_resp_appl_id,
7103                                      d_resp_id,
7104                                      d_user_id,
7105                                      d_login_id,
7106                                      x_return_status,
7107                                      x_msg_count,
7108                                      x_msg_data,
7109                                      l_interaction_rec);
7110 
7111         IF (x_return_status <> FND_API.G_RET_STS_SUCCESS) THEN
7112           raise FND_API.G_EXC_ERROR;
7113         END IF;
7114 
7115       END IF;   -- (l_new_interaction_id IS NOT NULL)
7116 
7117 
7118 
7119 
7120     END IF;  -- IF ( l_int_profile is  null or  l_int_profile ='AUTO_INT_LOG' )
7121 
7122   EXCEPTION
7123 
7124     WHEN l_NO_RESOURCE_FOUND THEN
7125       x_return_status := FND_API.G_RET_STS_ERROR;
7126       FND_MSG_PUB.initialize;
7127       FND_MESSAGE.Set_Name('CS', 'CS_SR_WF_NO_INT_RESOURCE');
7128       FND_MESSAGE.Set_Token('API_NAME', 'CS_WF_AUTO_NTFY_UPDATE_PKG.Create_Interaction_Activity');
7129       FND_MSG_PUB.ADD;
7130       FND_MSG_PUB.Count_And_Get
7131         ( p_count => x_msg_count,
7132           p_data  => x_msg_data
7133         );
7134 
7135     WHEN FND_API.G_EXC_ERROR THEN
7136       x_return_status := FND_API.G_RET_STS_ERROR;
7137       FND_MSG_PUB.Count_And_Get
7138         ( p_count => x_msg_count,
7139           p_data  => x_msg_data
7140         );
7141 
7142     WHEN OTHERS THEN
7143       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
7144       IF FND_MSG_PUB.Check_Msg_Level(FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
7145         FND_MSG_PUB.Add_Exc_Msg('CS_WF_AUTO_NTFY_UPDATE_PKG', 'Create_Interaction_Activity');
7146       END IF;
7147       FND_MSG_PUB.Count_And_Get
7148         ( p_count => x_msg_count,
7149           p_data  => x_msg_data
7150         );
7151 
7152 
7153   END Create_Interaction_Activity;
7154 
7155 
7156 
7157   /***********************************************************************
7158   -- SetWorkflowAdhocUser
7159   --   This procedure creates/updates a dummy adhoc user . This adhoc user
7160   --   is assigned the email address from the IN parameter and it's
7161   --   expiration date is extended if it has expired. This adhoc user is mainly
7162   --   used for notificaion to contacts, for performance reasons.
7163   --
7164   -- Modification History:
7165   --  Date        Name        Desc
7166   --  --------    ----------  --------------------------------------
7167   --  07/13/2004  RMANABAT    Created.
7168   --  07/28/2004  RMANABAT    Changed referred table in cursor from WF_LOCAL_USERS
7169   --			      to WF_LOCAL_ROLES since calling create_adhocuser()
7170   --			      creates a row in WF_LOCAL_ROLES .
7171   ***********************************************************************/
7172 
7173   PROCEDURE SetWorkflowAdhocUser(
7174 			p_wf_username	IN OUT NOCOPY VARCHAR2,
7175 			p_email_address IN VARCHAR2,
7176 			p_display_name	IN VARCHAR2 DEFAULT NULL) IS
7177 
7178     CURSOR sel_user_csr(c_user_name IN VARCHAR2) IS
7179       SELECT display_name,expiration_date
7180       FROM wf_local_roles
7181       WHERE name = c_user_name;
7182     l_sel_user_rec sel_user_csr%ROWTYPE;
7183 
7184     l_wf_username	VARCHAR2(320);
7185     l_user_display_name	VARCHAR2(360);
7186 
7187   BEGIN
7188 
7189     l_wf_username := p_wf_username;
7190     l_user_display_name := p_display_name;
7191 
7192     OPEN sel_user_csr(l_wf_username);
7193     FETCH sel_user_csr INTO l_sel_user_rec;
7194 
7195     IF (sel_user_csr%FOUND) THEN
7196 
7197       -- expired adhoc user, renew expiration date.
7198       IF (nvl(l_sel_user_rec.EXPIRATION_DATE, SYSDATE) < sysdate) THEN
7199         wf_directory.SetAdHocUserExpiration(user_name         => l_wf_username,
7200                                             expiration_date   => sysdate + 365);
7201       END IF;
7202 
7203       wf_directory.SetAdHocUserAttr(user_name		=> l_wf_username,
7204 				    display_name	=> l_user_display_name,
7205                                     email_address	=> p_email_address);
7206 
7207     ELSE
7208 
7209       WF_DIRECTORY.CreateAdHocUser(name			=> l_wf_username,
7210                                    display_name		=> l_user_display_name,
7211                                    email_address	=> p_email_address);
7212     END IF;
7213 
7214     p_wf_username := l_wf_username;
7215 
7216     IF sel_user_csr%ISOPEN THEN
7217       CLOSE sel_user_csr;
7218     END IF;
7219 
7220   END SetWorkflowAdhocUser;
7221 
7222 
7223   /***********************************************************************
7224   -- SetWorkflowAdhocRole
7225   --   This procedure creates/updates a dummy adhoc role . This adhoc role
7226   --   is assigned the user list from the IN parameter and it's
7227   --   expiration date is extended if it has expired. This adhoc role is mainly
7228   --   used for notificaion to contacts, for performance reasons.
7229   --
7230   -- Modification History:
7231   --  Date        Name        Desc
7232   --  --------    ----------  --------------------------------------
7233   --  07/13/2004  RMANABAT    Created.
7234   ***********************************************************************/
7235   PROCEDURE SetWorkflowAdhocRole(
7236 			p_wf_rolename	IN OUT NOCOPY VARCHAR2,
7237 			p_user_list	IN VARCHAR2) IS
7238 
7239     CURSOR l_sel_adhocrole_csr(c_role_name IN VARCHAR2) IS
7240       SELECT display_name,expiration_date
7241       FROM wf_local_roles
7242       WHERE name = c_role_name;
7243     l_sel_adhocrole_rec l_sel_adhocrole_csr%ROWTYPE;
7244 
7245     l_wf_rolename       VARCHAR2(320);
7246     l_role_display_name VARCHAR2(360);
7247 
7248   BEGIN
7249 
7250     l_wf_rolename := p_wf_rolename;
7251 
7252     OPEN l_sel_adhocrole_csr(l_wf_rolename);
7253     FETCH l_sel_adhocrole_csr INTO l_sel_adhocrole_rec;
7254 
7255     IF (l_sel_adhocrole_csr%FOUND) THEN
7256 
7257       -- expired adhoc role, renew expiration date.
7258       IF (nvl(l_sel_adhocrole_rec.EXPIRATION_DATE, SYSDATE) < sysdate) THEN
7259         wf_directory.SetAdHocRoleExpiration(role_name         => l_wf_rolename,
7260                                             expiration_date   => sysdate + 365);
7261       END IF;
7262 
7263       wf_directory.RemoveUsersFromAdHocRole(role_name	=> l_wf_rolename);
7264 
7265       wf_directory.AddUsersToAdHocRole(role_name	=> l_wf_rolename,
7266                                        role_users	=> p_user_list);
7267 
7268     ELSE
7269 
7270       wf_directory.CreateAdHocRole(role_name		=> l_wf_rolename,
7271                                    role_display_name	=> l_role_display_name,
7272                                    role_users		=> p_user_list);
7273 
7274     END IF;
7275 
7276     IF l_sel_adhocrole_csr%ISOPEN THEN
7277       CLOSE l_sel_adhocrole_csr;
7278     END IF;
7279 
7280   END SetWorkflowAdhocRole;
7281 
7282 
7283   PROCEDURE Prepare_HTML_Notification
7284               ( itemtype   IN  VARCHAR2,
7285                 itemkey    IN  VARCHAR2,
7286                 actid      IN  NUMBER,
7287                 funmode    IN  VARCHAR2,
7288                 result     OUT NOCOPY VARCHAR2 ) IS
7289 
7290   l_request_id               NUMBER;
7291   l_contact_id               NUMBER;
7292   l_contact_email            VARCHAR2(240);
7293   l_adhoc_user               VARCHAR2(240);
7294   l_contact_type             VARCHAR2(240);
7295   l_language                 VARCHAR2(240);
7296   l_notification_pref        VARCHAR2(240);
7297   l_message_name             VARCHAR2(240);
7298 
7299   l_contact_id_list          VARCHAR2(2000);
7300   l_contact_email_list       VARCHAR2(2000);
7301   l_adhoc_user_list          VARCHAR2(2000);
7302   l_contact_type_list        VARCHAR2(2000);
7303   l_language_list            VARCHAR2(2000);
7304   l_notification_pref_list   VARCHAR2(2000);
7305 
7306   l_element                  VARCHAR2(100);
7307   l_element1                 VARCHAR2(100);
7308   l_element2                 VARCHAR2(100);
7309   l_notification_template    VARCHAR2(240);
7310   l_user_display_name        VARCHAR2(240);
7311   l_notify_recipient         VARCHAR2(240);
7312   l_return_status            VARCHAR2(3);
7313   l_msg_count                NUMBER;
7314   l_msg_data                 VARCHAr2(1000);
7315   l_adhoc_role               VARCHAr2(240);
7316   l_message_id               NUMBER;
7317   l_count                    NUMBER := 0;
7318   l_request_number           NUMBER;
7319   l_sender                   VARCHAR2(240);
7320   l_from_status              VARCHAR2(240);
7321   l_to_status                VARCHAR2(240);
7322 
7323 
7324   BEGIN
7325 
7326   -- Log debug messages
7327 
7328       IF((FND_LOG.LEVEL_EXCEPTION >= FND_LOG.G_CURRENT_RUNTIME_LEVEL)) THEN
7329         IF (FND_LOG.TEST(FND_LOG.LEVEL_PROCEDURE, 'cs.plsql.CS_WF_AUTO_NTFY_UPDATE_PKG.PREPARE_HTML_NOTIFICATION')) THEN
7330 
7331           dbg_msg := ('In CS_WF_AUTO_NTFY_UPDATE_PKG.PREPARE_HTML_NOTIFICATION Procedure');
7332           IF( FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL ) THEN
7333               FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE, 'cs.plsql.CS_WF_AUTO_NTFY_UPDATE_PKG.PREPARE_HTML_NOTIFICATION', dbg_msg);
7334           END IF;
7335 
7336           dbg_msg := ('ItemType :'||ItemType);
7337           IF( FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL ) THEN
7338               FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE, 'cs.plsql.CS_WF_AUTO_NTFY_UPDATE_PKG.PREPARE_HTML_NOTIFICATION', dbg_msg);
7339           END IF;
7340 
7341           dbg_msg := ('ItemKey :'||ItemKey);
7342           IF( FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL ) THEN
7343               FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE, 'cs.plsql.CS_WF_AUTO_NTFY_UPDATE_PKG.PREPARE_HTML_NOTIFICATION', dbg_msg);
7344           END IF;
7345 
7346         END IF;
7347       END IF;
7348 
7349 
7350   -- Get necessary item attributes
7351 
7352 
7353   IF (funmode = 'RUN') THEN
7354 
7355      l_request_id             := WF_ENGINE.GetItemAttrText(
7356                                          itemtype        => itemtype,
7357                                          itemkey         => itemkey,
7358                                          aname           => 'REQUEST_ID' );
7359 
7360      l_request_number          := WF_ENGINE.GetItemAttrText(
7361                                          itemtype        => itemtype,
7362                                          itemkey         => itemkey,
7363                                          aname           => 'REQUEST_NUMBER' );
7364 
7365      l_sender                  :=  WF_ENGINE.GetItemAttrText(
7366                                          itemtype        => itemtype,
7367                                          itemkey         => itemkey,
7368                                          aname           => 'SENDER_ROLE' );
7369 
7370      l_contact_id_list  := WF_ENGINE.GetItemAttrText(
7371                                          itemtype        => itemtype,
7372                                          itemkey         => itemkey,
7373                                          aname           => 'CONTACT_ID_LIST' );
7374 
7375      l_contact_email_list     := WF_ENGINE.GetItemAttrText(
7376                                          itemtype        => itemtype,
7377                                          itemkey         => itemkey,
7378                                          aname           => 'CONTACT_EMAIL_LIST' );
7379 
7380      l_adhoc_user_list        := WF_ENGINE.GetItemAttrText(
7381                                           itemtype        => itemtype,
7382                                           itemkey         => itemkey,
7383                                           aname           => 'ADHOC_USER_LIST');
7384 
7385      l_contact_type_list      := WF_ENGINE.GetItemAttrText(
7386                                           itemtype        => itemtype,
7387                                           itemkey         => itemkey,
7388                                           aname           => 'CONTACT_TYPE_LIST');
7389 
7390      l_language_list          := WF_ENGINE.GetItemAttrText(
7391                                           itemtype        => itemtype,
7392                                           itemkey         => itemkey,
7393                                           aname           => 'LANGUAGE_LIST');
7394 
7395      l_notification_pref_list := WF_ENGINE.GetItemAttrText(
7396                                           itemtype        => itemtype,
7397                                           itemkey         => itemkey,
7398                                           aname           => 'NOTIFICATION_PREFERENCE_LIST');
7399 
7400      l_message_name           := WF_ENGINE.GetItemAttrText(
7401                                           itemtype        => itemtype,
7402                                           itemkey         => itemkey,
7403                                           aname           => 'NTFY_MESSAGE_NAME');
7404 
7405 
7406       IF((FND_LOG.LEVEL_EXCEPTION >= FND_LOG.G_CURRENT_RUNTIME_LEVEL)) THEN
7407         IF (FND_LOG.TEST(FND_LOG.LEVEL_PROCEDURE, 'cs.plsql.CS_WF_AUTO_NTFY_UPDATE_PKG.PREPARE_HTML_NOTIFICATION')) THEN
7408 
7409           dbg_msg := ('l_request_id : '||l_request_id);
7410           IF( FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL ) THEN
7411               FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE, 'cs.plsql.CS_WF_AUTO_NTFY_UPDATE_PKG.PREPARE_HTML_NOTIFICATION', dbg_msg);
7412           END IF;
7413 
7414           dbg_msg := ('l_request_number : '||l_request_number);
7415           IF( FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL ) THEN
7416               FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE, 'cs.plsql.CS_WF_AUTO_NTFY_UPDATE_PKG.PREPARE_HTML_NOTIFICATION', dbg_msg);
7417           END IF;
7418 
7419           dbg_msg := ('l_sender : '||l_sender);
7420           IF( FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL ) THEN
7421               FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE, 'cs.plsql.CS_WF_AUTO_NTFY_UPDATE_PKG.PREPARE_HTML_NOTIFICATION', dbg_msg);
7422           END IF;
7423 
7424           dbg_msg := ('l_contact_id_list : ' ||l_contact_id_list);
7425           IF( FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL ) THEN
7426               FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE, 'cs.plsql.CS_WF_AUTO_NTFY_UPDATE_PKG.PREPARE_HTML_NOTIFICATION', dbg_msg);
7427           END IF;
7428 
7429           dbg_msg := ('l_contact_email_list : '||l_contact_email_list);
7430           IF( FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL ) THEN
7431               FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE, 'cs.plsql.CS_WF_AUTO_NTFY_UPDATE_PKG.PREPARE_HTML_NOTIFICATION', dbg_msg);
7432           END IF;
7433 
7434           dbg_msg := ('l_adhoc_user_list : '||l_adhoc_user_list);
7435           IF( FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL ) THEN
7436               FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE, 'cs.plsql.CS_WF_AUTO_NTFY_UPDATE_PKG.PREPARE_HTML_NOTIFICATION', dbg_msg);
7437           END IF;
7438 
7439           dbg_msg := ('l_contact_type_list : '||l_contact_type_list);
7440           IF( FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL ) THEN
7441               FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE, 'cs.plsql.CS_WF_AUTO_NTFY_UPDATE_PKG.PREPARE_HTML_NOTIFICATION', dbg_msg);
7442           END IF;
7443 
7444           dbg_msg := ('l_language_list : '||l_language_list);
7445           IF( FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL ) THEN
7446               FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE, 'cs.plsql.CS_WF_AUTO_NTFY_UPDATE_PKG.PREPARE_HTML_NOTIFICATION', dbg_msg);
7447           END IF;
7448 
7449           dbg_msg := ('l_notification_pref_list : '||l_notification_pref_list);
7450           IF( FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL ) THEN
7451               FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE, 'cs.plsql.CS_WF_AUTO_NTFY_UPDATE_PKG.PREPARE_HTML_NOTIFICATION', dbg_msg);
7452           END IF;
7453 
7454           dbg_msg := ('l_message_name : '||l_message_name);
7455           IF( FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL ) THEN
7456               FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE, 'cs.plsql.CS_WF_AUTO_NTFY_UPDATE_PKG.PREPARE_HTML_NOTIFICATION', dbg_msg);
7457           END IF;
7458 
7459         END IF;
7460       END IF;
7461 
7462      IF l_message_name = 'CS_SR_IBU_EVT_STATUS_CHANGED' THEN
7463 
7464 	    l_from_status := WF_ENGINE.GetItemAttrText(
7465                                         itemtype        => itemtype,
7466                                         itemkey         => itemkey,
7467                                         aname           => 'REQUEST_STATUS_OLD');
7468 
7469 	    l_to_status   := WF_ENGINE.GetItemAttrText(
7470                                         itemtype        => itemtype,
7471                                         itemkey         => itemkey,
7472                                         aname           => 'REQUEST_STATUS');
7473      END IF;
7474 
7475             -- Get the message Name
7476   -- Set the Notification Details Required to send the email/wf html notification.
7477 
7478      IF l_contact_id_list IS NOT NULL THEN
7479 	    l_count := 1;
7480 
7481         pull_from_list(itemlist => l_contact_email_list,
7482                        element  => l_element);
7483 
7484         pull_from_list(itemlist => l_contact_id_list,
7485                        element  => l_element1);
7486 
7487         pull_from_list(itemlist => l_adhoc_user_list,
7488                        element  => l_adhoc_user);
7489 
7490         pull_from_list(itemlist => l_contact_type_list,
7491                        element  => l_contact_type);
7492 
7493         pull_from_list(itemlist => l_language_list,
7494                        element  => l_language);
7495 
7496         pull_from_list(itemlist => l_notification_pref_list,
7497                        element  => l_notification_pref);
7498 
7499         WF_ENGINE.SetItemAttrText
7500                       ( itemtype  => itemtype,
7501                         itemkey   => itemkey,
7502                         AName  => 'ADHOC_USER_LIST',
7503                         AValue => l_adhoc_user_list);
7504 
7505         WF_ENGINE.SetItemAttrText
7506                       ( itemtype  => itemtype,
7507                         itemkey   => itemkey,
7508                         AName  => 'CONTACT_ID_LIST',
7509                         AValue => l_contact_id_list);
7510 
7511         WF_ENGINE.SetItemAttrText
7512                       ( itemtype  => itemtype,
7513                         itemkey   => itemkey,
7514                         AName  => 'CONTACT_EMAIL_LIST',
7515                         AValue => l_contact_email_list);
7516 
7517         WF_ENGINE.SetItemAttrText
7518                       ( itemtype  => itemtype,
7519                         itemkey   => itemkey,
7520                         AName  => 'CONTACT_TYPE_LIST',
7521                         AValue => l_contact_type_list);
7522 
7523         WF_ENGINE.SetItemAttrText
7524                       ( itemtype  => itemtype,
7525                         itemkey   => itemkey,
7526                         AName  => 'NOTIFICATION_PREFERENCE_LIST',
7527                         AValue => l_notification_pref_list);
7528 
7529         WF_ENGINE.SetItemAttrText
7530                       ( itemtype  => itemtype,
7531                         itemkey   => itemkey,
7532                         AName  => 'LANGUAGE_LIST',
7533                         AValue => l_language_list);
7534 
7535       END IF;
7536 
7537       IF((FND_LOG.LEVEL_EXCEPTION >= FND_LOG.G_CURRENT_RUNTIME_LEVEL)) THEN
7538         IF (FND_LOG.TEST(FND_LOG.LEVEL_PROCEDURE, 'cs.plsql.CS_WF_AUTO_NTFY_UPDATE_PKG.PREPARE_HTML_NOTIFICATION')) THEN
7539 
7540           dbg_msg := ('Processing HTML Notification following set of attributes');
7541           IF( FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL ) THEN
7542               FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE, 'cs.plsql.CS_WF_AUTO_NTFY_UPDATE_PKG.PREPARE_HTML_NOTIFICATION', dbg_msg);
7543           END IF;
7544 
7545           dbg_msg := ('l_element : '||l_element);
7546           IF( FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL ) THEN
7547               FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE, 'cs.plsql.CS_WF_AUTO_NTFY_UPDATE_PKG.PREPARE_HTML_NOTIFICATION', dbg_msg);
7548           END IF;
7549 
7550           dbg_msg := ('l_element1 : '||l_element1);
7551           IF( FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL ) THEN
7552               FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE, 'cs.plsql.CS_WF_AUTO_NTFY_UPDATE_PKG.PREPARE_HTML_NOTIFICATION', dbg_msg);
7553           END IF;
7554 
7555           dbg_msg := ('l_adhoc_user : '||l_adhoc_user);
7556           IF( FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL ) THEN
7557               FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE, 'cs.plsql.CS_WF_AUTO_NTFY_UPDATE_PKG.PREPARE_HTML_NOTIFICATION', dbg_msg);
7558           END IF;
7559 
7560           dbg_msg := ('l_contact_type : '||l_contact_type);
7561           IF( FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL ) THEN
7562               FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE, 'cs.plsql.CS_WF_AUTO_NTFY_UPDATE_PKG.PREPARE_HTML_NOTIFICATION', dbg_msg);
7563           END IF;
7564 
7565           dbg_msg := ('l_language : '||l_language);
7566           IF( FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL ) THEN
7567               FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE, 'cs.plsql.CS_WF_AUTO_NTFY_UPDATE_PKG.PREPARE_HTML_NOTIFICATION', dbg_msg);
7568           END IF;
7569 
7570           dbg_msg := ('l_notification_pref : '||l_notification_pref);
7571           IF( FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL ) THEN
7572               FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE, 'cs.plsql.CS_WF_AUTO_NTFY_UPDATE_PKG.PREPARE_HTML_NOTIFICATION', dbg_msg);
7573           END IF;
7574 
7575         END IF;
7576       END IF;
7577 
7578       -- Set the recipient role for the notification
7579 
7580       WF_ENGINE.SetItemAttrText
7581                 ( itemtype        => itemtype,
7582                   itemkey         => itemkey,
7583                   aname           => 'NTFY_RECIPIENT',
7584                   avalue          => l_adhoc_user);
7585 
7586 
7587       l_contact_email    := l_element;
7588       l_contact_id := to_number(l_element1);
7589 
7590       -- Set the notification details as per the IBU templates.
7591 
7592       -- Call IBU API wrapper
7593 
7594       IF((FND_LOG.LEVEL_EXCEPTION >= FND_LOG.G_CURRENT_RUNTIME_LEVEL)) THEN
7595         IF (FND_LOG.TEST(FND_LOG.LEVEL_PROCEDURE, 'cs.plsql.CS_WF_AUTO_NTFY_UPDATE_PKG.PREPARE_HTML_NOTIFICATION')) THEN
7596 
7597           dbg_msg := ('Calling CS_WF_AUTO_NTFY_UPDATE_PKG.Set_HTML_Notification_Details Procedure');
7598           IF( FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL ) THEN
7599               FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE, 'cs.plsql.CS_WF_AUTO_NTFY_UPDATE_PKG.PREPARE_HTML_NOTIFICATION', dbg_msg);
7600           END IF;
7601 
7602         END IF;
7603       END IF;
7604 
7605       Set_HTML_Notification_Details
7606               ( p_itemtype         => itemtype,
7607                 p_itemkey          => itemkey,
7608                 p_actid            => actid,
7609                 p_funmode          => funmode,
7610                 p_contact_type     => l_contact_type,
7611                 p_contact_id       => to_number(l_contact_id),
7612                 p_request_id       => to_number(l_request_id),
7613 		p_request_number   => l_request_number,
7614 		p_sender           => l_sender,
7615                 p_email_preference => l_notification_pref,
7616                 p_user_language    => l_language,
7617 		p_recipient        => l_adhoc_user,
7618 		p_message_name     => l_message_name,
7619  	 	p_from_status      => l_from_status,
7620  		p_to_status        => l_to_status) ;
7621 
7622 
7623       IF((FND_LOG.LEVEL_EXCEPTION >= FND_LOG.G_CURRENT_RUNTIME_LEVEL)) THEN
7624         IF (FND_LOG.TEST(FND_LOG.LEVEL_PROCEDURE, 'cs.plsql.CS_WF_AUTO_NTFY_UPDATE_PKG.PREPARE_HTML_NOTIFICATION')) THEN
7625 
7626           dbg_msg := ('After Calling CS_WF_AUTO_NTFY_UPDATE_PKG.Set_HTML_Notification_Details Procedure');
7627           IF( FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL ) THEN
7628               FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE, 'cs.plsql.CS_WF_AUTO_NTFY_UPDATE_PKG.PREPARE_HTML_NOTIFICATION', dbg_msg);
7629           END IF;
7630 
7631         END IF;
7632       END IF;
7633 
7634       result := 'COMPLETE';
7635 
7636   ELSIF (funmode = 'CANCEL') THEN
7637       result := 'COMPLETE';
7638   END IF;
7639 
7640   IF((FND_LOG.LEVEL_EXCEPTION >= FND_LOG.G_CURRENT_RUNTIME_LEVEL)) THEN
7641     IF (FND_LOG.TEST(FND_LOG.LEVEL_PROCEDURE, 'cs.plsql.CS_WF_AUTO_NTFY_UPDATE_PKG.PREPARE_HTML_NOTIFICATION')) THEN
7642 
7643       dbg_msg := ('Prepare_HTML_Notification Proc Result : '||result);
7644       IF( FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL ) THEN
7645         FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE, 'cs.plsql.CS_WF_AUTO_NTFY_UPDATE_PKG.PREPARE_HTML_NOTIFICATION', dbg_msg);
7646       END IF;
7647 
7648     END IF;
7649   END IF;
7650 
7651   EXCEPTION
7652        WHEN OTHERS THEN
7653             WF_CORE.Context('CS_WF_AUTO_NTFY_UPDATE_PKG', 'Send_HTML_Notification',
7654 		    itemtype, itemkey, actid, funmode);
7655             RAISE;
7656 
7657   END Prepare_HTML_Notification;
7658 
7659 PROCEDURE Set_HTML_Notification_Details
7660            ( p_itemtype         IN        VARCHAR2,
7661              p_itemkey          IN        VARCHAR2,
7662              p_actid            IN        NUMBER,
7663              p_funmode          IN        VARCHAR2,
7664              p_contact_type     IN        VARCHAR2,
7665              p_contact_id       IN        NUMBER,
7666              p_request_id       IN        NUMBER,
7667 	     p_request_number   IN        VARCHAR2,
7668  	     p_sender           IN        VARCHAR2,
7669              p_email_preference IN        VARCHAR2,
7670              p_user_language    IN        VARCHAR2,
7671              p_recipient        IN        VARCHAR2,
7672 	     p_message_name     IN        VARCHAR2,
7673 	     p_from_status      IN        VARCHAR2,
7674 	     p_to_status        IN        VARCHAR2) IS
7675 
7676  l_count          NUMBER :=0 ;
7677  l_count1         NUMBER :=0 ;
7678  l_ticket_number  VARCHAR2(2000);
7679  l_ticket_valid   BOOLEAN:= FALSE;
7680  l_request        UTL_HTTP.REQ;
7681  l_response       UTL_HTTP.RESP;
7682  l_out_value      VARCHAR2(32000);
7683  l_ibu_url        VARCHAR2(2000);
7684  l_host           VARCHAR2(2000);
7685  l_request_number VARCHAR2(240);
7686  l_message_id     NUMBER;
7687  l_mail_pref      VARCHAR2(240);
7688 
7689   BEGIN
7690 
7691       IF((FND_LOG.LEVEL_EXCEPTION >= FND_LOG.G_CURRENT_RUNTIME_LEVEL)) THEN
7692         IF (FND_LOG.TEST(FND_LOG.LEVEL_PROCEDURE, 'cs.plsql.CS_WF_AUTO_NTFY_UPDATE_PKG.SET_HTML_NOTIFICATION_DETAILS')) THEN
7693 
7694           dbg_msg := ('In CS_WF_AUTO_NTFY_UPDATE_PKG.Set_HTML_Notification_Details Procedure');
7695           IF( FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL ) THEN
7696               FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE, 'cs.plsql.CS_WF_AUTO_NTFY_UPDATE_PKG.SET_HTML_NOTIFICATION_DETAILS', dbg_msg);
7697           END IF;
7698 
7699         END IF;
7700       END IF;
7701 
7702    -- Get the ticket number
7703 
7704       l_ticket_number := FND_HTTP_TICKET.GET_SERVICE_TICKET_STRING('CS_IBU_EMAIL');
7705 
7706    -- Set the mail preference
7707 
7708       IF (p_email_preference = 'MAILHTML' OR p_email_preference = 'MAILHTML2') THEN
7709          l_mail_pref := 'MAILHTML';
7710       ELSE
7711          l_mail_pref := 'MAILTEXT';
7712       END IF ;
7713 
7714    -- set the ibu url
7715 
7716 	  l_ibu_url := '/OA_HTML/ibuEmailContentProvider.jsp?ibuSRNum='||p_request_number||l_amp||'ibuSRID='
7717                        || p_request_id ||l_amp||'ibuTicketNum=' || l_ticket_number || l_amp||'ibuEmailIBUCONTENTPref='
7718                        || l_mail_pref || l_amp||'IBUContactID=' || p_contact_id || l_amp||'IBUContactType='
7719                        || p_contact_type || l_amp||'IBULanguage=' || p_user_language ;
7720 
7721    -- Get the host
7722 
7723       FND_PROFILE.GET('APPS_FRAMEWORK_AGENT',l_host);
7724 
7725       IF((FND_LOG.LEVEL_EXCEPTION >= FND_LOG.G_CURRENT_RUNTIME_LEVEL)) THEN
7726         IF (FND_LOG.TEST(FND_LOG.LEVEL_PROCEDURE, 'cs.plsql.CS_WF_AUTO_NTFY_UPDATE_PKG.SET_HTML_NOTIFICATION_DETAILS')) THEN
7727 
7728           dbg_msg := ('l_mail_pref : '||l_mail_pref);
7729           IF( FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL ) THEN
7730               FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE, 'cs.plsql.CS_WF_AUTO_NTFY_UPDATE_PKG.SET_HTML_NOTIFICATION_DETAILS', dbg_msg);
7731           END IF;
7732 
7733           dbg_msg := ('IBU URL : '||l_host||l_ibu_url);
7734           IF( FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL ) THEN
7735               FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE, 'cs.plsql.CS_WF_AUTO_NTFY_UPDATE_PKG.SET_HTML_NOTIFICATION_DETAILS', dbg_msg);
7736           END IF;
7737 
7738         END IF;
7739       END IF;
7740 
7741 	 -- bug 5661159
7742 	 IF lower(substr(nvl(l_host, ''), 1, 5)) = 'https' THEN
7743          UTL_HTTP.SET_WALLET('file:' || FND_PROFILE.Value('FND_DB_WALLET_DIR'));
7744 	 END IF;
7745 	 -- bug 5661159_eof
7746 
7747       l_request := UTL_HTTP.BEGIN_REQUEST(l_host||l_ibu_url);
7748 
7749       l_response := UTL_HTTP.Get_Response(l_request);
7750 
7751       LOOP
7752           l_count := l_count + 1 ;
7753           UTL_HTTP.READ_LINE(l_response, l_out_value,TRUE);
7754 
7755           IF((FND_LOG.LEVEL_EXCEPTION >= FND_LOG.G_CURRENT_RUNTIME_LEVEL)) THEN
7756             IF (FND_LOG.TEST(FND_LOG.LEVEL_PROCEDURE, 'cs.plsql.CS_WF_AUTO_NTFY_UPDATE_PKG.SET_HTML_NOTIFICATION_DETAILS')) THEN
7757               dbg_msg := ('l_count : '||l_count||'   l_count1'||l_count1);
7758               IF( FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL ) THEN
7759                   FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE, 'cs.plsql.CS_WF_AUTO_NTFY_UPDATE_PKG.SET_HTML_NOTIFICATION_DETAILS', dbg_msg);
7760               END IF;
7761 
7762               dbg_msg := ('Response Line : '||substr(l_out_value,1,4000));
7763               IF( FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL ) THEN
7764                   FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE, 'cs.plsql.CS_WF_AUTO_NTFY_UPDATE_PKG.SET_HTML_NOTIFICATION_DETAILS', dbg_msg);
7765               END IF;
7766 
7767             END IF;
7768           END IF;
7769 
7770           IF l_count1 > 0 THEN
7771              l_count1 := l_count1 + 1;
7772           END IF ;
7773 
7774           IF l_out_value = 'EMAILCONTENTSTART' THEN
7775              l_count1 := 1;
7776           END IF ;
7777 
7778           IF ((length(l_out_value) >0 ) AND(l_mail_pref = 'MAILTEXT')) THEN
7779              l_out_value := REPLACE(l_out_value,'\n',l_new_line);
7780           END IF;
7781 
7782           IF l_count1 = 2 THEN
7783 
7784               WF_ENGINE.SetItemAttrText(
7785                                  itemtype  => p_itemtype,
7786                                  itemkey   => p_itemkey,
7787                                  aname       => 'STYLESHEET',
7788                                  avalue      => 'plsql:IBU_SUBS_DOC_PKG.set_msg_body_token/'||l_out_value );
7789 
7790 
7791           ELSIF l_count1 = 3 then
7792 
7793               WF_ENGINE.SetItemAttrText(
7794                                  itemtype  => p_itemtype,
7795                                  itemkey   => p_itemkey,
7796                                  aname       => 'BRANDINGINFO',
7797                                  avalue      => 'plsql:IBU_SUBS_DOC_PKG.set_msg_body_token/'||l_out_value );
7798 
7799           ELSIF l_count1 = 4 then
7800 
7801                       WF_ENGINE.SetItemAttrText(
7802                                  itemtype  => p_itemtype,
7803                                  itemkey   => p_itemkey,
7804                                  aname     => 'IBU_CONTENT1',
7805                                  avalue    => 'plsql:IBU_SUBS_DOC_PKG.set_msg_body_token/'||l_out_value );
7806 
7807           ELSIF l_count1 = 5 then
7808 
7809                       WF_ENGINE.SetItemAttrText(
7810                                  itemtype  => p_itemtype,
7811                                  itemkey   => p_itemkey,
7812                                  aname     => 'IBU_CONTENT2',
7813                                  avalue    => 'plsql:IBU_SUBS_DOC_PKG.set_msg_body_token/'||l_out_value );
7814 
7815 
7816           ELSIF l_count1 = 6 then
7817 
7818                       WF_ENGINE.SetItemAttrText(
7819                                  itemtype  => p_itemtype,
7820                                  itemkey   => p_itemkey,
7821                                  aname     => 'IBU_CONTENT3',
7822                                  avalue    => 'plsql:IBU_SUBS_DOC_PKG.set_msg_body_token/'||l_out_value );
7823 
7824 
7825           ELSIF l_count1 = 7 then
7826 
7827                       WF_ENGINE.SetItemAttrText(
7828                                  itemtype  => p_itemtype,
7829                                  itemkey   => p_itemkey,
7830                                  aname     => 'IBU_CONTENT4',
7831                                  avalue    => 'plsql:IBU_SUBS_DOC_PKG.set_msg_body_token/'||l_out_value );
7832 
7833 
7834           ELSIF l_count1 = 8 then
7835 
7836                       WF_ENGINE.SetItemAttrText(
7837                                  itemtype  => p_itemtype,
7838                                  itemkey   => p_itemkey,
7839                                  aname     => 'IBU_CONTENT5',
7840                                  avalue    => 'plsql:IBU_SUBS_DOC_PKG.set_msg_body_token/'||l_out_value );
7841 
7842 
7843 
7844           ELSIF l_count1 = 9 then
7845 
7846                       WF_ENGINE.SetItemAttrText(
7847                                  itemtype  => p_itemtype,
7848                                  itemkey   => p_itemkey,
7849                                  aname     => 'IBU_CONTENT6',
7850                                  avalue    => 'plsql:IBU_SUBS_DOC_PKG.set_msg_body_token/'||l_out_value );
7851 
7852 
7853 
7854           ELSIF l_count1 = 10 then
7855 
7856                       WF_ENGINE.SetItemAttrText(
7857                                  itemtype  => p_itemtype,
7858                                  itemkey   => p_itemkey,
7859                                  aname     => 'IBU_CONTENT7',
7860                                  avalue    => 'plsql:IBU_SUBS_DOC_PKG.set_msg_body_token/'||l_out_value );
7861 
7862 
7863 
7864           ELSIF l_count1 = 11 then
7865 
7866                       WF_ENGINE.SetItemAttrText(
7867                                  itemtype  => p_itemtype,
7868                                  itemkey   => p_itemkey,
7869                                  aname     => 'IBU_CONTENT8',
7870                                  avalue    => 'plsql:IBU_SUBS_DOC_PKG.set_msg_body_token/'||l_out_value );
7871 
7872 
7873           ELSIF l_count1 = 12 then
7874 
7875                       WF_ENGINE.SetItemAttrText(
7876                                  itemtype  => p_itemtype,
7877                                  itemkey   => p_itemkey,
7878                                  aname     => 'IBU_CONTENT9',
7879                                  avalue    => 'plsql:IBU_SUBS_DOC_PKG.set_msg_body_token/'||l_out_value );
7880 
7881 
7882           ELSIF l_count1 = 13 then
7883 
7884                       WF_ENGINE.SetItemAttrText(
7885                                  itemtype  => p_itemtype,
7886                                  itemkey   => p_itemkey,
7887                                  aname     => 'IBU_CONTENT10',
7888                                  avalue    => 'plsql:IBU_SUBS_DOC_PKG.set_msg_body_token/'||l_out_value );
7889 
7890 
7891           ELSIF l_count1 = 14 then
7892 
7893                       WF_ENGINE.SetItemAttrText(
7894                                  itemtype  => p_itemtype,
7895                                  itemkey   => p_itemkey,
7896                                  aname     => 'IBU_CONTENT11',
7897                                  avalue    => 'plsql:IBU_SUBS_DOC_PKG.set_msg_body_token/'||l_out_value );
7898 
7899 
7900           ELSIF l_count1 = 15 then
7901 
7902                       WF_ENGINE.SetItemAttrText(
7903                                  itemtype  => p_itemtype,
7904                                  itemkey   => p_itemkey,
7905                                  aname     => 'IBU_CONTENT12',
7906                                  avalue    => 'plsql:IBU_SUBS_DOC_PKG.set_msg_body_token/'||l_out_value );
7907 
7908 
7909           ELSIF l_count1 = 16 then
7910 
7911                       WF_ENGINE.SetItemAttrText(
7912                                  itemtype  => p_itemtype,
7913                                  itemkey   => p_itemkey,
7914                                  aname     => 'IBU_CONTENT13',
7915                                  avalue    => 'plsql:IBU_SUBS_DOC_PKG.set_msg_body_token/'||l_out_value );
7916 
7917 
7918           ELSIF l_count1 = 17 then
7919 
7920                       WF_ENGINE.SetItemAttrText(
7921                                  itemtype  => p_itemtype,
7922                                  itemkey   => p_itemkey,
7923                                  aname     => 'IBU_CONTENT14',
7924                                  avalue    => 'plsql:IBU_SUBS_DOC_PKG.set_msg_body_token/'||l_out_value );
7925 
7926 
7927           ELSIF l_count1 = 18 then
7928 
7929                       WF_ENGINE.SetItemAttrText(
7930                                  itemtype  => p_itemtype,
7931                                  itemkey   => p_itemkey,
7932                                  aname     => 'IBU_CONTENT15',
7933                                  avalue    => 'plsql:IBU_SUBS_DOC_PKG.set_msg_body_token/'||l_out_value );
7934 
7935 
7936           ELSIF l_count1 = 19 then
7937 
7938                 NULL;
7939           ELSIF l_count1 = 20 then
7940 
7941              IF l_count1 IS NOT NULL THEN
7942                       WF_ENGINE.SetItemAttrText(
7943                                  itemtype  => p_itemtype,
7944                                  itemkey   => p_itemkey,
7945                                  aname       => 'URL_LINK',
7946                                  avalue    => 'plsql:IBU_SUBS_DOC_PKG.set_msg_body_token/'||l_out_value );
7947 
7948              END IF;
7949           END IF ;
7950 
7951           IF l_out_value = 'EMAILCONTENTEND' THEN
7952              EXIT;
7953           END IF;
7954 
7955 END LOOP;
7956 
7957       UTL_HTTP.End_Response(l_response);
7958 
7959   EXCEPTION
7960         WHEN UTL_HTTP.END_OF_BODY THEN
7961              UTL_HTTP.End_Response(l_response);
7962         WHEN OTHERS THEN
7963              WF_CORE.Context('CS_WF_AUTO_NTFY_UPDATE_PKG', 'Verify_Notify_Rules_Done',
7964 	 	      p_itemtype, p_itemkey, p_actid, p_funmode);
7965       RAISE;
7966   END Set_HTML_Notification_Details;
7967 
7968   PROCEDURE Are_All_HTML_Recips_Notified( itemtype   IN  VARCHAR2,
7969                              itemkey    IN  VARCHAR2,
7970                              actid      IN  NUMBER,
7971                              funmode    IN  VARCHAR2,
7972                              result     OUT NOCOPY VARCHAR2 ) IS
7973 
7974 
7975     l_contact_id_list  VARCHAR2(2000);
7976 
7977     BEGIN
7978 
7979       IF((FND_LOG.LEVEL_EXCEPTION >= FND_LOG.G_CURRENT_RUNTIME_LEVEL)) THEN
7980         IF (FND_LOG.TEST(FND_LOG.LEVEL_PROCEDURE, 'cs.plsql.CS_WF_AUTO_NTFY_UPDATE_PKG.ARE_ALL_HTML_RECIPS_NOTIFIED')) THEN
7981 
7982           dbg_msg := ('In CS_WF_AUTO_NTFY_UPDATE_PKG.Are_All_HTML_Recips_Notified Procedure');
7983           IF( FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL ) THEN
7984               FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE, 'cs.plsql.CS_WF_AUTO_NTFY_UPDATE_PKG.ARE_ALL_HTML_RECIPS_NOTIFIED', dbg_msg);
7985           END IF;
7986 
7987           dbg_msg := ('l_contact_id_list : '||l_contact_id_list);
7988           IF( FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL ) THEN
7989               FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE, 'cs.plsql.CS_WF_AUTO_NTFY_UPDATE_PKG.ARE_ALL_HTML_RECIPS_NOTIFIED', dbg_msg);
7990           END IF;
7991 
7992         END IF;
7993       END IF;
7994     IF (funmode = 'RUN') THEN
7995 
7996       l_contact_id_list := WF_ENGINE.GetItemAttrText(
7997                                   itemtype        => itemtype,
7998                                   itemkey         => itemkey,
7999                                   aname           => 'CONTACT_ID_LIST' );
8000 
8001       IF (l_contact_id_list IS NOT NULL ) THEN
8002         result := 'COMPLETE:N';
8003       ELSE
8004         result := 'COMPLETE:Y';
8005       END IF ;
8006 
8007 
8008     ELSIF (funmode = 'CANCEL') THEN
8009       result := 'COMPLETE';
8010     END IF;
8011 
8012     EXCEPTION
8013          WHEN OTHERS THEN
8014               WF_CORE.Context('CS_WF_AUTO_NTFY_UPDATE_PKG', 'Are_All_HTML_Recips_Notified',
8015 		      itemtype, itemkey, actid, funmode);
8016       RAISE;
8017 
8018   END Are_All_HTML_Recips_Notified;
8019 
8020 PROCEDURE Create_Role_List
8021               ( p_itemtype         IN        VARCHAR2,
8022                 p_itemkey          IN        VARCHAR2,
8023                 p_actid            IN        NUMBER,
8024                 p_funmode          IN        VARCHAR2,
8025                 p_action_code      IN        VARCHAR2,
8026                 p_role_group_type  IN        VARCHAR2,
8027                 p_role_group_code  IN        VARCHAR2) IS
8028 
8029    l_party_role_group_code        cs_party_role_group_maps.party_role_group_code%TYPE;
8030    l_part_role_code               cs_party_role_group_maps.party_role_code%TYPE;
8031    l_notify_party_role_list       varchar2(4000);
8032    l_notify_party_role_relsr_list varchar2(4000);
8033 
8034    CURSOR sel_party_role_csr IS
8035       SELECT party_role_code
8036         FROM cs_party_role_group_maps
8037        WHERE party_role_group_code = l_party_role_group_code
8038          AND trunc(sysdate) between trunc(nvl(start_date_active,sysdate))
8039                                 and trunc(nvl(end_date_active,sysdate));
8040 
8041    sel_party_role_rec sel_party_role_csr%ROWTYPE;
8042 
8043 BEGIN
8044 --x_return_status := FND_API.G_RET_STS_SUCCESS;
8045 
8046  IF (p_action_code = 'NOTIFY_ASSOCIATED_PARTIES') THEN
8047     IF (p_role_group_type = 'ROLEGROUP') THEN
8048        l_party_role_group_code := p_role_group_code;
8049 
8050        OPEN sel_party_role_csr;
8051             LOOP
8052               FETCH sel_party_role_csr into sel_party_role_rec;
8053                     EXIT WHEN sel_party_role_csr%NOTFOUND;
8054 
8055               -- Check for the length, not to exceed max WF text length of 4000.
8056 
8057               IF (nvl(LENGTH(l_notify_party_role_list), 0) +
8058                   nvl(LENGTH(sel_party_role_rec.party_role_code), 0) + 1) <= 4000 THEN
8059 
8060                  IF l_notify_party_role_list IS NULL THEN
8061                     l_notify_party_role_list := sel_party_role_rec.party_role_code;
8062                  ELSE
8063                     l_notify_party_role_list := l_notify_party_role_list || ' ' || sel_party_role_rec.party_role_code;
8064                  END IF;
8065               ELSE
8066                 /**********
8067                  If the query results exceeded the max allowable WF text length, then we
8068                  set an attribute to indicate that we need to re-query again to obtain
8069                  the remainder of the results which were not put in the initial list.
8070                  **********/
8071 
8072                  WF_ENGINE.SetItemAttrText
8073  		        (itemtype        => p_itemtype,
8074                          itemkey         => p_itemkey,
8075                          aname           => 'MORE_NTFY_ACTION_LIST',
8076                          avalue          => 'Y' );
8077 
8078               END IF;
8079             END LOOP;
8080        CLOSE sel_party_role_csr;
8081     ELSE  --(sel_action_rec.role_group_type = 'ROLEGROUP') THEN
8082         l_party_role_group_code := p_role_group_code;
8083 
8084         IF (nvl(LENGTH(l_notify_party_role_list), 0) +
8085             nvl(LENGTH(l_party_role_group_code), 0) + 1) <= 4000 THEN
8086 
8087            IF l_notify_party_role_list IS NULL THEN
8088               l_notify_party_role_list := l_party_role_group_code;
8089            ELSE
8090               l_notify_party_role_list := l_notify_party_role_list || ' ' || l_party_role_group_code;
8091            END IF;
8092 
8093         ELSE
8094           /**********
8095            If the query results exceeded the max allowable WF text length, then we
8096            set an attribute to indicate that we need to re-query again to obtain
8097            the remainder of the results which were not put in the initial list.
8098           **********/
8099 
8100            WF_ENGINE.SetItemAttrText
8101 		    (itemtype        => p_itemtype,
8102                      itemkey         => p_itemkey,
8103                      aname           => 'MORE_NTFY_ACTION_LIST',
8104                      avalue          => 'Y' );
8105 
8106         END IF;
8107     END IF;   --(sel_action_rec.role_group_type = 'GROUP') THEN
8108 
8109  ELSIF (p_action_code = 'NOTIFY_ALL_ASSOCIATED_PARTIES') THEN
8110        IF (p_role_group_type = 'ROLEGROUP') THEN
8111            l_party_role_group_code := p_role_group_code;
8112 
8113           OPEN sel_party_role_csr;
8114               LOOP
8115                 FETCH sel_party_role_csr into sel_party_role_rec;
8116                  EXIT WHEN sel_party_role_csr%NOTFOUND;
8117 
8118                 -- Check for the length, not to exceed max WF text length of 4000.
8119                 IF (nvl(LENGTH(l_notify_party_role_relsr_list), 0) +
8120                     nvl(LENGTH(sel_party_role_rec.party_role_code), 0) + 1) <= 4000 THEN
8121 
8122                     IF l_notify_party_role_relsr_list IS NULL THEN
8123                        l_notify_party_role_relsr_list := sel_party_role_rec.party_role_code;
8124                     ELSE
8125                        l_notify_party_role_relsr_list := l_notify_party_role_relsr_list || ' ' || sel_party_role_rec.party_role_code;
8126                     END IF;
8127 
8128                 ELSE
8129                   /**********
8130                    If the query results exceeded the max allowable WF text length, then we
8131                    set an attribute to indicate that we need to re-query again to obtain
8132 	               the remainder of the results which were not put in the initial list.
8133                    **********/
8134 
8135                    WF_ENGINE.SetItemAttrText
8136                             (itemtype        => p_itemtype,
8137                              itemkey         => p_itemkey,
8138                              aname           => 'MORE_NTFY_ACTION_LIST',
8139                              avalue          => 'Y' );
8140 
8141                 END IF;
8142 
8143               END LOOP;
8144           CLOSE sel_party_role_csr;
8145        ELSE  --(sel_action_rec.role_group_type = 'ROLEGROUP') THEN
8146             l_party_role_group_code := p_role_group_code;
8147 
8148             IF (nvl(LENGTH(l_notify_party_role_relsr_list), 0) +
8149                 nvl(LENGTH(l_party_role_group_code), 0) + 1) <= 4000 THEN
8150 
8151                IF l_notify_party_role_relsr_list IS NULL THEN
8152                   l_notify_party_role_relsr_list := l_party_role_group_code;
8153                ELSE
8154                   l_notify_party_role_relsr_list := l_notify_party_role_relsr_list || ' ' || l_party_role_group_code;
8155                END IF;
8156 
8157             ELSE
8158                /**********
8159                 If the query results exceeded the max allowable WF text length, then we
8160                 set an attribute to indicate that we need to re-query again to obtain
8161   	            the remainder of the results which were not put in the initial list.
8162                 **********/
8163 
8164                 WF_ENGINE.SetItemAttrText
8165 		         (itemtype        => p_itemtype,
8166                           itemkey         => p_itemkey,
8167                           aname           => 'MORE_NTFY_ACTION_LIST',
8168                           avalue          => 'Y' );
8169 
8170             END IF;
8171        END IF;   --(sel_action_rec.role_group_type = 'ROLEGROUP') THEN
8172 
8173   END IF;
8174 
8175   WF_ENGINE.SetItemAttrText
8176            (itemtype	=> p_itemtype,
8177             itemkey	=> p_itemkey,
8178 	    aname	=> 'NOTIFY_PARTY_ROLE_LIST',
8179   	    avalue	=> l_notify_party_role_list );
8180 
8181   WF_ENGINE.SetItemAttrText
8182            (itemtype	=> p_itemtype,
8183   	    itemkey	=> p_itemkey,
8184   	    aname	=> 'NOTIFY_RELSR_PARTY_ROLE_LIST',
8185   	    avalue	=> l_notify_party_role_relsr_list );
8186 
8187 EXCEPTION
8188          WHEN OTHERS THEN
8189               WF_CORE.Context('CS_WF_AUTO_NTFY_UPDATE_PKG', 'Create_Role_List',
8190 		      p_itemtype, p_itemkey, p_actid, p_funmode);
8191       RAISE;
8192 END Create_Role_List;
8193 
8194 END CS_WF_AUTO_NTFY_UPDATE_PKG;