4

This is my first attempt at an insert trigger :) Any help is greatly appreciated!

Scenario: If campaign member status gets updated, insert a record on a custom object (Registration).

Code:

trigger Insert_Registration_on_member_status_change on CampaignMember (after update) {
List<miiEventV1__Registration__c> reg = new List<miiEventV1__Registration__c>();
    for (miiEventV1__Registration__c c: Trigger.new)
         if (CampaignMember.Status != null){
                 reg.add (new miiEventV1__Registration__c(
                     miiEventV1__Client__c = CampaignMember.Account_ID__c,
                     miiEventV1__Presentation__c = CampaignMember.Related_Presentation__c,
                     miiEventV1__Presentation_Ticket_Type__c =  CampaignMember.Create_Ticket_Id__c,
                     miiEventV1__Status__c = 'Registered',
                     miiEventV1__Active__c = TRUE));   
         }
   insert reg;
}

Questions:

1) I copied and modified this code from a tutorial, is this line formatted correctly?

    for (miiEventV1__Registration__c c: Trigger.new)

2) I keep getting the error message "Invalid initial expression type for field miiEventV1_Registration_c.miiEventV1_Client_c, expecting: Id". This would apply for the Client_c and Presentation_c field as well. Those are all formula fields pulling the record Id value from the related records. Even if I change them to "miiEventV1_Client_c = CampaignMember.Contact.Account.Id" etc, I still get the same error message. What do I need to do to feed those fields as values for the new inserted record?

Thank you in advance for your replies!

Regards,

Robin

5

Trigger.new in this context is going to be a collection of CampaignMember objects rather than miiEventV1__Registration__c objects.

The references in your code that are CampaignMember.Account_ID__c, etc. (that explicitly use CampaignMember) are actually retrieving information about that field on the object definition rather than a record of that type of object.

You'll want to change your code to be similar to the following:

for (CampaignMember cm : Trigger.new) {
  if (cm.Status__c != null) {
    reg.add(new miiEventV1__Registration__c(miiEventV1__Client__c = cm.Account_ID__c)));
    ...
  }
}
  • Dear Michael, Thank you for your prompt response! I modified the code as you suggested and was able to save it without error messages! I am now stuck on writing the test class, but will open a new topic for it. Much appreciated! – Robin Koehler Feb 3 '14 at 1:15

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.