+ Start a Discussion
TheLearnerTheLearner 

Checkbox should be true and false based on the text value--- urgent

Hi Experts,

My requriment is that when i enter the values in the text box checkbox should be checked,if i remove the value in the text box , checkbox should be unchecked, but in my code checkbox are getting unchecked without removing the values in the text box, could anyone help me please. here Condition_Text__c , NCT01a_Date_Constraint__c ,NCT01b_Date_Constraint__c  are long text area and NCT01_Date_Constraints__c is checkbox. one more thing is that for the 2 text valeus there is only one checkbox it should be unchecked when there is no value in the both text boxs, if anyone contain value it should be checked


trigger Update_Condition_Text on SCC__c (Before insert, Before update) {

String newConditionType='';
    for(SCC__c  swCon:trigger.new)
    {
        if(swCon.Condition_Text__c != null && swCon.Condition_Text__c != '')
            newConditionType = swCon.Condition_Text__c;
      
        if(trigger.isInsert)
        {
           
            if(swCon.NCT01a_Date_Constraint__c !='' && swCon.NCT01a_Date_Constraint__c != null)
            {
                //newConditionType = newConditionType + ', NCT01a Date Constraint: '+swCon.NCT01a_Date_Constraint__c; //CH01.Old
                 system.debug('initial date check' + swCon.NCT01a_Date_Constraint__c);

                  newConditionType = newConditionType + ', NCT01a '+swCon.NCT01a_Date_Constraint__c; 
                  system.debug('initial date check' + swCon.NCT01_Date_Constraints__c);
                  swCon.NCT01_Date_Constraints__c=True;
                  system.debug('checkbox date check' + swCon.NCT01_Date_Constraints__c);
             }
             else
             {
              system.debug('Pleaes check' + swCon.NCT01_Date_Constraints__c);
              swCon.NCT01_Date_Constraints__c=false;
              system.debug('what check' + swCon.NCT01_Date_Constraints__c);
             }
             if(swCon.NCT01b_Date_Constraint__c !='' && swCon.NCT01b_Date_Constraint__c != null)
            {
                //newConditionType = newConditionType + ', NCT01b Date Constraint: '+swCon.NCT01b_Date_Constraint__c; //CH01.Old
             
                  newConditionType = newConditionType + ', NCT01b '+swCon.NCT01b_Date_Constraint__c;
                  swCon.NCT01_Date_Constraints__c=True;
                 system.debug('@@@@@@' + swCon.NCT01_Date_Constraints__c);

             }
             else
             {
                 swCon.NCT01_Date_Constraints__c=false;
             }
      }

      if(trigger.isUpdate)
        {
            if((swCon.NCT01a_Date_Constraint__c !=Trigger.oldMap.get(swCon.ID).NCT01a_Date_Constraint__c) && swCon.NCT01a_Date_Constraint__c != null && swCon.NCT01a_Date_Constraint__c !='')
            {
                //newConditionType = newConditionType + ', NCT01a Date Constraint: '+swCon.NCT01a_Date_Constraint__c;//CH01.Old
                //CH01.New.Start
                newConditionType = newConditionType + ', NCT01a '+swCon.NCT01a_Date_Constraint__c;
                swCon.NCT01_Date_Constraints__c=True;
                system.debug('######' + swCon.NCT01_Date_Constraints__c);
             }
             else
             {
              swCon.NCT01_Date_Constraints__c=false;
              system.debug('$$$$$$$$' + swCon.NCT01_Date_Constraints__c);
             }
             //CH01.New.End   
            if((swCon.NCT01b_Date_Constraint__c !=Trigger.oldMap.get(swCon.ID).NCT01b_Date_Constraint__c) && swCon.NCT01b_Date_Constraint__c != null && swCon.NCT01b_Date_Constraint__c !='')
             {
                //newConditionType = newConditionType + ', NCT01b Date Constraint: '+swCon.NCT01b_Date_Constraint__c;//CH01.old
                //CH01.New.Start
                 newConditionType = newConditionType + ', NCT01b '+swCon.NCT01b_Date_Constraint__c;
                 swCon.NCT01_Date_Constraints__c=True;
                 system.debug('!!!!!!!' + swCon.NCT01_Date_Constraints__c);
             }
             else
             {
              swCon.NCT01_Date_Constraints__c=false;
              system.debug('&&&&&&&' + swCon.NCT01_Date_Constraints__c);
             }
  }
  if(newConditionType != null && newConditionType != '')
        {
            newConditionType = newConditionType.removeStart(', ');
            if(newConditionType.length()>500)        
            {
               swCon.addError('Edit text in the fields in the section NCT Details to make it 500 or less so record can save');
            }
            else
                swCon.Condition_Text__c = newConditionType;
        }
    }
}
ManojjenaManojjena
Hi the Learner ,
Try with below code if it is working then implement other logic in this !!
trigger Update_Condition_Text on SCC__c (Before insert, Before update) {
   for(SCC__c ssc:Trigger.new){
      if(ssc.NCT01a_Date_Constraint__c != null || ssc.NCT01a_Date_Constraint__c != ''  && ssc.NCT01b_Date_Constraint__c   != null ||  ssc.NCT01b_Date_Constraint__c !=''){
	    ssc.NCT01_Date_Constraints__c =True;
	  }else{
	    ssc.NCT01_Date_Constraints__c =False;
	  }
   }
}
Try to comment  your triggger and add above code in trigger and test once .
Let me know if it helps !!
Thanks 
Manoj
TheLearnerTheLearner
Hi Manoj,

