Is there an alternative for g_form.isNewRecord()?

ahmedaabdel
Tera Contributor

Hi

 

I want a similar method as g_form.isNewRecord() to return true on the submit of a form to be used as a condition for changing a field value using:

"g_form.setValue('u_customer_state', 'Assigned');"

 

as this work everytime when i use it in the else part but i want to change it to else if

 

 

function onCondition() {
    if(g_form.isNewRecord()){  
      g_form.setValue('u_customer_state', 'Open');
    }
    else{
        g_form.setValue('u_customer_state', 'Assigned');
    }
}
2 ACCEPTED SOLUTIONS

Ankur Bawiskar
Tera Patron
Tera Patron

@ahmedaabdel 

Why client script is required?

why not use before insert BR which runs only once and will set the value to Open?

OR

why not use default value as Open for that at dictionary level

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

PrashantLearnIT
Giga Sage

Hi @ahmedaabdel 

 

Use Before Insert/Update BR instead of onSubmit Client Script, Use the default value for setting as Open

********************************************************************************************************
Please appreciate the efforts of community contributors by marking the appropriate response as the correct answer and helpful. This may help other community users to follow the correct solution in the future.

********************************************************************************************************
Cheers,
Prashant Kumar
ServiceNow Technical Architect


Community Profile LinkedIn YouTube Medium TopMate
********************************************************************************************************

View solution in original post

10 REPLIES 10

Juhi Poddar
Kilo Patron

Hello @ahmedaabdel 

Using an onSubmit client script is an ideal solution when you need to execute logic specifically during the form submission process.

 

function onSubmit() {
    if (g_form.isNewRecord()) {
        g_form.setValue('u_customer_state', 'Open');
    } else {
        g_form.setValue('u_customer_state', 'Assigned');
    }
}

 

Hope this helps!

 

"If you found my answer helpful, please like and mark it as an "accepted solution". It helps future readers to locate the solution easily and supports the community!"

 

Thank You
Juhi Poddar

Unfortunately it didn't enter the else if
is there any solution?

Ankur Bawiskar
Tera Patron
Tera Patron

@ahmedaabdel 

Why client script is required?

why not use before insert BR which runs only once and will set the value to Open?

OR

why not use default value as Open for that at dictionary level

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Can you explain to me how to do both solutions to try them