function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
SolidLucasSolidLucas 

Trigger help,Campaign Member Status

Hello, I'm trying to create a trigger that updates a CampaignMember status and create a case to a lead in a web to case action, i created a hidden field that comes filled with the campaign Id,but when my triggers is fired it only updates the status of my CampaignMember and don't create the case to the Lead. Someone could help me with that?
trigger ai_WebToCaseLead on Case (after insert) {


List<CampaignMember> listCm = [SELECT Id,Lead.Email,CampaignId FROM CampaignMember WHERE CampaignId = '701190000001F5l'];

    for(Case caso : Trigger.new){
        System.debug('caso');
        for(CampaignMember cm : listCm){
                    System.debug('caso');

            if(cm.Lead.Email == caso.SuppliedEmail && caso.ChaveCampanha__c == '701190000001F5l'){
                cm.Status = 'Respondido';
   				caso.ParentId = cm.Lead.Id;
                update cm;
                update caso;
                System.debug(cm);
                System.debug(caso);
            }
        }
    }
}



Regards!
Lucas
Best Answer chosen by SolidLucas
Phillip SouthernPhillip Southern
Hi Lucas, we might need some clarification on this.

You say you need to insert a case, but this trigger is already working when a case is insert.  Are you saying you need it to insert a second case?  If so you'll need to build that case out and perform an insert.

Right now it looks like you are trying to update the original case, you wont be able to do that in an after insert trigger context.  You'll have to create a new sobject and assign it the Id and update that, something like:

Case c = new Case(Id = caso.Id);
.....make assignments....
update c;

Also you're trying to set the ParentId field of the case to something it won't accept....the Id of a Lead record.  Case ParentId field is only releveant for other Case Ids.

 

All Answers

Phillip SouthernPhillip Southern
Hi Lucas, we might need some clarification on this.

You say you need to insert a case, but this trigger is already working when a case is insert.  Are you saying you need it to insert a second case?  If so you'll need to build that case out and perform an insert.

Right now it looks like you are trying to update the original case, you wont be able to do that in an after insert trigger context.  You'll have to create a new sobject and assign it the Id and update that, something like:

Case c = new Case(Id = caso.Id);
.....make assignments....
update c;

Also you're trying to set the ParentId field of the case to something it won't accept....the Id of a Lead record.  Case ParentId field is only releveant for other Case Ids.

 
This was selected as the best answer
SolidLucasSolidLucas
Hello Phillip, i already resolved this problem thanks for your attention!