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 

Checkbox should be true, when i enter the value in the text box-- urgent

Hi Experts,

I have requirement that is If text is entered in field NCT04b Material and Plant Storage(NCT04b_Material_and_Plant_Storage__c), on selecting Save button,  check box field NCT04 Material & Plant Storage((NCT04_Material_Plant_Storage__c)  should be true,
If text is removed/deleted from field the NCT04b Material and Plant Storage(NCT04b_Material_and_Plant_Storage__c), corresponding  chek box field is unchecked =(false). like this there are 11 text fields and 11 checkbox fields are there. Could you anyone help me please
Mohan_ShanmugamMohan_Shanmugam
Use the below code
trigger CheckPhoneNumber on Account (before insert,before update) {

    for(Account acc : trigger.new){
          //phone__c is the custom text field
          if(acc.phone__c=='')
              PhoneNumAvailable=false;
          else
              PhoneNumAvailable=true;
    }

}
Thanks,
Mohan Shanmugam
 
Mohan_ShanmugamMohan_Shanmugam
Replace the Account Object with your custom object and fields with your custom object fields.
TheLearnerTheLearner
Hi Mohan,

Thanks for the reply, i did as you said but the problem is that checkbox becaming true when i enter the value in the check box but , when i click on edit button and save the record all checkboxes getting uncheck, but my requriement is that when i clear the text box value then it has be to uncheck, but here when i just click on edit button and clicking on save button its showing all checkbox are unchecking . could you help me please. here is my code.

trigger Update_Condition_Text on SW_Condition__c (before insert, before update) 
{
    String newConditionType='';
    for(SW_Condition__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

                  newConditionType = newConditionType + ', NCT01a '+swCon.NCT01a_Date_Constraint__c; 
                  swCon.NCT01_Date_Constraints__c=True;
             }
             else
             {
              swCon.NCT01_Date_Constraints__c=false;
             }
             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;
             }
             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;
             }
             else
             {
              swCon.NCT01_Date_Constraints__c=false;
             }
             //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;
             }
             else
             {
              swCon.NCT01_Date_Constraints__c=false;
             }
  }
  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;
        }
    }
}
Mohan_ShanmugamMohan_Shanmugam
Hi TheLearner,

Below is a working code which i wrote.it was working fine for all scenarios.
trigger CheckPhoneNumber on Account (before insert,before update) {

    for(Account acc : trigger.new){
          //phone__c is the custom text field
            if(acc.phonenum__c==null)
              acc.PhoneNumAvailable__c=false;
          else
              acc.PhoneNumAvailable__c=true;
    }

}
In your case, the checkbox is getting unchecked because the below if condition is not met.
if(swCon.NCT01a_Date_Constraint__c !='' && swCon.NCT01a_Date_Constraint__c != null)
change to below if "swCon.NCT01a_Date_Constraint__c" is a string field, in case of anyother datatype put debug log and see when it is coming as Null and not Null and change the code accordingly.
 
if(swCon.NCT01a_Date_Constraint__c != null)

Still if it doesn't work revert again with the datatype of swCon.NCT01a_Date_Constraint__c

Thanks,
Mohan Shanmugam


 
TheLearnerTheLearner
Hi Mohan,

Its working fine in isinert, its problem coming in the isupdate only. could you please help me out.
Mohan_ShanmugamMohan_Shanmugam
Sure, i can help you, for that please post your updated code again and mention the datatype of the fields ?

Thanks,
Mohan Shanmugam
TheLearnerTheLearner
HI Mohan,

Thanks for the reply,Condition_Text__c , NCT01a_Date_Constraint__c ,NCT01b_Date_Constraint__c  are long text area.

NCT01_Date_Constraints__c is checkbox.


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;
        }
    }
}


 
 
Mohan_ShanmugamMohan_Shanmugam
Modified your code.Use the below and let me know
 
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)
            {
                //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 != 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)
            {
                //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 != 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;
			}
    }
}

 
TheLearnerTheLearner
Hi Mohan,

Thanks for the code its working fine, but the problem is that here two fields are there  NCT01a_Date_Constraint__c ,NCT01b_Date_Constraint__c for these two fields there only one checkbox,so when i remove the value in one of the  field eg:'NCT01a_Date_Constraint__c ' checkbox is becoming false but we need checkbox should be unchecked when the value is not there in the both fields, anyone of the field contains a value , it should be checke.

Could you help me please.


 
TheLearnerTheLearner
Hi Mohan,

Could you tell me why u removed the condition in the isupdate loop please. I would like to know the reason behind it so that i can use in the future , in my condition i used to compare with previous ids and new ids and null check, but simple checking null condition.
Mohan_ShanmugamMohan_Shanmugam
Modified the code again,below is the updated code.
 
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)
			{
				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;
		}
    }
}

 
TheLearnerTheLearner
One more problem is that, for two text boxes there is one checkbox, here the thing is that checkbox has be to unchecked when there is no value in the two text boxes, if one of text box contain value it should be checked only.
Mohan_ShanmugamMohan_Shanmugam
Hi,

The above code will uncheck the checkbox only when there is no values in both the textbox.

If you really want that old and new comparison, then compare the old and new values after the trigger.isupdate statement ,if there is a difference then execute your functionality.

What will happen if you dont compare ?
>>the code will execute each time you update the record.

What will happen if you compare old & new ?
>>the code will execute only if there is a difference.

Still have issues then i have to look into the code in your environment.

Thanks,
Mohan Shanmugam
TheLearnerTheLearner
HI Mohan,

Code is working fine without this condition if((swCon.NCT01a_Date_Constraint__c !=Trigger.oldMap.get(swCon.ID).NCT01a_Date_Constraint__c) , but we need to add this condition as well, please help me out.
 
Mohan_ShanmugamMohan_Shanmugam
Okay..No Problem.Replace the If condition in line#42 with the below,
 
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) ))

So, the the checkbox will be evaluated only when any of the text field is modified.
TheLearnerTheLearner
Hi Mohan,

thanks for the reply, i did amendment as you said,its getting same problem, without removing the values from the text field , checkbox are unchecking, could you please update me on this.

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;

        }

    }

}