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
Michael Lobel 3Michael Lobel 3 

Updating a field on a Salesforce lead (or Object) when a Zendesk ticket is opened

After installing the Zendesk Package for Salesforce, I'm trying to create a basic Salesforce apex trigger based off the Zendesk Ticket sObject, in order to update a boolean on a lead. This is my first Salesforce trigger, so there might be some basic steps I'm missing.. But I've saved the trigger in the Developer Console and it doesn't report any problems, yet creating a new ticket doesn't update the boolean.

Here's the trigger I'm running below. I've installed the Zendesk integration onto my Developer edition Salesforce, to the point that new tickets display on a lead's record. However when I have this trigger active and log a new ticket with a lead, it doesn't update the boolean.

Any help appreciated!
trigger UpdateCampaignResponse on Zendesk__Zendesk_Ticket__c (after insert) {
        for(Zendesk__Zendesk_Ticket__c ticket : {

            // Check if ticket requester is a lead
            if(String.valueOf(ticket.Zendesk__Requester__c).startsWith('00Q')== True) {

                // Create a set, add ticket requester id
                set<id>leadId=new set<id>();

                // Lookup and assign lead based on set
                Lead lead=[Select CampaignResponse__c from Lead where id in :leadId];
                // Mark Campaign Response on lead
                update lead;

Crystal Rochlitz 4Crystal Rochlitz 4
Yay for writing your first trigger. As per best practices, you never want to run a query or perform dml inside a loop. Other than that, you are only updating one lead out of your set. Try this:
trigger UpdateCampaignResponse on Zendedk__Zendesk_Ticket__c (after insert) {

    Set<Id> leadId = new Set<Id>();
     for(Zendesk__Zendesk_Ticket__c ticket : {
           if(string.valueOf(ticket.Zendesk__Requester__c).startsWith('00Q') == true) {
          List<Lead> leads = new List<Lead>([SELECT CampaignResponse__c FROM Lead                Where Id IN: leadId]);
          List<Lead> leadsToUpdate = new List<Lead>();
          for(Lead l : leads) {
                l.CampaignResponse__c = true;
             update leadsToUpdate;

There may be some typos in there but that should get you going. If you are still stuck write back. GL :)