Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Customer Service forum

Wep API Javascript

(0) ShareShare
ReportReport
Posted on by 1,942

Hi All,

I am using the following code to retrieve all the boolean fields present on the form and if the value = true, I am collecting all the display names and setting the value to multi text field. So far everything seems to work but it says invalid type error upon saving. 

function writebooleanfieldlables(executionContext){
debugger;
var attfldlst1 =[];
var outpt;
var formContext = executionContext.getFormContext();
var selectedfieldnames=[];
formContext.data.entity.attributes.forEach(
function(attribute,index){
if(attribute.getAttributeType() =="boolean"){
attfldlst1.push(attribute.getName());
}
}
);
attfldlst1.forEach(
function(attribute,index){
if(formContext.getAttribute(attribute).getValue() == 1){
selectedfieldnames.push(formContext.getControl(attribute).getLabel())
}
}
);
formContext.getAttribute("new_selectedfields").setValue(selectedfieldnames);
}

MS-CRM.JPG

Categories:
  • Suggested answer
    Vighnesh Profile Picture
    Vighnesh 1,942 on at
    RE: Wep API Javascript

    Thanks Andrew & LeoAlt, both the solutions are correct!

    Here is the updated code that I've used:

    function writebooleanfieldlables(executionContext){

    debugger;

    var attfldlst1 =[];

    var outpt;

    var formContext =  executionContext.getFormContext();

    var selectedfieldnames=[];

    formContext.data.entity.attributes.forEach(

    function(attribute,index){

    if(attribute.getAttributeType() =="boolean"){

    attfldlst1.push(attribute.getName());

    }

    }

    );

    attfldlst1.forEach(

    function(attribute,index){

    if(formContext.getAttribute(attribute).getValue() == 1 && formContext.getAttribute(attribute).getValue() != null){

    selectedfieldnames.push(formContext.getControl(attribute).getLabel())

    }

    }

    );

    formContext.getAttribute("new_selectedfields").setValue(selectedfieldnames.join(','));

    }

  • Verified answer
    LeoAlt Profile Picture
    LeoAlt 16,331 Moderator on at
    RE: Wep API Javascript

    Hi partner,

    "setValue()" function could only use String objects. The "selectedfieldnames" is an Array object so that it caused "invalid type" error.

    You should convert the "selectedfieldnames" from array to string.

    Try the following codes:

    var Strselectedfields=selectedfieldnames.join(","), setvalue(Strselectedfields);

    formContext.getAttribute("new_selectedfields").setValue(Strselectedfields);

    Hope it helps.

    Best Regards,
    Leo

  • LeoAlt Profile Picture
    LeoAlt 16,331 Moderator on at
    RE: Wep API Javascript

    Hi partner,

    "setValue()" function could only use string objects. The "selectedfieldnames" is an array so that it caused "invalid type" error.

    You should convert the "selectedfieldnames" from array to string.

    Try the following codes:

    var Strselectedfields=selectedfieldnames.join(","),

    formContext.getAttribute("new_selectedfields").setValue(Strselectedfields);

    Hope it helps.

    Best Regards,

    Leo

  • Verified answer
    a33ik Profile Picture
    a33ik 84,325 Most Valuable Professional on at
    RE: Wep API Javascript

    Hello,

    Try to replace

    formContext.getAttribute("new_selectedfields").setValue(selectedfieldnames);

    with

    formContext.getAttribute("new_selectedfields").setValue(selectedfieldnames.join(','));

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Coming on 11/8!

In our never-ending quest to help the Dynamics 365 Community members get answers faster …

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,516 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,432 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans