0
votes

I am working around with Salesforce and force.com API and metadata API, version 36. I can create a custom field in a Lead object but by default I can see it's hidden and this means I cannot create a new Lead with these custom fields because it returns a bad request (400 status code). Is there any way by Code to set the custom field Visible?

public boolean createCustomExtTextField(String name, LoginResult metadataLoginResult, int length) {
    boolean success = false;
    CustomField cs = new CustomField();
    cs.setFullName("Lead."+name+"__c");
    cs.setLabel("Custom"+name+"Field");
    cs.setType(FieldType.LongTextArea);
    cs.setLength(length);
    cs.setVisibleLines(50); // max 50

    try {
        MetadataConnection metadataConnection = createMetadataConnection(metadataLoginResult);
        SaveResult[] results = metadataConnection.createMetadata(new Metadata[] { cs });

        for (SaveResult r : results) {
            if (r.isSuccess()) {
                success = true;
            } else {
                System.out.println("Errors were encountered while creating " + r.getFullName());
                for (com.sforce.soap.metadata.Error e : r.getErrors()) {
                    System.out.println("Error message: " + e.getMessage());
                    System.out.println("Status code: " + e.getStatusCode());
                }
            }
        }
    } catch (ConnectionException e) {
        e.printStackTrace();
    }
    return success;
}

I am googling a lot and don't find something that actually helped. So, any hints are welcomed. Thank you.

1
not sure if this is related to this post: developer.salesforce.com/forums/?id=906F0000000AY54IAG . I don't want to change an Entire Profile, only visibility of a custom field in a Salesforce object. - Gabi Radu

1 Answers

0
votes

Finally found a solution to this. I final one for me was to make all custom fields REQUIRED.

CustomField cs = new CustomField();
cs.setFullName("Lead.YourCompanyName" + name + "__c");
cs.setLabel("YourCompanyName" + name);
cs.setRequired(true);

...

com.sforce.soap.enterprise.LoginResult metadataLoginResult = operations.loginToMetadata(username, password, "https://login.salesforce.com/services/Soap/c/36.0");

...

private boolean createFieldInMetadata(LoginResult metadataLoginResult, CustomField cs) {
        boolean success = false;
        try {
            MetadataConnection metadataConnection = createMetadataConnection(metadataLoginResult);
            SaveResult[] results = metadataConnection.createMetadata(new Metadata[] { cs });

            for (SaveResult r : results) {
                if (r.isSuccess()) {
                    success = true;
                } else {
                    System.out.println("Errors were encountered while creating " + r.getFullName());
                    for (com.sforce.soap.metadata.Error e : r.getErrors()) {
                        System.out.println("Error message: " + e.getMessage());
                        System.out.println("Status code: " + e.getStatusCode());
                    }
                }
            }
        } catch (Exception e) {
        }
        return success;
    }

And so it will appear in the page layout. Very important to know, a required field cannot have just an empty value set, it must be something. So if not all custom fields are required in your logic and you wanna avoid the entire process of unzipping page layout and zipping it back (however it may be done) just add "N/A" or any char at choice to the required by code but not your project custom fields.

I managed to make the custom Field Level Security visible for "Admin" profile but not Field Accessability to visible. The latter is untested.