function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
TheLearnerTheLearner 

changing the status by clicking on check box

Hi Experts,

I need to change the status when i check the checkbox, i need to amend in the code . could you anyone help me

Please call this new field “Defect_on_Hold__c” - checkbox. When checked, the Activity Status(Activity_Status__c) needs to change from "INTrR" to "HOLD". 

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'); 
                        }
                    }
                }
            }    
        }
    }
}
AshlekhAshlekh
Hi,

You need to check the value of check box field in before update and in before insert operation and if you found that value is true than do what you want to do. 

Thanks 
Ashlekh
TheLearnerTheLearner
Hi Ashlekh,

Thanks for ur reply, when Defect_on_Hold__c is ticked, i need to change this Activity_Status__c field value from     "INTrR" to "HOLD". 
TheLearnerTheLearner
Just i need to amend this change in that trigger, could help me pleaes
jyothsna reddy 5jyothsna reddy 5
Hi ,
    You need to check the value of check box field in before update and in before insert operation and write the below  trigger 


trigger Activity_Status_changed  on tRIIO_Activity__c (before insert, before update) {
 for(tRIIO_Activity__c tR:Trigger.New){
 
    if(tR.Defect_on_Hold__c==true){
      tR.Activity_Status__c ='HOLD';
      
    }
 }
}

 
If your are perform DML operations on the same object use 'Before' event.