+ Start a Discussion
Prasanth Reddy MPrasanth Reddy M 

Error while testing trigger in sandbox

I have received below error, when testing below trigger.

Trigger:

trigger CampaignMemberTaskCreation on CampaignMember (after update) {

list<Task> AddTask=new List<Task>();
for(CampaignMember CM : Trigger.new) {
if(cm.Status == 'Responded'){
AddTask.add(new Task(
WhoId = CM.ContactId,
Status = 'Completed',
Description='CM.Description',
Subject = 'Call - ' + CM,
Whatid= CM.CampaignId));
}
}
insert AddTask;
}
Error Message, when tried to update the campaign member status:

Sandbox
 
Apex script unhandled trigger exception by user/organization: 00570000003003D/00D190000000OrW Source organization: 00D700000008ieh (null)
CampaignMemberTaskCreation: execution of AfterUpdate
 
caused by: System.DmlException: Insert failed. First exception on row 0; first error: STRING_TOO_LONG, Subject: data value too large: Call - CampaignMember:{Id=00v19000000TEZkAAO, IsDeleted=false, CampaignId=70119000000A3qoAAC, LeadId=00Q19000001eGJWEA2, ContactId=null, Status=Responded, HasResponded=true, CreatedDate=2015-10-15 16:55:39, CreatedById=00570000003003DAAQ, LastModifiedDate=2016-01-25 09:59:56, LastModifiedById=00570000003003DAAQ, SystemModstamp=2016-01-25 09:59:56, FirstRespondedDate=2016-01-25 00:00:00, Name=null, Email__c=false} (max length=255): [Subject]
 
Trigger.CampaignMemberTaskCreation: line 14, column 1


How i can resolve the exception here?
Best Answer chosen by Prasanth Reddy M
Abhishek BansalAbhishek Bansal
Hi Prasantha,

It can be clearly seen from your code that you are appending the whole instane of your CampaignMember object i.e. CM in your task subject which leads to the limit exceed.
I have modified your code to avoid this error, Please change your trigger codce with below code :
trigger CampaignMemberTaskCreation on CampaignMember (after update) {

list<Task> AddTask=new List<Task>();
for(CampaignMember CM : Trigger.new) {
if(cm.Status == 'Responded'){
AddTask.add(new Task(
WhoId = CM.ContactId,
Status = 'Completed',
Description=CM.Description,
Subject = 'Call - CM',//As per my Assumption this is waht you want to acheived but if you want the subject to be something different than please change accordingly but please make sure to keep it upto 255 characters
Whatid= CM.CampaignId));
}
}
insert AddTask;
}
Please let me know if you any issue with the above code.

Thanks,
Abhishek Bansal.

All Answers

Sumeet_ForceSumeet_Force
As it highlights, since subject field data is too large for specific record(s), you can either handle that exception in the code OR you can use substring function to extract 255 i.e. max allowed characters for the Subject field.
Abhishek BansalAbhishek Bansal
Hi Prasantha,

It can be clearly seen from your code that you are appending the whole instane of your CampaignMember object i.e. CM in your task subject which leads to the limit exceed.
I have modified your code to avoid this error, Please change your trigger codce with below code :
trigger CampaignMemberTaskCreation on CampaignMember (after update) {

list<Task> AddTask=new List<Task>();
for(CampaignMember CM : Trigger.new) {
if(cm.Status == 'Responded'){
AddTask.add(new Task(
WhoId = CM.ContactId,
Status = 'Completed',
Description=CM.Description,
Subject = 'Call - CM',//As per my Assumption this is waht you want to acheived but if you want the subject to be something different than please change accordingly but please make sure to keep it upto 255 characters
Whatid= CM.CampaignId));
}
}
insert AddTask;
}
Please let me know if you any issue with the above code.

Thanks,
Abhishek Bansal.
This was selected as the best answer
Prasanth Reddy MPrasanth Reddy M
Thank You Abhishek, the trigger is working, i had written test class for the trigger, when tried to run the test, it is shwoing nullpoint exception

Class CampaignMemberTaskcreation
Method Name testtask
Pass/Fail Fail
Error Message System.NullPointerException: Attempt to de-reference a null object
Stack Trace Class.CampaignMemberTaskcreation.testtask: line 22, column 1


Here is the class :

@isTest

public class CampaignMemberTaskcreation {

    Static testmethod void testtask(){

        Contact co1 = new Contact(
        LastName = 'Last',
        Title='CEO');
        
        insert  co1;
        string co1Id = co1.id;

        Campaign ca1 = new Campaign(
            Name = 'Testcampaign',
            Date_Sent__c=Date.today(),
            IsActive = TRUE);
        insert ( ca1 );
        string ca1Id = ca1.id;

        CampaignMember m1 = new CampaignMember();
            m1.Contact.Id = co1.Id; 
            m1.Campaign.Id = ca1.Id;
            m1.status='Responded';
        insert m1;
    }

}


Could you please let me know, how i can correct the test class ?