我目前有一个自定义对象,让我们调用这个对象myObject。
该对象包含以下字段:
*注意:联系人是从插入前触发器设置的。
要求是创建一个应用程序,允许用户创建多个myObject并将图像附加到每个(最多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);
}
}
代码正确执行,为传递给函数的每个Car Object创建一个新的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);
}
}
答案 0 :(得分:2)
您无法进行一次通话,但您可以将其拆分为2,其中一个插入汽车列表,一旦您获得该结果,您就可以进行第二次调用以插入附件列表。