开发者

Salesforce upsert failing on an INVALID_FIELD error

I'm using the standard Case object from Salesforce (we have some custom fields but that doesn't appear to be the problem). The Case has a linked subquery to its Comments, replicated in the SOAP API as CaseComments.

My test case (an NUnit test) for upserting records is to query() for one by case number, change something on the Case itself, and upsert that record again. I'm not even adding a new comment or anything. When the upsert call goes out, I get an INVALID_FIELD error stating that CaseComments doesn't 开发者_高级运维exist on the Case table in the cloud. Of course it doesn't exist, and the API should know better. Or maybe I'm not telling it something. Help?

Relevant code:

[Test]
public void TestUpsertOfExistingRecord()
{
    var existingCase = sfRepository.GetCaseByNumber("00235452");

    var siteState = existingCase.Site_State__c;

    existingCase.Site_State__c = "Arkansas";

    //Tried the below line; the API didn't even recognize my Case as a valid object
    //existingCase.CaseComments = null;

    sfRepository.Save(existingCase);

    ...
}

//In the repository
public void Save<T>(T unboundInfo) where T : sObject
{
    service.Upsert(new sObject[]{unboundInfo});
}

//in a wrapper for the websevice
public void Upsert(sObject[] unboundInfo)
{
    //webService is the actual SOAP service client created by VS
    webService.upsert(unboundInfo.First().Id, unboundInfo);
}

UPDATE: As per some suggestions, instead of using the Case instance produced by the query, I am creating a new Case. That is failing with permission errors, and as the field I was trying to modify was a projection from another related object (the Account's address) it's understandable. I'll keep banging away at it; in the meantime, can anyone tell me why creating a new Case with the same information works, when cleaning out all related queries of a retrieved Case does not?


The first parameter to the upsert call should be the name of the ID field, not its value, so if you change your upsert method to be

//in a wrapper for the websevice
public void Upsert(sObject[] unboundInfo)
{
    //webService is the actual SOAP service client created by VS
    webService.upsert("Id", unboundInfo);
}

I think it should work. If your case number is an external ID (and marked as such in salesforce) you should be able to use that instead of "Id".

See the salesforce.com upsert docs for more info.


metadaddy's answer is correct, but I thought it might be useful to point out that the upsert method should really only be used if you need salesforce to determine whether to create the record(s) in question or just update them. In this case you already know the ID of the record since you queried for it, so you might as well just do an update.

This way your operation will be slightly faster since you're saving salesforce from having to do a lookup to see if your record exists or not. And you can be a good developer citizen by not wasting resources. :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