You need to sign in to do that
Don't have an account?
Girbson Bijou
Duplicate Value in field updated by trigger.
Duplicate Value in field updateed by trigger.
My objective is to have the names of the campaign member in a field called Acteurs in Campaign separate by comma.
The code belows works, but i have the following issue:
- All the campaignMember Name, are not displays in the campaign Custom field(Acteurs__c)
- when i update the campaign members, it re-copy the name of all to the campaign member to the Custom field(Acteurs__c) which cause duplicate name.
Below are my Code:
My objective is to have the names of the campaign member in a field called Acteurs in Campaign separate by comma.
The code belows works, but i have the following issue:
- All the campaignMember Name, are not displays in the campaign Custom field(Acteurs__c)
- when i update the campaign members, it re-copy the name of all to the campaign member to the Custom field(Acteurs__c) which cause duplicate name.
Below are my Code:
Trigger allActors on CampaignMember (after update, after insert, after delete, after undelete){ List <CampaignMember> cm = (Trigger.isInsert|| Trigger.isUnDelete) ? Trigger.new : Trigger.old; List <Id> campId = new List<Id> (); for (CampaignMember AllMember : cm) { campId.add(AllMember.CampaignId); } List< Campaign> campaignList = [ SELECT id, Acteurs__c, (SELECT Name from CampaignMembers) From Campaign Where id in:campId]; for (Campaign camp: campaignList){ if (camp.CampaignMembers.size()>0){ //camp.Name = string.valueOf(camp.CampaignMembers[0].Name); for(integer i =1; i < camp.CampaignMembers.size(); i++) camp.Acteurs__c = camp.Acteurs__c+';'+string.valueOf(camp.CampaignMembers[i].Name); } else camp.Acteurs__c = null; } update campaignList; } //Test Class @isTest Private class AllActorsTest{ @isTest static void InsetCm(){ Campaign c = new Campaign (); c.Name = 'My Campaign'; Insert c; CampaignMember cm = new CampaignMember(); contact cont = new Contact (firstName='Bijou', lastName ='Girbson'); Upsert cont; cm.CampaignId = c.Id; cm.ContactId = cont.Id; insert cm; Delete cm; } }
Apex trigger allActors caused an unexpected exception, contact your administrator: allActors: execution of AfterInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.allActors: line 17, column 1