2016-03-15 71 views
0

我正在使用Salesforce和force.com API和元數據API 36版。 我可以在Lead對象中創建自定義字段,但默認情況下我可以看到它隱藏,這意味着我不能用這些自定義字段創建新的潛在客戶,因爲它返回一個錯誤的請求(400狀態碼)。 代碼有沒有任何方法來設置自定義字段可見?設置字段從Java代碼訪問自定義Salesforce Lead字段的可訪問性

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; 
} 

我在Google上搜了很多,沒有找到真正有用的東西。所以,歡迎提供任何提示。謝謝。

+0

不知道這是否與這篇文章有關:https://developer.salesforce.com/forums/?id=906F0000000AY54IAG。我不想更改整個配置文件,只能更改Salesforce對象中的自定義字段的可見性。 – user1140656

回答

0

終於找到了解決辦法。我最後一個是爲了讓所有自定義字段都是必需的。

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; 
    } 

所以它會出現在頁面佈局。非常重要的是要知道,必填字段不能只有一個空值集,它必須是某種東西。因此,如果不是所有的自定義字段都是您的邏輯所必需的,並且您希望避免將解壓縮頁面佈局並將其拉回(但可能會完成)的整個過程,只需在代碼中添加「N/A」或任何字符但不是你的項目自定義字段。

我設法讓自定義字段級別安全性對於「管理員」配置文件可見,但不能將字段可訪問性設置爲可見。後者未經測試。