I haven't been in the garden for a long time. One is busy with work, but some trivial matters in my life make me unable to "idle". Recently I have just finished some projects, and I have been idle for some time, when I visited the garden, I suddenly had the urge to write a blog.
I have been working for four years since I was just working. I have been working on development and system integration based on the salesforce platform. There have been some "accumulation" on this platform over the years. In the future, I may write some blogs, technologies, or complaints about this platform.
I have recently created a project to read and update an xml file containing the static resource of salesforce. This xml file maintains the product and service information of a company. You have to download the file each time and edit it manually. Because the file is large and may not be easy to manage and edit, you need to use VF Page to read the XML file and display it on the Page according to the node data format. After editing, update the xml file.
I used to think that the content in static resource cannot be changed by code. Later I checked some documents and used some tools to intercept some http requests. I found that we can build an httprequest, update the xml file after the salesforce post operation.
The overall structure is: two entity classes XmlNode and XmlDocument are used to store some xml Information. A Helper class is used to parse/read XML files, there is also a page background controller and a VF page to display and edit xml files.
The key and difficulty of the Project is how to update the xml file. You can update the xml file by studying the xml string below salesforce post:
// Uploades the updated xml file to static resource private static Boolean updateXml(String newXMLStr, String serverUrl) { Boolean result = false; newXMLStr = EncodingUtil.base64Encode(Blob.valueOf(newXMLStr)); // the meta data used to post to salesforce to update static resource String metaData = '<?xml version="1.0" encoding="utf-8" ?>'+ '<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">'+ '<soap:Header>'+ '<SessionHeader xmlns="http://soap.sforce.com/2006/04/metadata">'+ '<sessionId>' + UserInfo.getSessionId() + '</sessionId>'+ '</SessionHeader>'+ '</soap:Header>'+ '<soap:Body>'+ '<update xmlns="http://soap.sforce.com/2006/04/metadata">'+ '<UpdateMetadata>'+ '<currentName>' + resourceName + '</currentName>'+ '<metadata xsi:type="StaticResource">'+ '<fullName>' + resourceName + '</fullName>'+ '<content>' + newXMLStr + '</content>'+ '<cacheControl>Public</cacheControl>'+ '<contentType>text/xml</contentType>'+ '</metadata>'+ '</UpdateMetadata>'+ '</update>'+ '</soap:Body>'+ '</soap:Envelope>'; HttpResponse response = requestForResponse(metaData, serverUrl); // if success, return the response body if(response.getStatusCode() == 200) { result = true; } return result; }