我目前有一個自定義對象,我們來調用這個對象myObject。在使用SalesForce Soap API插入之前,是否可以將附件添加到自定義對象?
對象具有以下字段:
- 製作 - 文本(20)
- 模式 - 文本(20)
- 開始時間 - 日期/時間
- 結束時間 - 日期/時間
- Country - Text(20)
- objContact - Lookup(Contact)*
*注意:聯繫人是從插入前觸發器設置的。
要求是創建一個應用程序,允許用戶創建多個myObjects並將圖像附加到每個(最大1MB)。
注意:我已經使用Rich Text Area字段將圖像推送到myObject,但是這種方法限於32kb。
我現在用的是Salesforce的SOAP API(C#)用下面的代碼(使用清晰度聯方法):
public void Create(List<Cars> myCars)
{
SforceService sforceService = new SforceService() { Timeout = 60000 };
LoginResult result = sforceService.login(USERNAME, PASSWORD);
if (result.passwordExpired != true)
{
String authEndPoint = sforceService.Url;
sforceService.Url = "result.serverUrl;
sforceService.SessionHeaderValue = new SessionHeader();
sforceService.SessionHeaderValue.sessionId = result.sessionId;
sObject[] myObjectArr = new sObject[myObjects.Count * 2];
for (int i = 0, carCount = myCars.Count; i < carCount; i++)
{
myObject__c myObj = new myObject__c();
myObj.Make__c = myCars[i].make;
myObj.Model__c = myCars[i].model;
myObj.StartTime__c = myCars[i].startTime;
myObj.EndTime__c = myCars[i].endTime;
myObj.Country__c = myCars[i].country;
//Convert Image to Byte[]:
byte[] imageBytes = new byte[];
using (MemoryStream ms = new MemoryStream())
{
myCars[i].Image.Save(ms, ImageFormat.Jpeg);
imageBytes = ms.ToArray();
}
Attachment myAttachment = new Attachment();
myAttachment.Body = imageBytes;
myAttachment.ContentType = "image/jpeg";
myAttachment.Name = "FileName";
myAttachment.ParentId = myObj.id;
myObjectArr[i] = myObj;
myObjectArr[i + myCars.Count] = myAttachment;
}
sforceService.create(myObjectArr);
}
}
代碼正確執行,創建傳遞給函數每輛車對象的新myObject的。但是,由於臨時myObj尚未被插入到SFDC中,因此它沒有id。當myAttachment.ParentId被賦值時,myObj.id爲null。
該函數將在myCars列表中使用1 - 2000個對象進行調用,因此非批量化方法將不被接受。
在插入操作期間,有什麼辦法可以將附件的ParentId字段設置爲myObj SFID嗎?
我能想到的唯一方法是在每個MyObj上創建一個臨時id(datetime + GUID),並將Attachment.Name設置爲該臨時id(datetime + GUID),然後創建一個觸發器以將兩者插入後。有沒有更好的方法來解決這個問題?
在此先感謝。
編輯: 這裏是什麼工作:
public void Create(List<Cars> myCars)
{
SforceService sforceService = new SforceService() { Timeout = 60000 };
LoginResult result = sforceService.login(USERNAME, PASSWORD);
if (result.passwordExpired != true)
{
String authEndPoint = sforceService.Url;
sforceService.Url = "result.serverUrl;
sforceService.SessionHeaderValue = new SessionHeader();
sforceService.SessionHeaderValue.sessionId = result.sessionId;
List<string> GUIDList = new List<string>();
List<Attachment> AttachmentList = new List<Attachment>();
sObject[] myObjectArr = new sObject[myObjects.Count];
sObject[] myObjectArr_Attachments = new sObject[myObjects.Count];
for (int i = 0, carCount = myCars.Count; i < carCount; i++)
{
string tempGUID = Guid.NewGuid();
GUIDList.add(tempGUID);
myObject__c myObj = new myObject__c();
myObj.Make__c = myCars[i].make;
myObj.Model__c = myCars[i].model;
myObj.StartTime__c = myCars[i].startTime;
myObj.EndTime__c = myCars[i].endTime;
myObj.Country__c = myCars[i].country;
myObj.GUID__c = tempGUID;
//Convert Image to Byte[]:
byte[] imageBytes = new byte[];
using (MemoryStream ms = new MemoryStream())
{
myCars[i].Image.Save(ms, ImageFormat.Jpeg);
imageBytes = ms.ToArray();
}
Attachment myAttachment = new Attachment();
myAttachment.Body = imageBytes;
myAttachment.ContentType = "image/jpeg";
myAttachment.Name = tempGUID;
AttachmentList.add(myAttachment);
myObjectArr[i] = myObj;
}
sforceService.create(myObjectArr);
string GUIDListString = "(";
for (int i = 0, listLen = GUIDList.Count; i < listLen; i++)
{
if (i < GUIDList.Count - 1)
{
GUIDListString += string.Format("'{0}', ", GUIDList[i]);
}
else
{
GUIDListString += string.Format("'{0}')", GUIDList[i]);
}
}
string queryString = String.Format("SELECT id, GUID__c FROM myObj__c WHERE GUID__c IN {0}", GUIDListString);
QueryResult myObjResult = sforceService.query(queryString);
sObject[] sObjQuery = myObjResult.records;
List<myObject__c> myObjs = new List<myObject__c>();
foreach(sObject sObj in sObjQuery)
{
myObjs.Add((myObject__c)sObj);
}
for (int i = 0, attLen = AttachmentList.Count; i < attLen; i++)
{
foreach (myObject__c obj in myObjs)
{
if(AttachmentList[i].Name == obj.GUID__c)
{
AttachmentList[i].ParentId = obj.Id;
sObjArr_Attachments[i] = AttachmentList[i];
}
}
}
SaveResult[] sr_att = sforceService.create(sObjArr_Attachments);
}
}
謝謝,我不得不做以下幾點:1.創建GUID 2.創建帶有GUID的myObj 3.創建附件名稱= Guid 4.將GUID添加到Guid列表5.插入myObj列表5. SOQL myObj IN GuidList 6.爲每個附件添加結果ID 7.插入附件。 – FEXTWOLF
創建調用返回的保存結果包含新創建記錄的記錄標識,因此您可以將其與附件記錄一起使用,您不需要其他所有步驟。 – superfell
謝謝,我會檢查出 – FEXTWOLF