- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-06-2024 07:32 PM - edited 04-06-2024 07:55 PM
Hi Team,
here is my code, please let me know which is best practice to follow
Please check BOLDED lines in code.
Business Rule :
var result = getDetails(current.getValue('sys_id');
Or
var result = JSON.parse(getDetails(current.getValue('sys_id')));
if(result.firstassignee){
current.u_assignee_level='Level 1 User';
} else if(result.group || result.user){
current.u_assignee_level='Level 2 User';
current.u_notify=gr.notify;
current.u_critical_level = gr.critical;
}
function getDetails(SYSID){
var gr ={ };
gr.firstassignee =false;
gr.group=false;
gr.user =false;
var gr1 = new GlideRecord('metric_instance');
gr.addQuery('definition=33j2krj329032rjwrjew98932jlsdwe^ORdefinition=33j2krj7863nsdf987232jlsljd^id='+SYSI);
gr.setLimit(1);
gr.query();
if(gr.next()){
if(gr.definition=='33j2krj329032rjwrjew98932jlsdwe')
gr.group=true;
else if(gr.definition=='33j2krj7863nsdf987232jlsljd')
gr.user =true;
gr.critical = 'P2';
gr.notify ='Yes';
}else {
gr.firstassignee =true;
}
return gr; OR return JSON.stringify(gr);
}
I normally use
return JSON.stringify(gr); format For Script include and Client script combination .
return gr ; format For Script include , Business rules combinations OR server-side inside function for return values like above.
Please let me know which is best practice for Server -side in the above example.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-06-2024 08:40 PM
In the context of your provided code and considering best practices, returning a JavaScript object (gr) without converting it to JSON (JSON.stringify(gr)) is the recommended approach.
Because of:
1. Simplicity and Readability: Returning the JavaScript object directly (gr) keeps the code simpler and easier to read.
2. Performance: Converting an object to JSON (JSON.stringify()) incurs additional processing overhead compared to returning the object directly. If there's no need to convert the object to JSON, it's better to avoid this unnecessary step, especially in server-side operations where performance might be critical.
Please Mark ✅Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-06-2024 08:40 PM
In the context of your provided code and considering best practices, returning a JavaScript object (gr) without converting it to JSON (JSON.stringify(gr)) is the recommended approach.
Because of:
1. Simplicity and Readability: Returning the JavaScript object directly (gr) keeps the code simpler and easier to read.
2. Performance: Converting an object to JSON (JSON.stringify()) incurs additional processing overhead compared to returning the object directly. If there's no need to convert the object to JSON, it's better to avoid this unnecessary step, especially in server-side operations where performance might be critical.
Please Mark ✅Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-08-2024 06:47 AM
Hi @Maddysunil Thanks for your reply on that.
Sure I will follow JavaScript object directly (gr) : return gr; as a Best practice and Best approach to keep simple and read-only faster.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-08-2024 07:17 AM