010_Soap update
Posted BandariFang
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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("[email protected]") ){ 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("[email protected]")){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的主要内容,如果未能解决你的问题,请参考以下文章
;010_跟随Windows7一起启动的AutoHotkey全自动化热键脚本源代码(长期实际测试完美进化版)
免费下载全套最新010-2Spring jpa分页整合视频教程+教学资料+学习课件+源代码+软件开发工具