Hello mates,
I hope you 're all doing well, I am facing a situation that I have never encountered before
I would like to set a field required based on a condition ? I started writing a JS code but dont know if its correct or no
My requirement is to set the field "nameofsd" as required if the field "taskproject" is equal to "finished" or "finished" and if the field "team" is equal to "Team 1" or "Team 2" or "Team3"
function
setFieldrequirementasrequired(executionContext)
{
// Getting formContext
var
formContext = executionContext.getFormContext();
var
task = formContext.getAttribute(
"new_taskproject"
).getValue();
var
team = formContext.getAttribute(
"new_projectteam"
).getValue();
if
(task == "finished" || "signed" && team= "Team1" || "Team2" || "Team3")
{
formContext.getAttribute(
"nameofsd"
).setRequiredLevel(
"required"
);
}
else
{
//setting Email field requirement as none.
formContext.getAttribute(
"nameofsd"
).setRequiredLevel(
"none"
);
}
}
Thanks in advance mates :)
Hi,
Are you Passing the Execution Context from the Form itself?
docs.microsoft.com/.../clientapi-execution-context
Thank you,
Amit Katariya
Hi,
Add alert in your code to check the valuue -
alert("Task Value:"+task);
alert("Team Value:"+team[0].name);
If you want to use integer value then you should change below line
var task = formContext.getAttribute("new_taskproject").getText();
to
var task = formContext.getAttribute("new_taskproject").getValue();
Debug your JS code - carldesouza.com/.../
Please mark my answer verified if this is helpful!
Regards,
Bipin Kumar
Follow my Blog: xrmdynamicscrm.wordpress.com/
[/quote]Hi mate,
Sorry doesnt work too :(, you will find below the code i'm using :
function setFieldrequirementasrequired(executionContext)
{
// Getting formContext
var formContext = executionContext.getFormContext();
var task = formContext.getAttribute("new_task").getValue();
var team = formContext.getAttribute("new_projectteamid").getText();
if ((task =="698690007" || task=="698690008"||) && (team=="Team1" || team=="Team2" || team=="Team3" || team== "Team4" || team=="Team5" || team=="Team6"))
{
formContext.getAttribute("new_nameofsd").setRequiredLevel("required");
}
else
{
formContext.getAttribute("new_nameofsd").setRequiredLevel("none");
}
alert("Task Value:"+task);
alert("Team Value:"+team);
}
Hi,
Add alert in your code to check the valuue -
alert("Task Value:"+task);
alert("Team Value:"+team[0].name);
If you want to use integer value then you should change below line
var task = formContext.getAttribute("new_taskproject").getText();
to
var task = formContext.getAttribute("new_taskproject").getValue();
Debug your JS code - carldesouza.com/.../
Please mark my answer verified if this is helpful!
Regards,
Bipin Kumar
Follow my Blog: xrmdynamicscrm.wordpress.com/
Hi,
I updated your code. Please copy and try that -
function setFieldrequirementasrequired(executionContext)
{
// Getting formContext
var formContext = executionContext.getFormContext();
var task = formContext.getAttribute("new_taskproject").getText();
var team = formContext.getAttribute("new_projectteam").getValue();
if ((task == "finished" || task=="signed") && (team[0].name= "Team1" || team[0].name=="Team2" || team[0].name=="Team3"))
{
formContext.getAttribute("nameofsd").setRequiredLevel("required");
}
else
{
//setting Email field requirement as none.
formContext.getAttribute("nameofsd").setRequiredLevel("none");
}
}
nameofsd should have prefix which you are missing in your code like new_nameofsd
docs.microsoft.com/.../gettext
docs.microsoft.com/.../getvalue
Please mark my answer verified if this is helpful!
Regards,
Bipin Kumar
Follow my Blog: xrmdynamicscrm.wordpress.com/
[/quote]Hi Bipin Kumar,
i try'd your code below and unfortunately it doesnt work :(.
function setFieldrequirementasrequired(executionContext)
{
// Getting formContext
var formContext = executionContext.getFormContext();
var task = formContext.getAttribute("new_taskproject").getText();
var team = formContext.getAttribute("new_projectteam").getValue();
if ((task == "finished" or i have to write "698 690 007" ? || task=="signed") && (team[0].name= "Team1" || team[0].name=="Team2" || team[0].name=="Team3"))
{
formContext.getAttribute("new_nameofsd").setRequiredLevel("required");
}
else
{
formContext.getAttribute("new_nameofsd").setRequiredLevel("none");
}
}
Hi,
I updated your code. Please copy and try that -
function setFieldrequirementasrequired(executionContext)
{
// Getting formContext
var formContext = executionContext.getFormContext();
var task = formContext.getAttribute("new_taskproject").getText();
var team = formContext.getAttribute("new_projectteam").getValue();
if ((task == "finished" || task=="signed") && (team[0].name= "Team1" || team[0].name=="Team2" || team[0].name=="Team3"))
{
formContext.getAttribute("nameofsd").setRequiredLevel("required");
}
else
{
//setting Email field requirement as none.
formContext.getAttribute("nameofsd").setRequiredLevel("none");
}
}
nameofsd should have prefix which you are missing in your code like new_nameofsd
docs.microsoft.com/.../gettext
docs.microsoft.com/.../getvalue
Please mark my answer verified if this is helpful!
Regards,
Bipin Kumar
Follow my Blog: xrmdynamicscrm.wordpress.com/
assuming these two fields are text (because if they are of another type like lookups the syntax is different) the problem is how the conditions are written, the correct way would be
Hi Guido Preite,
The format of "taskproject" field is an options group list
The format of "team" field is a lookup
Regards
assuming these two fields are text (because if they are of another type like lookups the syntax is different) the problem is how the conditions are written, the correct way would be
if ((task == "finished" || task == "signed") && (team == "Team1" || team == "Team2" || team == "Team3"))
Maybe it's too simple, buy why not just use business rules to do this. It's drag and drop in the UI?
[/quote]Hi, thanks for your quick reply, unfortunately it's not achievable in my case :(
Maybe it's too simple, buy why not just use business rules to do this. It's drag and drop in the UI?
André Arnaud de Cal...
291,979
Super User 2025 Season 1
Martin Dráb
230,848
Most Valuable Professional
nmaenpaa
101,156