Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- trigger PhysicianVisit_AU_UpdateFutureVisits on Physician_Visit__c (after Update)
- {
- if(clsPhysicianVisitUpdate.isExecute == false && clsPhysicianVisitUpdate.isInsert == false)
- {
- clsPhysicianVisitUpdate.isExecute = true;
- if(!clsPhysicianVisitUpdate.blnIsTriggerFiredAlreadyForJntFldWrk)
- {
- if(Trigger.isUpdate)
- {
- try
- {
- clsUpdateFuturePhysicianVisits oclsUpdateFuturePhysicianVisits = new clsUpdateFuturePhysicianVisits();
- oclsUpdateFuturePhysicianVisits.updateFuturePhysicianVisits(trigger.new,'update');
- //List of affected physician visits.
- List<Physician_Visit__c> list_PhysicianVisit = [select Id, OwnerId, Activity_Date__c, recordType.Name, Status__c, Call_Objective__c, Call_Objective_Achieved__c from Physician_Visit__c where id in :Trigger.New];
- //Proceed only if the status of any of the record is completed.
- Boolean blnContinue = False;
- for (Physician_Visit__c pv:list_PhysicianVisit){
- System.debug('TESTTTTTINNNNNGGGGG>>>>'+pv.Status__c);
- if (pv.Status__c=='Completed'){
- blnContinue = True;
- break;
- }
- }
- if (!blnContinue) return;
- //List of Call summaries to insert.update.
- List<Call_Summary__c> list_CallSummary = new List<Call_Summary__c>();
- //List of effectiveness summaries to insert.update.
- List<Effectiveness_Summary__c> list_EffectivenessSummary = new List<Effectiveness_Summary__c>();
- //Get the user values.
- User objUser = [Select Id, Name, APAC_Country__c, APAC_Region__c, APAC_Area__c, Profile.Name From User Where Id=:Userinfo.getUserId()];
- //Get the user profile.
- //Profile objProfile = [Select Name from Profile where id=:Userinfo.getProfileId()];
- String strProfile = objUser.Profile.Name;
- if (strProfile.contains('Service')){
- strProfile = 'Service';
- }else{
- strProfile = 'Sales';
- }
- //For-loop: loops through each affected store visit record.
- for(Physician_Visit__c p : list_PhysicianVisit){
- /*
- * To update the call summary and effectiveness summary objects.
- *
- */
- if (p.Status__c=='Completed'){
- //Map of the unique value and the no. of actual calls for the day.
- Map<String, Integer> map_Unique_ActualCalls = new Map<String, Integer>();
- //Map of the unique value and the no. of planned calls for the day.
- Map<String, Integer> map_Unique_PlannedCalls = new Map<String, Integer>();
- //Map of the unique value and the no. of targetted calls for the day.
- Map<String, Integer> map_Unique_TragettedCalls = new Map<String, Integer>();
- //Map of the unique value and the no. of actual calls for the day.
- Map<String, Integer> map_UniqueEffectiveness_ActualCalls = new Map<String, Integer>();
- //Map of the unique value and objective for the day.
- Map<String, Integer> map_UniqueEffectiveness_Obj = new Map<String, Integer>();
- //List of Physician visits by the rep for the visit date.
- List<Physician_Visit__c> list_PhysicianVisitsForDay = [Select Segmentation__c, Planned_Call__c, Status__c, Call_Objective__c, Call_Objective_Achieved__c from Physician_Visit__c where Activity_Date__c = :p.Activity_Date__c and OwnerId = :p.OwnerId];
- //for-loop: to calculate the no. of actual, planned and targetted calls.
- for (Physician_Visit__c pv : list_PhysicianVisitsForDay){
- //Unique value to represent a call summary record.
- String strUnique = String.valueOf(p.OwnerId)+String.valueOf(p.Activity_Date__c);
- //Unique value to represent a effectiveness summary record.
- String strUniqueEffectiveness = String.valueOf(pv.Segmentation__c)+String.valueOf(p.OwnerId)+String.valueOf(p.Activity_Date__c);
- //If the status is completed.
- if (pv.Status__c=='Completed'){
- Integer intActualCalls = 1;
- if (map_Unique_ActualCalls.containsKey(strUnique)){
- intActualCalls = map_Unique_ActualCalls.get(strUnique) + 1;
- }
- System.debug('unique='+strUnique+',intActualCalls'+intActualCalls);
- map_Unique_ActualCalls.put(strUnique, intActualCalls);
- //If the visit is a planned one.
- if (pv.Planned_Call__c == True){
- Integer intTargettedCalls = 1;
- if (map_Unique_TragettedCalls.containsKey(strUnique)){
- intTargettedCalls = map_Unique_TragettedCalls.get(strUnique) + 1;
- }
- System.debug('unique='+strUnique+',intTargettedCalls'+intTargettedCalls);
- map_Unique_TragettedCalls.put(strUnique, intTargettedCalls);
- }
- if (map_UniqueEffectiveness_ActualCalls.containsKey(strUniqueEffectiveness)){
- intActualCalls = map_UniqueEffectiveness_ActualCalls.get(strUniqueEffectiveness) + 1;
- }
- System.debug('unique='+strUniqueEffectiveness+',intActualCalls'+intActualCalls);
- map_UniqueEffectiveness_ActualCalls.put(strUniqueEffectiveness, intActualCalls);
- //If objective is achieved.
- if (pv.Call_Objective_Achieved__c==TRUE){
- Integer intObj = 1;
- if (map_UniqueEffectiveness_Obj.containsKey(strUniqueEffectiveness)){
- intObj = map_UniqueEffectiveness_Obj.get(strUniqueEffectiveness) + 1;
- }
- System.debug('unique='+strUniqueEffectiveness+',intObj'+intObj);
- map_UniqueEffectiveness_Obj.put(strUniqueEffectiveness, intObj);
- }
- }
- //If the visit is a planned one.
- if (pv.Planned_Call__c == True) {
- Integer intPlannedCalls = 1;
- if (map_Unique_PlannedCalls.containsKey(strUnique)){
- intPlannedCalls = map_Unique_PlannedCalls.get(strUnique) + 1;
- }
- System.debug('unique='+strUnique+',intPlannedCalls'+intPlannedCalls);
- map_Unique_PlannedCalls.put(strUnique, intPlannedCalls);
- }
- }
- //Business_Day_or_not__c is '0' on Weekends and '1' on Weekdays
- Datetime dtmVisitDate = Datetime.newInstance(p.Activity_Date__c, Time.newInstance(0,0,0,0));
- String strWeekday = dtmVisitDate.format('E');
- Boolean blnBusinessDay = TRUE;
- if(strWeekday == 'Sat' || strWeekday == 'Sun'){
- blnBusinessDay = FALSE;
- }
- //Set of the unique values for call summary.
- Set<String> list_Unique = new Set<String>();
- //Set of the unique values for effectiveness summary.
- Set<String> list_UniqueEffectiveness = new Set<String>();
- //for-loop: to create the call summary & effectiveness summary records.
- for (Physician_Visit__c pv : list_PhysicianVisitsForDay){
- //Unique value to represent a call summary record.
- String strUnique = String.valueOf(p.OwnerId)+String.valueOf(p.Activity_Date__c);
- //Unique value to represent a effectiveness summary record.
- String strUniqueEffectiveness = String.valueOf(pv.Segmentation__c)+String.valueOf(p.OwnerId)+String.valueOf(p.Activity_Date__c);
- //To create the call summary record.
- if (!list_Unique.contains(strUnique)) {
- Call_Summary__c objCallSummary = new Call_Summary__c();
- objCallSummary.Country__c = objUser.APAC_Country__c;
- objCallSummary.RegionINDSU__c = objUser.APAC_Region__c;
- objCallSummary.Area__c = objUser.APAC_Area__c;
- objCallSummary.Date__c = p.Activity_Date__c;
- objCallSummary.Business_Day_or_not__c = blnBusinessDay;
- objCallSummary.Actual_Calls__c = (map_Unique_ActualCalls.containsKey(strUnique) ? map_Unique_ActualCalls.get(strUnique) : 0);
- objCallSummary.Planned_Calls__c = (map_Unique_PlannedCalls.containsKey(strUnique) ? map_Unique_PlannedCalls.get(strUnique) : 0);
- objCallSummary.Targeted_Calls__c = (map_Unique_TragettedCalls.containsKey(strUnique) ? map_Unique_TragettedCalls.get(strUnique) : 0);
- objCallSummary.Profile__c = strProfile;
- objCallSummary.OwnerId = p.OwnerId;
- objCallSummary.Unique__c = strUnique;
- list_CallSummary.add(objCallSummary);
- list_Unique.add(strUnique);
- System.debug('unique value='+strUnique+',actual='+objCallSummary.Actual_Calls__c+',planned='+objCallSummary.Planned_Calls__c+'targetted='+objCallSummary.Targeted_Calls__c);
- }
- //To create the effectiveness summary record.
- if (!list_UniqueEffectiveness.contains(strUniqueEffectiveness)) {
- Effectiveness_Summary__c objEffectSummary = new Effectiveness_Summary__c();
- objEffectSummary.ABC_Classification__c = pv.Segmentation__c;
- objEffectSummary.Country__c = objUser.APAC_Country__c;
- objEffectSummary.Region__c = objUser.APAC_Region__c;
- objEffectSummary.Area__c = objUser.APAC_Area__c;
- objEffectSummary.Date__c = p.Activity_Date__c;
- objEffectSummary.Actual_Calls__c = (map_UniqueEffectiveness_ActualCalls.containsKey(strUniqueEffectiveness) ? map_UniqueEffectiveness_ActualCalls.get(strUniqueEffectiveness) : 0);
- objEffectSummary.Objective_Met__c = (map_UniqueEffectiveness_Obj.containsKey(strUniqueEffectiveness) ? map_UniqueEffectiveness_Obj.get(strUniqueEffectiveness) : 0);
- objEffectSummary.Profile__c = strProfile;
- objEffectSummary.OwnerId = p.OwnerId;
- objEffectSummary.Unique__c = strUniqueEffectiveness;
- list_EffectivenessSummary.add(objEffectSummary);
- list_UniqueEffectiveness.add(strUniqueEffectiveness);
- //System.debug('unique value='+strUniqueEffectiveness+',actual='+objEffectSummary.Actual_Calls__c+',No_of_objective_1s_met__c='+objEffectSummary.No_of_objective_1s_met__c+'No_of_objective_2s_met__c='+objEffectSummary.No_of_objective_2s_met__c+'No_of_objective_3s_met__c='+objEffectSummary.No_of_objective_3s_met__c);
- }
- }
- }
- }
- //To update the call summary.
- if (list_CallSummary.Size()>0){
- Database.upsert(list_CallSummary,Call_Summary__c.Unique__c,false);
- }
- //To update the effectiveness summary.
- if (list_EffectivenessSummary.Size()>0){
- Database.upsert(list_EffectivenessSummary,Effectiveness_Summary__c.Unique__c,false);
- }
- }
- catch(Exception e)
- {
- Trigger.New[0].addError('Error occurred in Updating future Physician Visits - ' + e);
- }
- }
- }
- }
- }
- trigger PhysicianVisit_AU_UpdateFutureVisits on Physician_Visit__c (after Update)
- {
- /*
- *
- * This trigger future Physician Visit Records whenever an update happens on Physician Visit
- *
- * Author |Author-Email |Date |Comment
- * ------------------------------|------------------------------------------------- |------------|----------------------------------------------------
- * DeepaLakshmi.R |deepalakshmi.venkatesh@listertechnologies.com |30.04.2010 |First Draft
- *
- * DeepaLakshmi.R |deepalakshmi.venkatesh@listertechnologies.com |30.04.2010 |Second Draft to update call summary, effectiveness summary
- *
- */
- if(clsPhysicianVisitUpdate.isExecute == false && clsPhysicianVisitUpdate.isInsert == false)
- {
- clsPhysicianVisitUpdate.isExecute = true;
- if(!clsPhysicianVisitUpdate.blnIsTriggerFiredAlreadyForJntFldWrk)
- {
- if(Trigger.isUpdate)
- {
- try
- {
- clsUpdateFuturePhysicianVisits oclsUpdateFuturePhysicianVisits = new clsUpdateFuturePhysicianVisits();
- oclsUpdateFuturePhysicianVisits.updateFuturePhysicianVisits(trigger.new,'update');
- //List of affected physician visits.
- List<Physician_Visit__c> list_PhysicianVisit = [select Id, OwnerId, Activity_Date__c, recordType.Name, Status__c, Call_Objective__c, Call_Objective_Achieved__c from Physician_Visit__c where id in :Trigger.New];
- //Proceed only if the status of any of the record is completed.
- Boolean blnContinue = False;
- for (Physician_Visit__c pv:list_PhysicianVisit){
- System.debug('TESTTTTTINNNNNGGGGG>>>>'+pv.Status__c);
- if (pv.Status__c=='Completed'){
- blnContinue = True;
- break;
- }
- }
- if (!blnContinue) return;
- //List of Call summaries to insert.update.
- List<Call_Summary__c> list_CallSummary = new List<Call_Summary__c>();
- //List of effectiveness summaries to insert.update.
- List<Effectiveness_Summary__c> list_EffectivenessSummary = new List<Effectiveness_Summary__c>();
- //Get the user values.
- User objUser = [Select Id, Name, APAC_Country__c, APAC_Region__c, APAC_Area__c, Profile.Name From User Where Id=:Userinfo.getUserId()];
- //Get the user profile.
- //Profile objProfile = [Select Name from Profile where id=:Userinfo.getProfileId()];
- String strProfile = objUser.Profile.Name;
- if (strProfile.contains('Service')){
- strProfile = 'Service';
- }else{
- strProfile = 'Sales';
- }
- //For-loop: loops through each affected store visit record.
- for(Physician_Visit__c p : list_PhysicianVisit){
- /*
- * To update the call summary and effectiveness summary objects.
- *
- */
- if (p.Status__c=='Completed'){
- //Map of the unique value and the no. of actual calls for the day.
- Map<String, Integer> map_Unique_ActualCalls = new Map<String, Integer>();
- //Map of the unique value and the no. of planned calls for the day.
- Map<String, Integer> map_Unique_PlannedCalls = new Map<String, Integer>();
- //Map of the unique value and the no. of targetted calls for the day.
- Map<String, Integer> map_Unique_TragettedCalls = new Map<String, Integer>();
- //Map of the unique value and the no. of actual calls for the day.
- Map<String, Integer> map_UniqueEffectiveness_ActualCalls = new Map<String, Integer>();
- //Map of the unique value and objective for the day.
- Map<String, Integer> map_UniqueEffectiveness_Obj = new Map<String, Integer>();
- //List of Physician visits by the rep for the visit date.
- List<Physician_Visit__c> list_PhysicianVisitsForDay = [Select Segmentation__c, Planned_Call__c, Status__c, Call_Objective__c, Call_Objective_Achieved__c from Physician_Visit__c where Activity_Date__c = :p.Activity_Date__c and OwnerId = :p.OwnerId];
- //for-loop: to calculate the no. of actual, planned and targetted calls.
- for (Physician_Visit__c pv : list_PhysicianVisitsForDay){
- //Unique value to represent a call summary record.
- String strUnique = String.valueOf(p.OwnerId)+String.valueOf(p.Activity_Date__c);
- //Unique value to represent a effectiveness summary record.
- String strUniqueEffectiveness = String.valueOf(pv.Segmentation__c)+String.valueOf(p.OwnerId)+String.valueOf(p.Activity_Date__c);
- //If the status is completed.
- if (pv.Status__c=='Completed'){
- Integer intActualCalls = 1;
- if (map_Unique_ActualCalls.containsKey(strUnique)){
- intActualCalls = map_Unique_ActualCalls.get(strUnique) + 1;
- }
- System.debug('unique='+strUnique+',intActualCalls'+intActualCalls);
- map_Unique_ActualCalls.put(strUnique, intActualCalls);
- //If the visit is a planned one.
- if (pv.Planned_Call__c == True){
- Integer intTargettedCalls = 1;
- if (map_Unique_TragettedCalls.containsKey(strUnique)){
- intTargettedCalls = map_Unique_TragettedCalls.get(strUnique) + 1;
- }
- System.debug('unique='+strUnique+',intTargettedCalls'+intTargettedCalls);
- map_Unique_TragettedCalls.put(strUnique, intTargettedCalls);
- }
- if (map_UniqueEffectiveness_ActualCalls.containsKey(strUniqueEffectiveness)){
- intActualCalls = map_UniqueEffectiveness_ActualCalls.get(strUniqueEffectiveness) + 1;
- }
- System.debug('unique='+strUniqueEffectiveness+',intActualCalls'+intActualCalls);
- map_UniqueEffectiveness_ActualCalls.put(strUniqueEffectiveness, intActualCalls);
- //If objective is achieved.
- if (pv.Call_Objective_Achieved__c==TRUE){
- Integer intObj = 1;
- if (map_UniqueEffectiveness_Obj.containsKey(strUniqueEffectiveness)){
- intObj = map_UniqueEffectiveness_Obj.get(strUniqueEffectiveness) + 1;
- }
- System.debug('unique='+strUniqueEffectiveness+',intObj'+intObj);
- map_UniqueEffectiveness_Obj.put(strUniqueEffectiveness, intObj);
- }
- }
- //If the visit is a planned one.
- if (pv.Planned_Call__c == True) {
- Integer intPlannedCalls = 1;
- if (map_Unique_PlannedCalls.containsKey(strUnique)){
- intPlannedCalls = map_Unique_PlannedCalls.get(strUnique) + 1;
- }
- System.debug('unique='+strUnique+',intPlannedCalls'+intPlannedCalls);
- map_Unique_PlannedCalls.put(strUnique, intPlannedCalls);
- }
- }
- //Business_Day_or_not__c is '0' on Weekends and '1' on Weekdays
- Datetime dtmVisitDate = Datetime.newInstance(p.Activity_Date__c, Time.newInstance(0,0,0,0));
- String strWeekday = dtmVisitDate.format('E');
- Boolean blnBusinessDay = TRUE;
- if(strWeekday == 'Sat' || strWeekday == 'Sun'){
- blnBusinessDay = FALSE;
- }
- //Set of the unique values for call summary.
- Set<String> list_Unique = new Set<String>();
- //Set of the unique values for effectiveness summary.
- Set<String> list_UniqueEffectiveness = new Set<String>();
- //for-loop: to create the call summary & effectiveness summary records.
- for (Physician_Visit__c pv : list_PhysicianVisitsForDay){
- //Unique value to represent a call summary record.
- String strUnique = String.valueOf(p.OwnerId)+String.valueOf(p.Activity_Date__c);
- //Unique value to represent a effectiveness summary record.
- String strUniqueEffectiveness = String.valueOf(pv.Segmentation__c)+String.valueOf(p.OwnerId)+String.valueOf(p.Activity_Date__c);
- //To create the call summary record.
- if (!list_Unique.contains(strUnique)) {
- Call_Summary__c objCallSummary = new Call_Summary__c();
- objCallSummary.Country__c = objUser.APAC_Country__c;
- objCallSummary.RegionINDSU__c = objUser.APAC_Region__c;
- objCallSummary.Area__c = objUser.APAC_Area__c;
- objCallSummary.Date__c = p.Activity_Date__c;
- objCallSummary.Business_Day_or_not__c = blnBusinessDay;
- objCallSummary.Actual_Calls__c = (map_Unique_ActualCalls.containsKey(strUnique) ? map_Unique_ActualCalls.get(strUnique) : 0);
- objCallSummary.Planned_Calls__c = (map_Unique_PlannedCalls.containsKey(strUnique) ? map_Unique_PlannedCalls.get(strUnique) : 0);
- objCallSummary.Targeted_Calls__c = (map_Unique_TragettedCalls.containsKey(strUnique) ? map_Unique_TragettedCalls.get(strUnique) : 0);
- objCallSummary.Profile__c = strProfile;
- objCallSummary.OwnerId = p.OwnerId;
- objCallSummary.Unique__c = strUnique;
- list_CallSummary.add(objCallSummary);
- list_Unique.add(strUnique);
- System.debug('unique value='+strUnique+',actual='+objCallSummary.Actual_Calls__c+',planned='+objCallSummary.Planned_Calls__c+'targetted='+objCallSummary.Targeted_Calls__c);
- }
- //To create the effectiveness summary record.
- if (!list_UniqueEffectiveness.contains(strUniqueEffectiveness)) {
- Effectiveness_Summary__c objEffectSummary = new Effectiveness_Summary__c();
- objEffectSummary.ABC_Classification__c = pv.Segmentation__c;
- objEffectSummary.Country__c = objUser.APAC_Country__c;
- objEffectSummary.Region__c = objUser.APAC_Region__c;
- objEffectSummary.Area__c = objUser.APAC_Area__c;
- objEffectSummary.Date__c = p.Activity_Date__c;
- objEffectSummary.Actual_Calls__c = (map_UniqueEffectiveness_ActualCalls.containsKey(strUniqueEffectiveness) ? map_UniqueEffectiveness_ActualCalls.get(strUniqueEffectiveness) : 0);
- objEffectSummary.Objective_Met__c = (map_UniqueEffectiveness_Obj.containsKey(strUniqueEffectiveness) ? map_UniqueEffectiveness_Obj.get(strUniqueEffectiveness) : 0);
- objEffectSummary.Profile__c = strProfile;
- objEffectSummary.OwnerId = p.OwnerId;
- objEffectSummary.Unique__c = strUniqueEffectiveness;
- list_EffectivenessSummary.add(objEffectSummary);
- list_UniqueEffectiveness.add(strUniqueEffectiveness);
- //System.debug('unique value='+strUniqueEffectiveness+',actual='+objEffectSummary.Actual_Calls__c+',No_of_objective_1s_met__c='+objEffectSummary.No_of_objective_1s_met__c+'No_of_objective_2s_met__c='+objEffectSummary.No_of_objective_2s_met__c+'No_of_objective_3s_met__c='+objEffectSummary.No_of_objective_3s_met__c);
- }
- }
- }
- }
- //To update the call summary.
- if (list_CallSummary.Size()>0){
- Database.upsert(list_CallSummary,Call_Summary__c.Unique__c,false);
- }
- //To update the effectiveness summary.
- if (list_EffectivenessSummary.Size()>0){
- Database.upsert(list_EffectivenessSummary,Effectiveness_Summary__c.Unique__c,false);
- }
- }
- catch(Exception e)
- {
- Trigger.New[0].addError('Error occurred in Updating future Physician Visits - ' + e);
- }
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment