2

我目前有一个自定义对象,我们称这个对象为 myObject。

该对象具有以下字段:

  • 制作 - 文字(20)
  • 模型 - 文本(20)
  • 开始时间 - 日期/时间
  • 结束时间 - 日期/时间
  • 国家 - 文本(20)
  • objContact - 查找(联系人)*

*注意:触点是在插入触发器之前设置的。

要求是创建一个应用程序以允许用户创建多个 myObject 并为每个对象附加一个图像(最多 1MB)。

注意:我已经使用富文本区域字段将图像推送到 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 对象创建一个新的 myObject。但是,由于临时 myObj 尚未插入 SFDC,因此它没有 id。当分配 myAttachment.ParentId 时,myObj.id 为空。

该函数将使用 myCars 列表中的 1 - 2000 个对象调用,因此不接受非散装方法。

有没有办法在插入操作期间将附件的 ParentId 字段设置为 myObj SFID?

我能想到的唯一方法是在每个 MyObj 上创建一个临时 ID(日期时间 + GUID),并将 Attachment.Name 设置为该临时 ID(日期时间 + 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);
    }
}
4

1 回答 1

2

您不能一次调用,但可以将其拆分为仅 2 个,一个插入 Cars 列表,一旦获得该结果,您就可以进行第二次调用以插入附件列表。

于 2012-08-22T20:28:25.523 回答