You need to sign in to do that
Don't have an account?
trigger help: populating Account Name in Opportunity
Hello,
I have a trigger that inserts a new Opportunity whenever a campaign member is selected for a certain campaign type. The difficulty I am having is in getting the Account name into the Account field on Opportunity. I can seem to only get the ID option to work correctly, but that is not what I want - here is the code:
trigger createOpportunityCampaignMember on CampaignMember (after insert)
{
date myDate =
date.today();
date newDate = mydate.addDays(180);
List<Opportunity> op = new List<Opportunity>();
for (CampaignMember newCampaignMember: Trigger.New)
if (newCampaignMember.RecordTypeID == '01250000000Dtga')//record type for marketing campaign member
{
op.add (
new Opportunity
(
AccountId = newCampaignMember.Contact.AccountId,
Name = newCampaignMember.Contact.AccountId + ' Marketing Lead',
RecordTypeID = '01250000000DcJk', //BDS Leads
StageName = 'Submit Lead for Verification',
CloseDate = newDate,
Campaign = newCampaignMember.Campaign
)
);
insert op;
}
Got asked to create opp when campaing member added to certain campaign type - This is what worked:
trigger createOpportunityCampaignMember on CampaignMember (after insert)
{
date myDate =
date.today();
date newDate = mydate.addDays(180); //used to create follow-up date
Contact Con;
Account Acc1;
Campaign Cam;
for (CampaignMember CM: Trigger.New)
{
if (CM.RecordTypeId == '01250000000Duea') // certain campaign member type
{
if (CM.ContactId!=NULL)//allows leads to be added without creating opportunity
{
Con = [select AccountId from contact where id=:CM.ContactId];
Acc1 = [select name from account where id=:con.AccountId];
Cam = [select name from campaign where id=:CM.CampaignId];
}
}
}
List<Opportunity> op = new List<Opportunity>();
for (CampaignMember newCampaignMember: Trigger.New)
if (newCampaignMember.RecordTypeId == '01250000000Duea')//record type for certain Marketing Campaign Member
{
if (newCampaignMember.ContactId!=NULL)
{
op.add (
new Opportunity
(
AccountId = Acc1.id,
Name = newCampaignMember.Account__c + ' Marketing Lead',
RecordTypeID = '01250000000DcJk', // Leads type being created
StageName = 'Submit Lead for Verification',
CloseDate = newDate,
CampaignId = Cam.id
)
);
system.debug('opp:'+op);
insert op;
}
}
}
All Answers
By setting the ID in code, the name will show up in the Account field in the UI.
Why are you wanting to set the account name in code?
I do not want to - just using it as an example - I want the name
Oh, now I see what you meant. Sorry, I misunderstood the first time.
I'll get back to you in a bit.
Please try this. this is not tested although
Also i will advice you to remove the insert statement from within the for loop as you will hit limits in this case.
Got asked to create opp when campaing member added to certain campaign type - This is what worked:
trigger createOpportunityCampaignMember on CampaignMember (after insert)
{
date myDate =
date.today();
date newDate = mydate.addDays(180); //used to create follow-up date
Contact Con;
Account Acc1;
Campaign Cam;
for (CampaignMember CM: Trigger.New)
{
if (CM.RecordTypeId == '01250000000Duea') // certain campaign member type
{
if (CM.ContactId!=NULL)//allows leads to be added without creating opportunity
{
Con = [select AccountId from contact where id=:CM.ContactId];
Acc1 = [select name from account where id=:con.AccountId];
Cam = [select name from campaign where id=:CM.CampaignId];
}
}
}
List<Opportunity> op = new List<Opportunity>();
for (CampaignMember newCampaignMember: Trigger.New)
if (newCampaignMember.RecordTypeId == '01250000000Duea')//record type for certain Marketing Campaign Member
{
if (newCampaignMember.ContactId!=NULL)
{
op.add (
new Opportunity
(
AccountId = Acc1.id,
Name = newCampaignMember.Account__c + ' Marketing Lead',
RecordTypeID = '01250000000DcJk', // Leads type being created
StageName = 'Submit Lead for Verification',
CloseDate = newDate,
CampaignId = Cam.id
)
);
system.debug('opp:'+op);
insert op;
}
}
}
This solution should be optimized as you're executing 3 SOQL queries within a for-loop.
That will run into the SOQL query governor exception if your trigger size ever gets above 33.
you can optimize it by doing one large query and moving your records into data structures that can then be used to retrieve the records you need without performing queries.