flag true/false depending on two field value

madalinus1
Tera Contributor

Hello, I dont' kow if I write in correct location but I will try:

I need some help for this: 

When the user field changes, we will check whether the customer has visited the store at least once during the previous week and made a purchase; in this case, we will set the flag to true.

I created the form and the field names are the following:

user field: (user)
entry date ( entry_date )
flag: (already_customer)

 

madalinus1_0-1704995144256.png

 

 

if the user it's alredy present with a date oldest than 7 days, the field ( already_customer ) will change to true.

How I can do it with a onChange client script? 

7 REPLIES 7

Hi @madalinus1 ,
Try with client script and script include some methods will not work directly in client side.

function onChange(control, oldValue, newValue, isLoading) {    
            if (isLoading || newValue == '') {   
                  return;         }    

          var entry_date = g_form.getValue('entry_date');    
          var ga = new GlideAjax("calcDate"); //name of script include    
          ga.addParam("sysparm_name", "getDate"); //name of function in script include    
          ga.addParam("sysparm_start", entry_date); //send start value to script    
          ga.getXML(checkDate); //callback function    

      }    
      function checkDate(response) {          
          var answer = response.responseXML.documentElement.getAttribute("answer"); //the response from the script    
          if (answer <= 7) { 
          g_form.setValue('already_customer', true); 
          }    else{
 g_form.setValue('already_customer', true); 
          }  
      }

script include code-
var calcDate = Class.create();      
      calcDate.prototype = Object.extendsObject(AbstractAjaxProcessor,{      
          getDate : function() {      
              var startDT = new GlideDate();
              startDT.setDisplayValue(this.getParameter('sysparm_start'));
var currentdate = new GlideDate();
              var duration = new GlideDuration();
              duration= GlideDate.subtract(startDT, currentdate);
              return duration.getDayPart();  
          },      
          type: 'calcDate'      

      });  

Mark it as helpful and solution proposed if it serves your purpose.
Thanks,
Anand

Hello @Anand Kumar P ,

work fine thanks, but I have another question, if  Iwant to add another check for example, When the user field changes we will check if the customer came to the store at least once in the previous week and made a purchase, if so we will set the flag to true:

I think in this case I need to put a condition that check if the user it's already present in the table, and afetr the check on the date with the script that you shared with me

madalinus1_0-1705404123581.png

 

Hi @madalinus1 ,

You can send the user id from client side to script include and then check if user is available in sys_user table if available then return that dayscount.

 

Mark it as helpful and solution proposed if it serves your purpose.
Thanks,
Anand