{"revision":"c10276e953456835cdbb1f3a60d58ae1980be353","document":{"id":37074,"path":"order-management/api_calls_for_navigating_sets_in_logik_io.md","title":"API calls for navigating sets in ServiceNow CPQ","bundle":null,"doc_type":null,"release":"australia","body":"\n# API calls for navigating sets in ServiceNow CPQ\n\nUnderstand how to use sets in a headless environment using API calls.\n\n## Setup\n\nThe following set and fields were created for this example:\n\n-   Field: Example set field 1\n    -   Picklist with options A, B, C\n    -   Field variable: exampleSetField1\n-   Field: Example set field 2\n    -   Boolean\n    -   Field variable: exampleSetField2\n-   Set: Example set\n    -   Contains Fields exampleSetField1 and exampleSetField2\n    -   Variable name: exampleSet \\(therefore, size field is set.exampleSet.size\\)\n\n\\[Omitted image \"cpq-apis-example-set-field-1.png\"\\] Alt text: Example set\n\n## Row identifier and row index\n\nEvery time a set is expanded to include a new row, the new row has a row identifier and a row index assigned to it. These are how you change the set.\n\n-   The row identifier starts at 1 and increases incrementally by 1. This number is used to add or remove rows from the set.\n-   The row index starts at 0 and increases incrementally by 1. This number is used to adjust values of Fields in the set.\n\nAfter every API call that changes the set, the rows are organized. Due to the reorganization, the row index and row identifiers are reassigned. This is explained in greater detail in the examples below.\n\n## Changing the set size\n\nThere are two ways to change the number of rows in a set: by sending a single number, and by sending specific values for the row identifiers.\n\nThe first way to change the number of rows in a set is to send a single number \\(the number of rows\\) as the value of the size variable in the API call. This method offers less control, but is useful if you know the total number of rows in the set you want to build. In the following example, the number 6 is set as the value of the size and it creates row identifiers 1-6. To edit the fields in these rows, you would use the row index numbers of 0-5.\n\n\\[Omitted image \"cpq-apis-change-set-size-1.png\"\\] Alt text: API\n\nThe second way to edit the size of the array is to send values in the placeholder for each row of the array. This is useful for adjusting the rows of the array with precision. Depending on what values are added to the API call and their order, a combination of the following three changes will occur:\n\n-   If the API call references a value that is not among the current row identifiers, it will create a new row in the position of the new value.\n-   If the API call does not reference any value that is among the current row identifiers, the row will be deleted from the array.\n-   If the API call references a value that is among the current row identifiers, it will put that array row in the position of the row identifier in the API call.\n\nPreviously, the size of ExampleSet was set to 6, which resulted in the row identifiers \\[“1”, “2”,”3”,”4”,”5”,”6”\\].\n\nNow, the following API call is sent to the same environment:\n\n\\[Omitted image \"cpq-apis-send-row-identifier.png\"\\] Alt text: Script\n\nWhen this call is sent, the following occurs:\n\n-   No row identifier is called “Newrow1”. Therefore, a new row \\( with the fields in the row set to defaults\\) is inserted before any known row identifiers. \\(This is the first change listed above.\\)\n-   A row identifier is called “5”. In the API call, it is in the second position \\(after NewRow1\\). Therefore, it will move to the second row position. \\(This is the second change listed above.\\)\n-   No row identifier is called “7”. Therefore, a new blank row is inserted in position 3. \\(First change.\\)\n-   The row identifiers “1”, “2”, “3”, “4”, and “6” are not referenced in the API call. Therefore, they are deleted. \\(Third change listed above.\\)\n\nWhen all changes from the API call are complete, the row identifiers are reset.\n\n\\[Omitted image \"cpq-apis-row-identifiers-reset.png\"\\] Alt text: Script\n\nThe following table shows this in greater detail, including the index values, which will be discussed in the next section.\n\n<table id=\"table_k2t_l4x_2hc\"><thead><tr><th>\n\nSize field new identifier\n\n</th><th>\n\nValue sent in API call\n\n</th><th>\n\nOld row identifier\n\n</th><th>\n\nNew index\n\n</th><th>\n\nOld index\n\n</th></tr></thead><tbody><tr><td>\n\n1\n\n</td><td>\n\nNewrow1\n\n</td><td>\n\nDid not exist\n\n</td><td>\n\n0\n\n</td><td>\n\nDid not exist\n\n</td></tr><tr><td>\n\n2\n\n</td><td>\n\n5\n\n</td><td>\n\n5\n\n</td><td>\n\n1\n\n</td><td>\n\n4\n\n</td></tr><tr><td>\n\n3\n\n</td><td>\n\n7\n\n</td><td>\n\nDid not exist\n\n</td><td>\n\n2\n\n</td><td>\n\nDid not exist\n\n</td></tr><tr><td>\n\n\\{deleted rows\\}\n\n</td><td>\n\n1,2,3,4 and 6 were not sent in API call defining size\n\n</td><td>\n\n1,2,3,4 and 6\n\n</td><td>\n\nDeleted\n\n</td><td>\n\n0,1,2,3 and 5\n\n</td></tr></tbody>\n</table>Important details to note:\n\n-   After this API call, the row with row identifier 5 \\(and index 4\\) now has the row identifier 2 \\(and an index of 1\\) that need to be used for all future API calls.\n-   Once a row is deleted from the array, it is deleted completely. So a new API call that references row identifier 6 would be referencing a new row. All earlier data and changes to fields in row identifier 6 are no longer relevant and cannot be referenced.\n-   This is helpful for changing the order of rows.\n\nOne final note: If the API patch call \"variableName\": \"set.exampleSet.size\", \"value\": \\[\"Newrow1\", \"5\", \"7\"\\] is sent more than once, the three old rows are deleted and three new rows are created. This would lose the data that was previously in 5.\n\n## Changing set fields\n\nThe next step is to define values for the fields in the set. Assume that the following call occurs after the first size API patch sets the size to six rows. To change Example Set Field 1 and Example Set Field 2 for several rows, the following code would be added to the fields section of an API call:\n\n```\n{{\n            \"set\": \"exampleSet\",\n            \"index\": 0,\n            \"variableName\": \"ExampleSetField1\",\n            \"value\": \"A\"\n        },\n        {\n            \"set\": \"exampleSet\",\n            \"index\": 4,\n            \"variableName\": \"ExampleSetField1\",\n            \"value\": \"B\"\n        },\n{\n            \"set\": \"exampleSet\",\n            \"index\": 4,\n            \"variableName\": \"ExampleSetField2\",\n            \"value\": \"True\"\n        },\n{\n            \"set\": \"exampleSet\",\n            \"index\": 5,\n            \"variableName\": \"ExampleSetField1\",\n            \"value\": \"C\"\n        },\n```\n\nThis would result in the following:\n\n<table id=\"table_l2t_l4x_2hc\"><tbody><tr><td>\n\nRow Identifier\n\n</td><td>\n\n1\n\n</td><td>\n\n2\n\n</td><td>\n\n3\n\n</td><td>\n\n4\n\n</td><td>\n\n5\n\n</td><td>\n\n6\n\n</td></tr><tr><td>\n\nIndex\n\n</td><td>\n\n0\n\n</td><td>\n\n1\n\n</td><td>\n\n2\n\n</td><td>\n\n3\n\n</td><td>\n\n4\n\n</td><td>\n\n5\n\n</td></tr><tr><td>\n\nexample SetField 1\n\n</td><td>\n\nA\n\n</td><td>\n\n \n\n</td><td>\n\n \n\n</td><td>\n\n \n\n</td><td>\n\nB\n\n</td><td>\n\nC\n\n</td></tr><tr><td>\n\nexample SetField 2\n\n</td><td>\n\n \n\n</td><td>\n\n \n\n</td><td>\n\n \n\n</td><td>\n\n \n\n</td><td>\n\nTrue\n\n</td><td>\n\n \n\n</td></tr></tbody>\n</table>If the second size API patch from above is sent, the following would result:\n\n<table id=\"table_m2t_l4x_2hc\"><tbody><tr><td>\n\nSize Field New Identifier\n\n</td><td>\n\n1\n\n</td><td>\n\n2\n\n</td><td>\n\n3\n\n</td></tr><tr><td>\n\nSize Field Old Identifier\n\n</td><td>\n\n\\{new row, see above\\}\n\n</td><td>\n\n5\n\n</td><td>\n\n\\{new row, see above\\}\n\n</td></tr><tr><td>\n\nOld Index\n\n</td><td>\n\n\\{new row\\}\n\n</td><td>\n\n4\n\n</td><td>\n\n\\{new row\\}\n\n</td></tr><tr><td>\n\nNew Index\n\n</td><td>\n\n0\n\n</td><td>\n\n1\n\n</td><td>\n\n2\n\n</td></tr><tr><td>\n\nexampleSetField 1\n\n</td><td>\n\n \n\n</td><td>\n\nB\n\n</td><td>\n\n \n\n</td></tr><tr><td>\n\nexampleSetField 2\n\n</td><td>\n\n \n\n</td><td>\n\nTrue\n\n</td><td>\n\n \n\n</td></tr></tbody>\n</table>"}}