0

Also new at APEX and this is my first crack at it. I want to update some fields on the CampaignMember object when I catch a Webhook on a force.com site. I have the Webhook working, however I am having issues updating a record in the object.

@RestResource(urlMapping='/myservice')

global class MyService {

@HttpGet

global static String doGet() {

String campId = RestContext.request.params.get('campId');
String email = RestContext.request.params.get('email');
String medium = RestContext.request.params.get('medium');
String source = RestContext.request.params.get('source');

String queriedCampaign = [SELECT ID FROM CampaignMember WHERE CampaignMember.CampaignID = :campId AND CampaignMember.Email = :email].ID;

CampaignMember.queriedCampaign.Medium__c = medium;
queriedCampaign.CampaignMember.Source__c = source;

update queriedCampaign.CampaignMember;  

}
}

I believe my query is correct, but struggling with the proper syntax to update the record. Thanks in advance for helping a newbie!

2

Querying A Record

To update a record, you must first have a copy of a record in memory. This means you need a CampaignMember object to update:

CampaignMember queriedCampaign = [SELECT ID FROM CampaignMember WHERE CampaignID = :campId AND Email = :email LIMIT 1];

Assignment

In general, you declare and assign values in the form of:

DataType variableName [= someValue];

Where [= someValue] is optional.

Examples:

Integer x = 5;
Integer y; // Has no value, a "null" value

Query Fields

For queries, you don't generally use the Object.Field syntax, unless referring to a parent object (see query updated, above).

Incorrect

SELECT CampaignMember.Email FROM CampaignMember 

Correct

SELECT Email FROM CampaignMember
SELECT Campaign.Name FROM CampaignMember

Assignment (again)

Then, you can assign the values to that record:

queriedCampaign.Medium__c = medium;
queriedCampaign.Source__c = source;

Notice how this is in the form of:

variable.property = valueToAssign;

For simple variables, such as String or Integer, the assignment is in the form of:

variable = valueToAssign;

Example:

Integer x = 5;
Integer y;
y = x * 2; // Multiply x times 2, and place that value in y. x is still 5.

DML Operations

Finally, to update the record, you:

update queriedCampaign;

Generically, DML is in the form of:

operation recordOrRecordList;

You'll find all of this in the documentation and on the Apex trailhead (and other trails there you can follow for free!).

  • Thanks so much! Jumping into Trailhead now. You = The Man. – Mktg_ops Jan 14 at 2:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.