web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

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:
I have the same question (0)
  • Verified answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Hello,

    Try to replace

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

    with

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

  • LeoAlt Profile Picture
    16,331 Moderator on at

    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
    LeoAlt Profile Picture
    16,331 Moderator on at

    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

  • Suggested answer
    Vighnesh Profile Picture
    1,942 on at

    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(','));

    }

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

News and Announcements

Season of Giving Solutions is Here!

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 > Customer Service

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans