zoukankan      html  css  js  c++  java
  • 010_Soap update

    SOAP API:For example, you can use SOAP API to integrate Salesforce with your org’s ERP and finance systems. 

    简介:SOAP API is a powerful web service. It uses a Web Services Description Language(WSDL) file to rigorously define the parameters for accessing data through the API. 

    支持格式:SOAP API supports XML only. 

    备选方案:Most of the SOAP API functionality is also available through REST API. It just depends on which standard better meets your needs.

    应用优势:Because SOAP API uses the WSDL file as a formal contract between the API and consumer, it’s great for writing server-to-server integrations.

    WSDLs及分类:Web Services Description Language, It contains the bindings, protocols, and objects to make API calls. | Enterprise(strongly typed | integration for a single Salesforce org) and Partner(loosely typed | For several orgs) WSDLs

    /c—Specifies that we’re using the enterprise WSDL. Use /u for the partner WSDL.

     

    因此顺序应该是:外部service->wsdl->to Apex->Apex Invoke


    Tools:We’re using a third-party tool called SoapUI to consume our enterprise WSDL file. SoapUI is a free and open-source app for testing web services.

    登录时password需要password+security_token e.g. 

    <urn:login>

             <urn:username>username</urn:username>

             <urn:password>password+security_token</urn:password>

     </urn:login>

    最近有一个需求,是从数据库中取数据,同步到系统的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();}
    此刻,静下心来学习
  • 相关阅读:
    关于HashMap的线程安全问题
    Java利器之UML类图详解
    mongoDB4.0数据库
    requests-html库render的使用
    爬虫最新的库requests-html库总结
    爬虫多次爬取时候cookie的存储用于登入
    requests模块响应体属性和方法重新整理
    Visual Studio 代码补全功能有时候会失效的原因
    C++Primer笔记——文本查询程序(原创,未使用类)
    Clion 常用快捷键
  • 原文地址:https://www.cnblogs.com/bandariFang/p/6322764.html
Copyright © 2011-2022 走看看