通过salesforce API创建记录时出错

时间:2015-12-17 15:02:13

标签: java salesforce

运行以下代码时,

private static void createAccounts() {

    Itil_b__Incident__c[] records = new Itil_b__Incident__c[5];

    try {

      // create 5 test accounts
      for (int i=0;i<1;i++) {
        Itil_b__Incident__c a = new Itil_b__Incident__c();
        a.setCurrencyIsoCode("USD");
        a.setIsDeleted(Boolean.FALSE);
        a.setItil_b__Age_Minutes__c(29d);
        a.setHas_Managed_Services__c(Boolean.FALSE);
        a.setItil_b__Priority_from_ITIL_Matrix__c("Medium");
        a.setItil_b__Priority__c("Medium");
        a.setItil_b__Description__c("Testing API Descrition");
        a.setRank_Zero__c(Boolean.FALSE);
        a.setItil_b__Close_With_Problem__c(Boolean.FALSE);
        a.setItil_b__Subject__c("Test from API");
        a.setItil_b__Origin__c("Phone");
        a.setItil_b__Age__c("29 Days");
        a.setItil_b__Status__c("New");
        a.setAssigned_for_Today__c(Boolean.FALSE);
        a.setItil_b__Urgency__c("P4 - Low");
        a.setItil_b__Category__c("Request");
        a.setItil_b__Impact__c("Low");
        a.setSeverity__c("Sev4");   
        records[i] = a;
      }

      // create the records in Salesforce.com
      SaveResult[] saveResults = connection.create(records);

      // check the returned results for any errors
      for (int i=0; i< saveResults.length; i++) {
        if (saveResults[i].isSuccess()) {
          System.out.println(i+". Successfully created record - Id: " + saveResults[i].getId());
        } else {  
          Error[] errors = saveResults[i].getErrors();
          for (int j=0; j< errors.length; j++) {
            System.out.println("ERROR creating record: " + errors[j].getMessage());
          }
        }    
      }

    } catch (Exception e) {
      e.printStackTrace();
    }    

  }

我在saveResults行中收到以下错误,

[InvalidSObjectFault [ApiQueryFault [ApiFault  exceptionCode='INVALID_TYPE'
 exceptionMessage='Must send a concrete entity type.'
]
 row='-1'
 column='-1'
]
]

我正在通过Java API在自定义sObject [Itil_b__Incident__c]中创建一条新记录,你能帮我解决一下我可能缺少的东西吗?

1 个答案:

答案 0 :(得分:0)

检查你的for循环。您目前有for (int i = 0; i < 1; i++) {您的评论说您要创建5个对象,因此它应该是for (int i=0; i<5; i++) {

另外,我不熟悉Java API - 我使用的是Python API。通常在我的情况下,我需要将对象序列化为JSON,因为我使用的是REST API。 Java API是否会在create方法中自动为您执行JSON序列化?你的Itil_b__Incident__c对象是否有一个返回JSON字符串的toJson()方法?