Trigger CreateList on Campaign ( after insert ) {
List<Opportunity> opp = new List<Opportunity>( );
List <OpportunityContactRole> ocrole = new List<OpportunityContactRole>( );
public integer iOPPCount {get; set;
}
public integer icount {get; set;
}
for( Campaign camp : trigger.new ){
if( camp.Create_List__c == True ){
String sCamp= String.valueof( camp.id );
iOPPCount = [SELECT count( ) FROM CampaignMember WHERE CampaignId =:sCamp];
if ( iOPPCount > 0 ){
// camp.addError( String.Valueof( iOPPCount ) );
List<CampaignMember> camps = [SELECT id,FirstName,LastName,CompanyOrAccount,ContactId FROM CampaignMember WHERE CampaignId =:sCamp];
for( CampaignMember CM : camps ){
String sEvent = String.valueof( camp.EventIDName__c );
String sName = String.valueof( CM.FirstName ) + ' ' + String.valueof( CM.LastName );
icount = [SELECT count( ) FROM Opportunity WHERE Campaign__c =:camp.Id and Name=:sEvent +'-' + sName];
if ( icount == 0 ){
// camp.addError( sEvent + '-' + sLast );
//Opportunity op = new Opportunity( Campaign__c=camp.Id,Name=sEvent+ '-' + sName,CloseDate=camp.EndDate,StageName='Prospecting' );
Opportunity op = new Opportunity(
Campaign__c=camp.Id,
Name=sEvent +'-' + sName,
CloseDate=camp.EndDate,
StageName='Prospecting',
Contact__c= cm.ContactId );
opp.add( op );
}
insert opp;
for( Opportunity ops : opp ){
string sOppId = String.Valueof( ops.get( 'id' ) );
string sConId = String.Valueof( ops.get( 'Contact__c' ) );
// camp.addError( String.Valueof( opp[0].id ) );
OpportunityContactRole ocr = new OpportunityContactRole(
OpportunityId = sOppId,
//ContactId = cm.ContactId,
ContactId = sConId,
IsPrimary = TRUE );
ocrole.add( ocr );
}
insert ocrole;
// insert ocrole;
}
}
else if( iOPPCount < 0 ){
//do nothing
}
/* {
List<Contact> OPPList =[Select Name from Contact];
for ( Contact OPP : OPPList )
{
String sOPPList= String.Valueof( OPP.get( 'Name' ) );
OOpportunity o = new Opportunity( Campaign = camp.id, Name=camp.Name + '-' + sOPPList ,Contact__c =camp.Contact__c );
ot.add( o );
}
insert ot;
}
*/
}
}
}