Actually we need to amend the code in the above code, i told you already about my requiremnet is that when i enter the value in this in this field NCT01a_Date_Constraint__c ( long text area) this NCT01_Date_Constraints__c checkbox field has to be checked if i remove the value in that field this checkbox has to be unchecked, my code is working fine without this conditon  ((swCon.NCT01a_Date_Constraint__c !=Trigger.oldMap.get(swCon.ID).NCT01a_Date_Constraint__c) ||(swCon.NCT01b_Date_Constraint__c !=Trigger.oldMap.get(swCon.ID).NCT01b_Date_Constraint__c) ))
, could you tell me please.



trigger Update_Condition_Text on SCC__c (Before insert, Before update) {

String newConditionType='';
 for(SCC__c  swCon:trigger.new)
 {
    if(swCon.Condition_Text__c != null )
     newConditionType = swCon.Condition_Text__c;
      if(trigger.isInsert)
       {
        if(swCon.NCT01a_Date_Constraint__c != null || swCon.NCT01b_Date_Constraint__c != null)
         {  
            if(swCon.NCT01a_Date_Constraint__c != null)
               {
                   //newConditionType = newConditionType + ', NCT01a Date Constraint: '+swCon.NCT01a_Date_Constraint__c; //CH01.Old

                     system.debug('initial date check' + swCon.NCT01a_Date_Constraint__c);

                      newConditionType = newConditionType + ', NCT01a '+swCon.NCT01a_Date_Constraint__c;

                      system.debug('initial date check' + swCon.NCT01_Date_Constraints__c);

                      swCon.NCT01_Date_Constraints__c=True;

                      system.debug('checkbox date check' + swCon.NCT01_Date_Constraints__c);

                }

                if(swCon.NCT01b_Date_Constraint__c != null)

                {

                    //newConditionType = newConditionType + ', NCT01b Date Constraint: '+swCon.NCT01b_Date_Constraint__c; //CH01.Old

                  

                      newConditionType = newConditionType + ', NCT01b '+swCon.NCT01b_Date_Constraint__c;

                      swCon.NCT01_Date_Constraints__c=True;

                     system.debug('@@@@@@' + swCon.NCT01_Date_Constraints__c);

 

                }

            }  

             else

             {

                 swCon.NCT01_Date_Constraints__c=false;

             }

      }

 

      if(trigger.isUpdate)

        {

            if( ( swCon.NCT01a_Date_Constraint__c != null || swCon.NCT01b_Date_Constraint__c != null) && ((swCon.NCT01a_Date_Constraint__c !=Trigger.oldMap.get(swCon.ID).NCT01a_Date_Constraint__c) ||(swCon.NCT01b_Date_Constraint__c !=Trigger.oldMap.get(swCon.ID).NCT01b_Date_Constraint__c) ))


            {

                if(swCon.NCT01a_Date_Constraint__c != null)

                {

                    //newConditionType = newConditionType + ', NCT01a Date Constraint: '+swCon.NCT01a_Date_Constraint__c;//CH01.Old

                    //CH01.New.Start

                    newConditionType = newConditionType + ', NCT01a '+swCon.NCT01a_Date_Constraint__c;

                    swCon.NCT01_Date_Constraints__c=True;

                    system.debug('######' + swCon.NCT01_Date_Constraints__c);

                 }

                  

                if(swCon.NCT01b_Date_Constraint__c != null)

                 {

                    //newConditionType = newConditionType + ', NCT01b Date Constraint: '+swCon.NCT01b_Date_Constraint__c;//CH01.old

                    //CH01.New.Start

                     newConditionType = newConditionType + ', NCT01b '+swCon.NCT01b_Date_Constraint__c;

                     swCon.NCT01_Date_Constraints__c=True;

                     system.debug('!!!!!!!' + swCon.NCT01_Date_Constraints__c);

                 }

             }

             else

             {

              swCon.NCT01_Date_Constraints__c=false;

              system.debug('&&&&&&&' + swCon.NCT01_Date_Constraints__c);

             }

       }

        if(newConditionType != null && newConditionType != '')

        {

            newConditionType = newConditionType.removeStart(', ');
            if(newConditionType.length()>500)       

            {
               swCon.addError('Edit text in the fields in the section NCT Details to make it 500 or less so record can save');

            }

            else

                swCon.Condition_Text__c = newConditionType;

        }

    }

}