+ Start a Discussion
TheLearnerTheLearner 

Amending the change when check box is check field status has to be changed

Hi Experts,

i need to do some amendments in my trigger, when ever Defect_on_Hold__c checkbox  is checked, i need to change this Activity_Status__c field value from    "INTrR" to "HOLD". im not getting where and how exactly i need to amendments,Could anyone help please.


trigger tRIIO_Calc_Activity_Status_And_Fields on tRIIO_Activity__c (before update, before insert) {
    //To calculate Reinstatement Status
    if(!Test.isRunningTest())
    if(Trigger.isUpdate){
        Boolean isActivityWithoutOM  = false;
        Boolean isAnyOMWithoutFM = false;
        Boolean doesAnyFMHaveInterimDate = false;
        String Status = ''; 
        Integer countInterimDate = 0;
        for(tRIIO_Reinstatement_FM__c fm:[SELECT Id, Interim_Date__c FROM tRIIO_Reinstatement_FM__c WHERE Activity__c =: trigger.new[0].id]){
            if(fm.Interim_Date__c != null)
                countInterimDate++;
        }
        List<tRIIO_Reinstatement_OM__c> OM = [select id, name,FM__c from tRIIO_Reinstatement_OM__c where Activity__c =: trigger.new[0].id ];
        Boolean isNotWithOutOM = false;
        Boolean isAnyOMWOFM = false;
        Boolean doesNiFMHaveInterimDate = false;
        String NGA_Work_Status_Text = '';
        if (OM.size()==0) {
            isActivityWithoutOM = true;
        } else {   
            //Loop for all OMs and if any OM is without FM then set the flag
            for(tRIIO_Reinstatement_OM__c a : OM) {
                if (a.FM__c!=true) {
                    isAnyOMWithoutFM = true;
                }
           }
           if (countInterimDate > 0) {
               doesAnyFMHaveInterimDate = true;
           } 
        }
        //if there are no Original Measures for Notice set Status to 'Schld'
        if (isActivityWithoutOM == true) {
            Status = 'Schld';
        } else {
            //if any original measures is without a final measures set status to 'InPrg'
            if (IsAnyOMWithoutFM == true) {
                Status = 'InPrg';
            } else {
                //if any final measures have an interim date set status to 'InPrg' else set to 'IntReinst'
                if (DoesAnyFMHaveInterimDate == true) {
                    Status = 'IntrC';
                } else {
                    Status = 'PermC';
                }
            }   
         }
        trigger.new[0].Status__c = Status;
    }
    //To calculate Activity Status
    if(Trigger.isInsert || Trigger.isUpdate){
        String ActivityStatus = '';
        tRIIO_Activity__c [] ActivityNew;
        ActivityNew = trigger.new;
        for( tRIIO_Activity__c Nots : ActivityNew){
            if(Nots.Activity_Status__c != 'IntrR(S)'){
                Nots.Clk_Int_Perm__c = false;
            }
        }
        Profile p = [SELECT Id,Name FROM profile WHERE id=:Userinfo.getProfileId()];
        String currentProfile = p.Name;
        String readOnlyProfiles = 'NG Read-Only';//Add other read-only profiles here seperated by ,
        if (readOnlyProfiles.indexOf(currentProfile) > -1){
            ActivityNew[0].addError('You do not have sufficient privilages to add or modify records.');
        }
        for(tRIIO_Activity__c activity : ActivityNew){
            if ((activity.Activity_Status__c != 'IntrR(S)')&&(activity.Activity_Status__c != 'Cancelled') ) {
                if (activity.Abandoned__c == true){
                    if(activity.Hit_Int_Perm__c == true){
                        ActivityStatus = 'Awaiting Notice';
                    } else{
                        ActivityStatus = 'Abandoned';
                    }
              }
              else{
                  //If Works Close Date AND Registration Date exists AND Reinstatement Status = "Schld" then "Nil Reinstatement"
                  if ((activity.Works_Close__c != null) && (activity.Registration__c != null) && (activity.Status__c == 'Schld'))
                      ActivityStatus = 'Nil Reinstatement';
                  //If Works Close Date AND Registration Date exists AND Reinstatement Status = "PermC" then "PermR"
                  else if ((activity.Works_Close__c != null) && (activity.Registration__c != null) && (activity.Status__c == 'PermC'))
                      ActivityStatus = 'PermR';
                  //If Works Close Date exists AND Registration Date does not exist AND Reinstatement Status = "PermC" then "PermC"     
                  else if ((activity.Works_Close__c != null) && (activity.Registration__c == null) && (activity.Status__c == 'PermC'))
                      ActivityStatus = 'PermC';
                  //If Works Close Date AND Registration Date exists AND Reinstatement Status = "IntrC" then "IntrR" 
                  else if ((activity.Works_Close__c != null) && (activity.Registration__c != null) && (activity.Status__c == 'IntrC'))
                      ActivityStatus = 'IntrR';
                  //If Works Close Date exists AND Registration Date does not exist AND Reinstatement Status = "IntrC" then "IntrC"  
                  else if ((activity.Works_Close__c != null) && (activity.Registration__c == null) && (activity.Status__c == 'IntrC'))
                      ActivityStatus = 'IntrC';
                  //If Actual Start Date exists then "InPrg
                  else if ((activity.Actual_Start__c != null))
                      ActivityStatus = 'InPrg';
                  //If Works Start Date exists then "Notice Raised" 
                  else if ((activity.Works_End_Date__c != null))
                      ActivityStatus = 'Notice Raised';
                  else
                      ActivityStatus = 'Awaiting Notice';                    
               }
               activity.Activity_Status__c = ActivityStatus;
               //If this is a new record then default reinstatement status to 'Schld'
               if(Trigger.isInsert)
                   activity.Status__c = 'Schld';
            }
            //when Reinstatement status changes to a status that is not 'PermC'or 'IntrC'then deattach Activity from closure report
            //so that it can can be added to next batch
            if ((activity.Status__c != 'PermC')&&(activity.Status__c != 'IntrC')){
                activity.Closure_Report__c = null;
            }
            if(Trigger.isInsert){
                Map<Id,Double> mapCounts = tRIIO_QuotationAttachment.ValidateNotice(ActivityNew);
                for(tRIIO_Activity__c activity_Quotation: Trigger.new){                
                    if(activity_Quotation.Works_Order__c != null){
                        if(mapCounts.get(activity_Quotation.Works_Order__c) != null && mapCounts.get(activity_Quotation.Works_Order__c) != 0.0){
                            activity_Quotation.addError('Notice already generated for this Quotation'); 
                        }
                    }
                }
            }    
        }
    }

 
pconpcon
To start off with, this is simply just adding a for loop that iterates over all of you Trigger.new objects if isUpdate and if the checkbox is checked then update the status.  I would say at this point you probably want to refactor your trigger into a class [1] and then add methods for each one of your concerns.  So updating the status would be one method, and generating notices would be another method, etc.

[1] http://blog.deadlypenguin.com/blog/2012/02/13/classifying-triggers-in-salesforce/
TheLearnerTheLearner
Hi Pcon,

Thanks for the reply , they already mentioned is insert and isupdate , in that how to amend the code , could you please tell me how to amened please please
pconpcon
I wouldn't recommend doing this because your trigger is already way past the size and scope of what I would have in a single trigger.  You can create an additional trigger (since these don't seem to conflict).
 
trigger setActivityStatusForDefectOnHold on tRIIO_Activity__c (before update, before insert) {
    for (tRIIO_Activity__c activity : Trigger.new) {
        if (activity.Defect_on_Hold__c) {
            activity.Activity_Status__c = 'HOLD';
        }
    }
}

Doing this will seperate your concerns instead of having one massive trigger.  There is NO guarenteed order of operations on triggers so this one could run earlier than your other trigger.  I strongly suggest that you start investigating classifying your triggers at this point.