首页 > 代码库 > 010_Soap update

010_Soap update

最近有一个需求,是从数据库中取数据,同步到系统的Currency上。

我在使用Enterprise jar 时候总是提示Invalid—Type的错误,各种看大神的文章发现,针对这个问题,我们要采用Partner jar,如果这个不知道怎么生成,请参考SOAP手册。

连接url =/services/Soap/u/38.0"

放下代码,大家参考下:以下是最终版本,之前遇到的错误是:

com.sforce.ws.SoapFaultException: A duplicate value was specified for field ‘Id‘ in object ‘DatedConversionRate‘,  duplicate value ‘04wO00000004K7OIAU‘ prior value ‘04wO00000004K7OIAU‘    Debug跟进去发下Id有两个,这个比较奇怪,后来加上一句s.setField("Id", null); 就Ok了

try {
    connection = Connector.newConnection(config);
    QueryResult queryResults = connection.query("SELECT Id, Username, IsActive from User");

    if ( queryResults.getSize() > 0 ) {
        // keep track of which records you want to update with an ArrayList
        ArrayList<SObject> updateObjects = new ArrayList<SObject>();
        for (SObject s : queryResults.getRecords()) {
            if ( s.getField("Username").equals("abcd@pqrs.com") ){
                System.out.println("Username: " + s.getField("Username"));
                s.setField("Id", null);
                s.setField("IsActive", false);
            }
            updateObjects.add(s);    // if you want to update all records...if not, put this in a conditional statement
            System.out.println("Username: " + s.getField("Username") + " IsActive: " + s.getField("IsActive"));
        }
        // make the update call to Salesforce and then process the SaveResults returned
        SaveResult[] saveResults = connection.update(updateObjects.toArray(new SObject[updateObjects.size()]));
        for ( int i = 0; i < saveResults.length; i++ ) {
            if ( saveResults[i].isSuccess() )
                System.out.println("record " + saveResults[i].getId() + " was updated successfully");
            else {                        
                // There were errors during the update call, so loop through and print them out
                System.out.println("record " + saveResults[i].getId() + " failed to save");
                for ( int j = 0; j < saveResults[i].getErrors().length; j++ ) {
                    Error err = saveResults[i].getErrors()[j];
                    System.out.println("error code: " + err.getStatusCode().toString());
                    System.out.println("error message: " + err.getMessage());
                }
            }
        }
    }
} catch (ConnectionException ce) {
        ce.printStackTrace();
}

  

try{
    connection =Connector.newConnection(config);QueryResult queryResults = connection.query("SELECT Id, Username, IsActive from User");if( queryResults.getSize()>0){// keep track of which records you want to update with an ArrayListArrayList<SObject> updateObjects =newArrayList<SObject>();for(SObject s : queryResults.getRecords()){if( s.getField("Username").equals("abcd@pqrs.com")){System.out.println("Username: "+ s.getField("Username"));
                s.setField("Id",null);
                s.setField("IsActive",false);}
            updateObjects.add(s);// if you want to update all records...if not, put this in a conditional statementSystem.out.println("Username: "+ s.getField("Username")+" IsActive: "+ s.getField("IsActive"));}// make the update call to Salesforce and then process the SaveResults returnedSaveResult[] saveResults = connection.update(updateObjects.toArray(newSObject[updateObjects.size()]));for(int i =0; i < saveResults.length; i++){if( saveResults[i].isSuccess())System.out.println("record "+ saveResults[i].getId()+" was updated successfully");else{// There were errors during the update call, so loop through and print them outSystem.out.println("record "+ saveResults[i].getId()+" failed to save");for(int j =0; j < saveResults[i].getErrors().length; j++){Error err = saveResults[i].getErrors()[j];System.out.println("error code: "+ err.getStatusCode().toString());System.out.println("error message: "+ err.getMessage());}}}}}catch(ConnectionException ce){
        ce.printStackTrace();}

010_Soap update