Important Update: Community URLs redirect issues are partially resolved. Learn More. .

cancel
Showing results for 
Search instead for 
Did you mean: 

Javascript custom code for Multiple values

Bharath
Contributor III

Hi ,

We have a requirement to populate source data from one application to other application on a button click by creating a record, we have achieved this using content API buy writing to JavaScript code in custom object. When a single value is selected  in the source application the value is moving to target as per below code. When multiple values are selected only 1 values is moving to target application (its happening because we used [0] in below code). Can someone help me on how the code looks when more than one value is selected in source.

 

var countries = $CM.getFieldvalue(1234);

var countries = 0;

if(countries !=null){

countries = countries.toString();

countries = countries.split(";");

countries_value = countries[0];

}

and now using this countries_value to be populated in target by using Type, tag, value and valuelistids attributes.

 

Thanks

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions

@AnuragTyagi  & @DavidPetty  This one worked for me as below

countries_value = countries.map(item=> item.split(':')[0] );

13870": {
"Type": 4,
"Tag": "Severity (Values List)",
"Value": {
"ValuesListIds": countries_value,
},
"FieldId": 13870
}

 

View solution in original post

8 REPLIES 8

DavidPetty
Archer Employee
Archer Employee

@Bharath it looks like you're  only returning the first value of the split, countries_value = countries[0];.  You're going to have to iteratate through the split array and populate accordingly.

The JSON for setting multiple values shoud look like this:

      "13870": {
        "Type": 4,
        "Tag": "Severity (Values List)",
        "Value": {
          "ValuesListIds": [
            63738,
            63821
          ]
        },
        "FieldId": 13870
      }

 

 Advisory Consultant

@DavidPetty  : Can you please help on how to use split array. 

Try something like this:

countries_value  = countries.split(";").map((country, index) => {
    return(
        countries[index] + {index < contries.length - 1 ? ", " : ""}
    );
})

 Advisory Consultant

Hi @DavidPetty ,

Output of source application is ['603:0', '604:0', '605:0'].

I have tried something like this but in target its not populating anything. Am i missing anything?

var countries = $CM.getFieldvalue(1234);

countries_value = countries.map(item=> item.split(':')[0] + ':0');

"13870": {
        "Type": 4,
        "Tag": "Severity (Values List)",
        "Value": {
          "ValuesListIds": [
            countries_value
          ]
        },
        "FieldId": 13870
      }

 

 

AnuragTyagi
Contributor II

@Bharath 

You almost there,  I think you need this.

var countries =  ['603:0', '604:0', '605:0'];

countries_value = countries.map(item=> item.split(':')[0] );

countries_value= ['603', '604', '605'];

@AnuragTyagi  I already tried it. Its not working. Nothing is populating in target app

@Bharath  You have to include double quotes in every value. your final object should be like this.

"13870": {
"Type": 4,
"Tag": "Severity (Values List)",
"Value": {
"ValuesListIds": [
"63738",
"63821"
]
},
"FieldId": 13870
}

I don't know how you will do but in my case its working with below code.

var idsOfValue= ["\"601\"", "\"602\""];

var Data="{\"Content\":{\"Id\":12345678,\"LevelId\" :0000,\"FieldContents\" : {\"9999\" :{\"Type\" : 4,\"Value\": {\"ValuesListIds\": ["+idsOfValue+"]},\"FieldId\" :9999}}}}";

@AnuragTyagi  & @DavidPetty  This one worked for me as below

countries_value = countries.map(item=> item.split(':')[0] );

13870": {
"Type": 4,
"Tag": "Severity (Values List)",
"Value": {
"ValuesListIds": countries_value,
},
"FieldId": 13870
}