Here’s a comprehensive solution covering all three focus areas:
CAD-to-PLM Attribute Mapping:
The core issue is incorrect attribute mapping structure. SAP PLM API requires custom attributes in a specific format with attribute IDs, not names. Update your script to query the metadata endpoint and build a proper mapping:
# Get attribute metadata
meta_response = requests.get(f'{base_url}/api/metadata/attributes')
attr_map = {attr['name']: attr['id'] for attr in meta_response.json()}
# Transform CAD attributes to PLM format
custom_attrs = [
{'attributeId': attr_map['MaterialGrade'], 'value': part_data['material']},
{'attributeId': attr_map['SupplierCode'], 'value': part_data['supplier']}
]
API Schema Configuration:
Your payload structure needs to match the BOM item schema exactly. The customAttributes field must be an array of attribute objects, not a dictionary:
payload = {
'partNumber': part_data['number'],
'description': part_data['desc'],
'bomItems': [{
'itemNumber': item['number'],
'customAttributes': custom_attrs # Properly formatted array
}]
}
Also ensure you’re setting the correct content type and API version headers. SAP PLM 2021 requires ‘Content-Type: application/json’ and ‘API-Version: 2.0’ headers for custom attribute processing.
BOM Data Validation:
Implement pre-submission validation to catch issues before they cause silent failures:
- Attribute existence check: Verify all custom attribute IDs exist in the metadata response before building the payload
- Value validation: Query validation rules from the metadata endpoint and check attribute values against allowed ranges, enums, or reference tables
- Data type conversion: Convert CAD string values to required PLM types (numeric codes, date formats, boolean flags)
- Mandatory field check: Ensure all required attributes are present - some custom attributes might be mandatory for certain part types
- Length restrictions: Validate string attribute values against maximum length constraints from the schema
Add comprehensive error handling:
try:
response = requests.post(api_url, json=payload, headers=headers)
response.raise_for_status()
result = response.json()
# Check for partial success
if result.get('warnings'):
log_warnings(result['warnings']) # Attributes might be dropped
except requests.exceptions.HTTPError as e:
# API returned error - log full response for debugging
log_error(f"API Error: {e.response.text}")
Implement a validation report that runs before the actual import. This script should read your CAD file, extract all attributes, validate them against PLM rules, and generate a report of any issues. Only proceed with the import if validation passes completely.
For ongoing monitoring, add logging that captures the full API request and response for each BOM import. This makes it much easier to diagnose issues when attribute mapping rules change or new validation requirements are added in future SAP PLM updates.