+ Start a Discussion
anjaanja 

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;

         }

        

        

 

Best Answer chosen by Admin (Salesforce Developers) 
anjaanja

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

spraetzspraetz

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?

anjaanja

I do not want to  - just using it as an example - I want the name

spraetzspraetz

Oh, now I see what you meant.  Sorry, I misunderstood the first time.

 

I'll get back to you in a bit.

raseshtcsraseshtcs

Please try this. this is not tested although

trigger createOpportunityCampaignMember on CampaignMember (after insert)

{

    date myDate =

     date.today();

    date newDate = mydate.addDays(180);
   Set<Id> accID  = new Set<Id>();
   for (CampaignMember newCampaignMember: Trigger.New){
	accID.add(newCampaignMember.Contact.AccountId);   
}
Map <ID,Account> accts = new map <ID,Account>([Select Id,Name from Account where Id IN:accID ]);
	 

    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

                        (
		accountName = accts.get(newCampaignMember.Contact.AccountId).Name,//accountName will be a custom field in my opinion
                            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;

         }

 Also i will advice you to remove the insert statement from within the for loop as you will hit limits in this case.

 

anjaanja

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 was selected as the best answer
spraetzspraetz

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.