+ Start a Discussion
JkkJkk 

getting error of illegal assignment

i am trying to create one to  one relation between campaign and campaign member like one campaign member for one campaign.
below is  my code:
trigger CampaignmemberControl on CampaignMember (before insert) {
    If(trigger.isInsert && trigger.isbefore){
        Set<String> CampaignMember_parentid=new Set<String>();
        for (CampaignMember cc:trigger.new){
            If(cc.Email !=null){
                CampaignMember_parentid.add(cc.CampaignId);
            }
            List <CampaignMember> listCampaign= [SELECT Id, Name,(SELECT Id FROM CampaignMembers) FROM Campaign WHERE Id IN:CampaignMember_parentid];
            for(CampaignMember cnew:trigger.new){
                if(listCampaign.size()>0)
                {
                    cnew.addError('Not Allowed');
                }
                
            }update listCampaign;
        }
    }

}

However i am facing error:  Illegal assignment from List<Campaign> to List<CampaignMember>

Please help me with this
Steven NsubugaSteven Nsubuga
trigger CampaignmemberControl on CampaignMember (before insert) {
    If(trigger.isInsert && trigger.isbefore){
        Set<String> CampaignMember_parentid=new Set<String>();
        for (CampaignMember cc:trigger.new){
            If(cc.Email !=null){
                CampaignMember_parentid.add(cc.CampaignId);
            }
            List <Campaign> listCampaign= [SELECT Id, Name,(SELECT Id FROM CampaignMembers) FROM Campaign WHERE Id IN:CampaignMember_parentid];
            for(CampaignMember cnew:trigger.new){
                if(listCampaign.size()>0)
                {
                    cnew.addError('Not Allowed');
                }
                
            }update listCampaign;
        }
    }

}

 
Raj VakatiRaj Vakati
YOU assigned Campaign data to CampaignMembers in SOQL 

YOUR COde in not bulkified .. try somethink like below 
 
trigger CampaignmemberControl on CampaignMember (before insert) {
If(trigger.isInsert && trigger.isbefore){
Set<String> CampaignMember_parentid=new Set<String>();
Map<Id ,List<Campaign>> mapOfCmap = new Map<Id ,List<Campaign>>();


for (CampaignMember cc:trigger.new){
If(cc.Email !=null){
//CampaignMember_parentid.add(cc.CampaignId);

if(mapOfCmap.containsKey(cc.id)) {
List<Campaign> usersId = mapOfCmap.get(cc.Id);
usersId.add(cc.Campaign);
userRoleToUsers.put(cc.Id, usersId);
} else {
mapOfCmap.put(cc.Id, new List<Campaign> { cc.Campaign });
}

}

}

for(CampaignMember cnew:trigger.new){
if(mapOfCmap.get(cnew.id).size()>0)
{
cnew.addError('Not Allowed');
}

} 
}
}
Glyn Anderson (Slalom)Glyn Anderson (Slalom)
In this trigger, the first for-loop groups all of the CampaignMembers in the trigger by their CampaignIds.  The second for-loop adds an error to CampaignMembers if two or more are being inserted into the same Campaign.  After the second for-loop, the map contains only lists with a single CampaignMember in them.  The third for-loop uses an aggregate query to find any Campaigns that already have members, and adds an error to the CampaignMembers that are being inserted into those Campaigns.
trigger CampaignmemberControl on CampaignMember ( before insert )
{
    if ( Trigger.isInsert && Trigger.isbefore )
    {
        String errorMessage = 'Campaigns cannot have more than one CampaignMember';

        Map<Id,List<CampaignMember>> campaignMembersByCampaignId
            = new Map<Id,List<CampaignMember>>();

        for ( CampaignMember campaignMember : Trigger.new )
        {
            if  (   !campaignMembersByCampaignId
                        .containsKey( campaignMember.CampaignId )
                )
            {
                campaignMembersByCampaignId.put
                (   campaignMember.CampaignId
                ,   new List<CampaignMember>()
                );
            }
            campaignMembersByCampaignId
                .get( campaignMember.CampaignId )
                .add( campaignMember );
        }

        for ( Id campaignId : campaignMembersByCampaignId.keySet() )
        {
            List<CampaignMember> campaignMembers
                = campaignMembersByCampaignId.get( campaignId );
            if ( campaignMembers.size() > 1 )
            {
                for ( CampaignMember campaignMember : campaignMembers )
                {
                    campaignMember.addError( errorMessage );
                }
                campaignMembersByCampaignId.remove( campaignId );
            }
        }

        if ( campaignMembersByCampaignId.isEmpty() ) return;

        for ( AggregateResult result :
            [   SELECT  COUNT(Id), CampaignId
                FROM    CampaignMember
                WHERE   CampaignId IN :campaignMembersByCampaignId.keySet()
                GROUP BY CampaignId
            ]
            )
        {
            for ( CampaignMember campaignMember :
                    campaignMembersByCampaignId.get( (Id) result.get( 'CampaignId' ) )
                )
            {
                campaignMember.addError( errorMessage );
            }
        }
    }
}
Glyn Anderson (Slalom)Glyn Anderson (Slalom)
Already found a bug in my first post.  Use this instead.  We shouldn't modify the map that we're iterating over inside the loop.  This fixes that.
trigger CampaignmemberControl on CampaignMember ( before insert )
{
    if ( Trigger.isInsert && Trigger.isbefore )
    {
        String errorMessage = 'Campaigns cannot have more than one CampaignMember';

        Map<Id,List<CampaignMember>> campaignMembersByCampaignId
            = new Map<Id,List<CampaignMember>>();

        for ( CampaignMember campaignMember : Trigger.new )
        {
            if  (   !campaignMembersByCampaignId
                        .containsKey( campaignMember.CampaignId )
                )
            {
                campaignMembersByCampaignId.put
                (   campaignMember.CampaignId
                ,   new List<CampaignMember>()
                );
            }
            campaignMembersByCampaignId
                .get( campaignMember.CampaignId )
                .add( campaignMember );
        }

        Set<Id> campaignsToRemove = new Set<Id>();
        for ( Id campaignId : campaignMembersByCampaignId.keySet() )
        {
            List<CampaignMember> campaignMembers
                = campaignMembersByCampaignId.get( campaignId );
            if ( campaignMembers.size() > 1 )
            {
                for ( CampaignMember campaignMember : campaignMembers )
                {
                    campaignMember.addError( errorMessage );
                }
                campaignsToRemove.add( campaignId );
            }
        }
        campaignMembersByCampaignId.keySet().removeAll( campaignsToRemove );

        if ( campaignMembersByCampaignId.isEmpty() ) return;

        for ( AggregateResult result :
            [   SELECT  COUNT(Id), CampaignId
                FROM    CampaignMember
                WHERE   CampaignId IN :campaignMembersByCampaignId.keySet()
                GROUP BY CampaignId
            ]
            )
        {
            for ( CampaignMember campaignMember :
                    campaignMembersByCampaignId.get( (Id) result.get( 'CampaignId' ) )
                )
            {
                campaignMember.addError( errorMessage );
            }
        }
    }
}