Updating Campaign Member using Salesforce API and C#

Well, if you check at Salesforce API documentation and follow their tutorial to connect to SF database and request data, everything should be fine. The API its pretty flexible and solid. But, once you get to the update point you may find some problem…

I connected to Salesforce and requested my CampaignMember, loading it into an object called “CampaignMember” (yes, im so original). Then, if I tried to update it using something like:

Salesforce.SaveResult[] saveResults = sf.update(new Salesforce.sObject[] { CampaignMember });

It would not work, since it seems that object has every field filled and would try to update all of them, including the read-only ones. So I would receive a saveResults object with the error message “Unable to create/update fields: CampaignId, HasResponded, LeadId. Please check the security settings of this field and verify that it is read/write for your profile“. And that’s why instead of using the loaded object you need to create a new one, set the value you want to update and update just that field.

Then, not reading properly last message, you may think as I did to create a new object (let’s say uCampaignMember) and set uCampaignMember.HasResponded = true; but unfortunately in my case, that field was read-only again. Ok, now I’m stock. How can I update that now if its read-only??? Luckily after testing here and there I found out you have to update its Status instead, doing something like this:

            if (CampaignMember == null) {
                return false;
            } else {
                Connect();

                try {
                    Salesforce.CampaignMember uCampaignMember = new Salesforce.CampaignMember();
                    uCampaignMember.Id = CampaignMember.Id;
                    uCampaignMember.Status = "Responded";
                    Salesforce.SaveResult[] saveResults = sf.update(new Salesforce.sObject[] { uCampaignMember });
                    if (saveResults[0].success) {
                        return true;
                    } else {
                        return false;
                    }
                }
                catch (Exception ex) {
                    throw (ex);
                }
            }

And that will work. If you want to set the campaign member to “not responded” again, just set Status = “Sent”.

Conclusions

When sending an object to be updated it will try to update all the fields the object has filled using its id for the where clause. So, if you read an object from db, modify some of its properties and use that same object to update it on the db it will try to update every field, including the read-only ones, and that will not work.

To update an object you will need then, to create a new one, fill in only the fields you want to update and send it to be updated. Taking care not to fill any read-only field.

Finally, if you are trying to update a Campaign Member to set that it has responded to an email campaign, instead of updating HasResponded = true, as you may think would be the logic, since HasResponded is read-only you will have to update its status instead, like in the code above.

Leave a Reply

Close Bitnami banner
Bitnami