certinia / apex-mdapi

Apex Wrapper for the Salesforce Metadata API
BSD 3-Clause "New" or "Revised" License
689 stars 976 forks source link

When using the GlobalValueSet: Invalid color format '' in Color (FIELD_INTEGRITY_EXCEPTION). #207

Open CedricWillems opened 6 years ago

CedricWillems commented 6 years ago

Class Example:

public static void updateGlobalValueSet(String globalValueSetName, List<MetadataService.CustomValue> customValues) {
        MetadataService.MetadataPort service = createService();

        // Read Custom Field
        MetadataService.GlobalValueSet globalValueSet =
                (MetadataService.GlobalValueSet) service.readMetadata('GlobalValueSet',
                        new String[]{
                                globalValueSetName
                        }).getRecords()[0];

        // Add pick list values
        for (MetadataService.CustomValue customValue : customValues) {
            globalValueSet.customValue.add(customValue);
        }

        for (MetadataService.CustomValue customValue : globalValueSet.customValue) {
            if (customValue.default_x == null)
                customValue.default_x = false;
            if (customValue.isActive == null)
                customValue.isActive = true;
            System.debug('customValue: ' + customValue);
        }

        // Update Custom Field
        handleSaveResults(
                service.updateMetadata(
                        new MetadataService.Metadata[]{
                                globalValueSet
                        })[0]);
    }

Anonymous Apex:

MetadataService.CustomValue customValue = MetadataServiceExamples.customValue('Test', false, null, null, true);
List<MetadataService.CustomValue> customValues = new List<MetadataService.CustomValue>();
customValues.add(customValue);

MetadataServiceExamples.updateGlobalValueSet('Recruitment_Label_Types', customValues);

'default' and 'isActive' are 'null', so these are giving an error aswell.

afawcett commented 6 years ago

Can you run this via Developer Console exec anonymous and post the XML shown in the debug log? This also helps narrow the issue down to a Metadata API issue or an issue with this lib.

CousinC commented 6 years ago

I came across the exact same error when I tried to use the updateMetadata call to add a picklist value to an existing field. It seems like it's due to the following line in MetadataService.cls : https://github.com/financialforcedev/apex-mdapi/blob/08656f19002124d841e797fbd37d5a7ee8aa5678/apex-mdapi/src/classes/MetadataService.cls#L1411 The 'color' attribute of CustomValue is not described as optional while the Metadata WDSL describes it as such :

<xsd:complexType name="CustomValue">
    <xsd:complexContent>
        <xsd:extension base="tns:Metadata">
            <xsd:sequence>
                <xsd:element name="color" minOccurs="0" type="xsd:string"/>
                <xsd:element name="default" type="xsd:boolean"/>
                <xsd:element name="description" minOccurs="0" type="xsd:string"/>
                <xsd:element name="isActive" minOccurs="0" type="xsd:boolean"/>
                <xsd:element name="label" minOccurs="0" type="xsd:string"/>
            </xsd:sequence>
        </xsd:extension>
    </xsd:complexContent>
</xsd:complexType>

The following adjustment resolved the issue :

private String[] color_type_info = new String[]{'color','http://soap.sforce.com/2006/04/metadata',null,'0','1','false'};
afawcett commented 6 years ago

Yes this looks like a bug in the Salesforce WSDL is the root cause.

srigottipati commented 4 years ago

Hi,

I am executing below code in Dev Console.Code is executing without any error, but Global picklist is not getting created.

any suggestion? `MetadataService.MetadataPort service = new MetadataService.MetadataPort(); service.SessionHeader = new MetadataService.SessionHeader_element(); service.SessionHeader.sessionId = UserInfo.getSessionId();

String valueSetName = 'test123';

MetadataService.GlobalValueSet globalValueSet = new MetadataService.GlobalValueSet();

MetadataService.CustomValue newItem = new MetadataService.CustomValue(); newItem.fullName = 'value'; newItem.label = 'label'; newItem.isActive = true; newItem.default_x = true;

if(globalValueSet.customValue ==null){ globalValueSet.customValue = new List(); }

globalValueSet.customValue.add(newItem);

globalValueSet.masterLabel='test123'; globalValueSet.sorted=false; globalValueSet.description='sd'; service.createMetadata( new MetadataService.Metadata[]{ globalValueSet }); `

and

errors =null and success =false from saveResult