You need to sign in to do that
Don't have an account?
Opportunity trigger code issue
trigger campaign_update on Opportunity (after insert,after update) {
//This trigger looks for the first contact in Account, then adds a campaignmember to the proper campaign
Opportunity[] opportunities = Trigger.new;
for (Integer i = 0; i < opportunities .size(); i++) {
Contact contact;
boolean foundContactId = false;
boolean respondedtocampaign = false;
//Verifiy that this record has a related account and a priamry campaign
if (opportunities[i].AccountId != null && opportunities[i].campaign != null) {
//Get related contactID (one and only one contact per account)
try {
contact = [SELECT Id FROM Contact WHERE AccountId = :opportunities[i].AccountId limit 1];
foundContactId = true;
} catch (Exception e){
//Found no contact on this account
}
//Check if related contact already has a a primary campaign
if (foundContactId) {
try {
Campaignmember c = [SELECT campaignID FROM campaignmember WHERE ContactID = : contact.Id AND
CampaignID = :opportunities[i].campaign limit 1];
} catch (Exception e){
//Found no primary campaign
respondedtocampaign = true;
}
}
//If reservation exists for this contact then book it
if (foundContactId && respondedtocampaign) {
// Add a new reservation to related seminar
CampaignMember cm= new CampaignMember(CampaignID = opportunities[i].Campaign,
ContactID=contact.Id,
Status='Responded');
//Insert the new reservation
insert cm;
}
}
}
}
The error is the following :
Error: Compile Error: Invalid bind expression type of SOBJECT:Campaign for column of type Id at line 25 column 117
Any idea why is this error happening ?
All Answers
Yeah, correct thank you :)
Agreed with marked solution and just want to add FYI that Campaign is a standard object so to get id we just simply append Id after Object name like other example
AccountId, OpportunityId, ContactId ....etc
This is an approximate translation of your code to work correctly, even through the data loader. I tested this in my developer edition, and it appears to work. Let me know if this is what you're looking for.
This is the error I am getting
Failure Message: "System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, Campaign_Update: execution of AfterInsert caused by: System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Attempted to add a campaign member where eithe...
@isTest
private class campaign_updateTestclass {
static testMethod void validateContractCloner() {
Opportunity newOpportunity = new Opportunity(Name = 'testOpp',
CloseDate = System.today(),
StageName = 'Providing Information',
AccountId = '001C000000yq2Jw',
CampaignID = '701C0000000e6PF',
Presentation_Request__c='CNC Appointment',
Zip_Code__c='76021',
LeadSource='Direct Mail',
Lead_Type__C='CNC Lead' );
insert newOpportunity;
}
}