SAP Logo LeanIX is now part of SAP
Question

Unable to add /replace externalID to the factsheet through API

  • 31 October 2023
  • 7 replies
  • 63 views

Hello team, We are trying to add/replace an externalID with some custom field and value and some how we are not able to achieve through the python script through api. We are getting an error like this ‘{'code': 400, 'message': 'Query string must be provided'}’  Could you please suggest or advise how I can add/update/replace with externalId to the factsheet. Can we create and assign value to externalId while fact sheet creation through API?? please advise. Below is my python script.

import requests
import json

api_token = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXX'
auth_url = 'https://app.leanix.net/services/mtm/v1/oauth2/token'
#request_url = 'https://app.leanix.net/services/pathfinder/v1/graphql'
## Authentication:
response = requests.post(auth_url, auth=('apitoken', api_token),
data={'grant_type': 'client_credentials'})
response.raise_for_status()
access_token = response.json()['access_token']
auth_header = 'Bearer ' + access_token
header = {'Authorization': auth_header}
# #value: "{\"uffective\":\"FYCAPEX-9999\"}"
mutation_string = """"
mutation {
updateFactSheet( id: "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
patches: [{
op: replace,
path: "/externalId",
value: "{\”uffective\":\"FYCAPEX-9999\"}"
}],
validateOnly: true) {
factSheet {
... on Project {
externalId {
externalId
}
}
}
}
}
"""
mutation_string1= """
mutation ($patches: [Patch]!) {
result: updateFactSheet(id: "XXXXXXXXXXXXXXXXXXXXXXXX",
patches: $patches, validateOnly: true) {
factSheet {
... on Project {
externalId {
externalId
}
}
}
}
}
"""
data_mutation = {"query": mutation_string1}
json_post = json.dumps(data_mutation)
print("json_post",json_post)
import time
start_time = time.time()
graphql_api_url = "https://XXX.leanix.net/services/pathfinder/v1/graphql"
query_params={
"op": "replace",
"path": "/externalId",
"value": "{\”uffective\":\"FYCAPEX-9999\"}" }


query_params1={ "patches": [ {
"op": "replace",
"path": "/externalId",
"value": "{\”uffective\":\"FYCAPEX-9999\"}"
}
]
}
client = requests.Session()
response = client.post(
graphql_api_url,
headers={"Authorization": f"Bearer {access_token}", "Content-Type": "application/json"},
json=json_post,params=query_params
)
elapsed_time = time.time() - start_time
print(response)
print(response.json())

---------------------------

We want this way to be created as shown in image. 

 

 

we also tried by passing the below query to the script with out any query param but it says same error. mutation  {
  updateFactSheet( id: "b09e2c6d-4d2a-4f2d-afbf-4076ebdcd9d2", 
   patches: [{
        op: replace,
        path: "/externalId",
        value: "{\"uffective\":\"FYCAPEX-9999\"}"
      }], 
   validateOnly: true) {
    factSheet {
      ... on Project {
        externalId {
          externalId
        }
      }
    }
  }
}

Could you please advise as soon as possible . Awaiting your reply..


7 replies

Userlevel 5
Badge +2

Hi @comm_e2e, I am not sure if you want to go in this direction, but if you want to massively simplify your Python code, you can use the 3rd party LeanIX Python module documented at  https://doc.aronis.de/utils/fact_sheet.html, or in general https://doc.aronis.de/ .

Your 75 lines of code can be shortened to just 2 lines:

project = Project("xxxxxxxxxxxxxxxxxxxx")
project.update_field(externalId={"uffective": "FYCAPEX-9999"})

Note that this is assuming that your external ID field is indeed called “externalId”, but I guess that you need to update this to the real name of your data structure. The externalId field usually does not have a sub-field called “uffective”, so I guess you have a custom externalId field.

If you don’t want to consider using this Python module, I can recommend you to switch on a developer toolbar in the browser while you update the Uffective ID of a project, and then look at the network requests to find out the GraphQL that is sent.

Thank you @Thomas Schreiner, Thank you for the information. Could you please advise if this tool is open source ? The open Git workspace when I checked it just download a snapshot of projects and feeds. It's not fully functional for the update or create factsheets. Could you please provide some graphql sample if I have to replace with external id with Uffective id. like this ‘externalId={"uffective": "FYCAPEX-9999"}’ 

Userlevel 5
Badge +2

The LeanIX Python Module is not open source, but it can be licensed in different ways, including a lifetime license and a package for support and maintenance. Feel free to get in touch via direct message for a full demo and pricing options.

If you want to code it manually, you can use the following GraphQL:

mutation ($patches: [Patch]!) {
  updateFactSheet(id: "xxxxxxxxxxxxxxxxxxxxxxxx", 
   patches: $patches, validateOnly: false) {
    factSheet {
      ... on Project {
        externalId {
          externalId
        }
      }
    }
  }
}

Variables:
{
"patches":[{"op":"replace","path":"/externalId","value":"{\"uffective\":\"FYCAPEX-9999\"}"}]
}

Again, I’m almost 100% certain that the field names are incorrect in your example, because - as I wrote above - I think the combination of “/externalId” for the data structure and “uffective” as field name is not valid. You should check the real names for your fields in the meta model configuration.

I would assume it’s either “/externalId” and “externalId” (if only the label was changed to “Uffective ID”), or it could be like “/uffectiveExternalId” and “externalId” (if it’s a whole new field).

Thank you so much @Thomas Schreiner . It's a good info. Just realised now to check the meta model to confirm before trying to do anything. May I know how I can get the Meat model where I can access it. if it is something I need to check with my Admin? or technical user can check it and do the corrections?

 

yes its whole new field that I am trying to create in the factsheets. But while going through the docs I come to know that it should be done through externalId. Correct me if I am wrong. 

 

Userlevel 5
Badge +2

Hi @comm_e2e, yes, it’s possible to check the data model without involving the admin.

The easiest way to see what is going on is the following:

Alternatively, you could ask your admin the following questions:

1.) Where does the field “Uffective ID” for the fact sheet type “Project” come from? Did they just change the label of an existing field such as “Alias” or “External ID”, or has it been added as a new field? What is the ID of the field?

2.) If it’s a new field: What is the data type? Did they create it as a text field, or as a real external ID, which is only possible with LeanIX CSM?

Hello @Thomas Schreiner  Thank you for the message. I have figured it out the UffectiveId add through the patch. I was trying to add as externalid. But this is not externalid field rather its a field with in the project. I will follow this suggestion to check the way query details through developer tools. Thank you so much. 

Reply