auto filled data from previous record

beycos
Tera Contributor

Hi everyone,

 

In my scenario, a new record is automatically created in the system every six months for each user. I want the newly created record to be pre-populated with the data that the same user submitted in their previous record.

 

What would be the best way to implement this functionality?

Thank you 

Beyza

10 REPLIES 10

Please don't use 'current.update()' for before BR ever.

 

Please try change few lines,

 

if (current.getValue('u_assign_to') == targetUserSysId)

{..........

          current.setValue('u_1_hr', gr.u_1_hr);

          current.setValue('u_10_min', gr.u_10_min);
          ........
}
 

If this address your question, please don't forget to mark this response correct by clicking on Accept as Solution and/or Kudos.

You may mark this helpful as well if it helps you.

Thanks, 

Animesh Das

Thank you Animesh , 

I updated the Business Rule as you suggested, and it works. However, every time a new record is created, it fetches data from the same record. I want a data from the previous record updated by the user each time. Unfortunately, with script  I am unable to update the u_10_min fields.

create new record ==> auto filled data from previous record with same user(1)==> updated by user(2)==> wait for condition==> auto filled data from previous record updated by user==> updated by user..... 

Thank you in advance for your help.. 

Best Regards, 

Beyza

 

Hi @beycos ,

 

I hope you are using before BR only.

It fetches data from same record meaning the record which is getting created that one not it's previous one?

I am not quite sure about other related scripts and configuration but you can try adjust the script a bit to cater this.

 

(function executeRule(current, previous /*null when async*/ ) {
 var targetUserSysId = 'aa55a5a5a5a5a5a5a5a5a5a5a';  // users sys_id
if (current.getValue('u_assign_to') == targetUserSysId) {
        {
            var gr = new GlideRecord('u_y'); // Target table
            gr.addQuery('u_assign_to', targetUserSysId); // Match the same user reference
            gr.orderByDesc('sys_created_on'); // Sort by creation date (most recent first)
            gr.setLimit(2); // Fetch only the latest records
            gr.query();
            gr.next();
            if (gr.next()) {
                // Copy the field value from the found record to the current record

            current.setValue('u_1_hr', gr.u_1_hr);

            current.setValue('u_10_min', gr.u_10_min);
            }
        }
    }
})(current, previous);
 

If this address your question, please don't forget to mark this response correct by clicking on Accept as Solution and/or Kudos.

You may mark this helpful as well if it helps you.

Thanks, 

Animesh Das

Hello Animesh again , 

Thank you for your help and time. 

Yes, I am using only a "before" Business Rule. With this BR, for example, u_1_hr is automatically coming from the previous record. Because of this, I cannot update it. For instance, if the previous u_1_hr = 2, this value (2) is automatically carried over to the new record. However, I cannot change this 2 to 3 in the new record. Because the BR says "this value from the previous record. When I update the value from 2 to 3 on u_1_hrit reverts back to 2.(expected behaviour).Is there a way to update this field in the new record?

Thanks again , Regards 

Beyza

Your before BR should run only for insert not update.

Only keep the 'Insert' check box checked and 'Update' check box unckecked.

 

If this address your question, please don't forget to mark this response correct by clicking on Accept as Solution and/or Kudos.

You may mark this helpful as well if it helps you.

Thanks, 

Animesh Das