+ Start a Discussion
hemmhemm 

Trigger fails when License Manager submits a Lead

I wrote the following trigger.

Code:
trigger Create_CampaignMember_For_New_Leads on Lead (after insert) {
 
 List <CampaignMember> cm = new list<CampaignMember>();
 
 for(Lead L : Trigger.new) {
  
  String cname = L.leadsource;
  
  List <Campaign> c = [select id, name from Campaign where name = :cname limit 1];
  
  if(!c.isEmpty()){
   CampaignMember cml = new CampaignMember();
   cml.campaignid = c[0].id;
   cml.leadid = l.id;
   cm.add(cml);
  }
 }
 
 if(!cm.isEmpty()){
  insert cm;
 }
}

 
The code adds a Campaign Member record automatically if it can find a Campaign with a name matching the Lead Source name. 

All is great when I use the application and with web-to-lead.  However, License Manager submitted a Lead/License today and I got the following email.

Code:
Apex script unhandled trigger exception by user/organization: 0053000000125N9/00D300000007gjY

Create_CampaignMember_For_New_Leads: compiling trigger body

caused by: line 15, column 29: sObject type 'Campaign' is not supported.

 
That user id is one of the License Manager users that gets created behind the scenes.  The end result is that the License record gets created, but the Lead does not.

Is there anything I can do to make this get fixed?  I was thinking I could edit the API Access Restrictions on the License Manager installed package to give access to Campaigns.  Will that help?

If that won't help, do I need to edit my Apex Code to do a user lookup to make sure it's not an LMA user?  Or can I do a describeGlobal type of query to make sure the user has access to Campaign?  If that's the route to take, I could use a little help.

Thanks.
Ron HessRon Hess
The license manager user ( not sure what profile that is) probably has the marketing user checkbox on the profile turned off.  Not sure if this is something unique to your system or not. 
Start by checking the profiles for marketing checkbox( campaigns), then see if this object has been hidden from some profiles.
hemmhemm
Ron, thanks for the response.  I actually tried this already.

License Manager users are not visible in the UI, but I used the Excel Connector (thanks for building that, by the way.) to try that.  Updating that value on any License Manager record gives the following error:
Marketing User is not allowed for this License Type.
All of the License Manager users use a profile called "Package License Manager" and this profile is not accessible in the UI.  I looked at the profile in the Excel Connector and I went so far as trying to give that profile the Modify All Data permission.  Here's the error I get:

You may not turn on permission ModifyAllData for this License Type : Permissions



MKPartners.comMKPartners.com
What about adding an IF statement that prevents the trigger from running when the lead creator doesn't have the marketing check box checked on their user record.  If this trigger isn't going to be packaged, you could just hardcode the userId, if not you'll have to do a query. 

Of course if the whole purpose of this was to enhance license manager, then that really wont work.  We've developed a Campaign Member 2.0 functionality that would sit on top of Campaign Members but add additional fields and functionality.  You could leverage this concept to have the trigger create a CampaignMember2.0 object where you are the owner, and then use another trigger to create a standard campaign member based on that custom object.  Since you are the owner of the custom object, I think the trigger will create the standard campaign member using your userId instead of the license manager's.

Email me if you want the xml to paste into Eclipse to update your config via the metadata API.

-Matt
hemmhemm
I tried the IF statement to not have the code run when the Lead is not a Package Installation lead, but it still gives the error.  I also tried adding some try...catch code in there, but it still gives the error.

The populating of another object to then populate the campaign members it not practical.
hemmhemm
I have tried IF statements using username and also IF statements using the lead source.  I am pretty sure it's an Apex / License Manager bug.
hemmhemm
My workaround will be to use Outbound Messaging to ping a PHP I have out there to do what I need it to.
hpatelhpatel
The fix will be coming out within the next month. Will update this post when it's available on all instances.
hemmhemm
How much longer until the new LMA 2.0 is out?  I assume the fix is built in that.
hpatelhpatel
Can you please try installing your package in a new DE org and see if the lead gets created? If your instance has been upgraded to the latest version, it should work already.

You shouldn't have to wait till LMA 2.0.
hemmhemm
Based upon initial testing, it works.  For anyone who cares, here's my code.

Code:
trigger Create_CampaignMember_For_New_Leads on Lead (after insert) {

 try { 
  
  if (Trigger.new.size() == 1) {
   
   List <CampaignMember> cm = new list<CampaignMember>();
   
   for(Lead L : Trigger.new) {
    
     String cname = L.leadsource;
     
     List <Campaign> c = [select id, name from Campaign where name = :cname limit 1];
     
     if(!c.isEmpty()){
      CampaignMember cml = new CampaignMember();
      cml.campaignid = c[0].id;
      cml.leadid = l.id;
      cm.add(cml);
     }
   }
   
   if(!cm.isEmpty()){
    insert cm;
   }
  }
  
  
 } catch(Exception e) {
  system.debug ('error: ' + e.getMessage() );
 } 
 
}

 
And the Test:

Code:
public class Create_CampaignMember_For_New_Leads {

 static testMethod void Create_CampaignMember_For_New_Leads() {
 
   List <Lead> Leads;
      
   // Create a Lead with a Lead Source matching a Campaign
   Lead L1 = new Lead();
   L1.lastname = 'Create_CampaignMember_For_New_Leads';
   L1.firstname = 'Test For';
   L1.company = 'Company';
   L1.leadsource = 'Web';
      
   insert L1;
   String holder = L1.id;
   
   List <CampaignMember> cm = [select id from CampaignMember where leadid = :holder limit 1];
   system.AssertEquals(1,cm.size());
   
   // Create a Lead without a Lead Source matching a Campaign
   Lead L2 = new Lead();
   L2.lastname = 'Create_CampaignMember_For_New_Leads';
   L2.firstname = 'Test For';
   L2.company = 'Company';
   L2.leadsource = 'No Matching Campaign';
      
   insert L2;
   
   String holder2 = L2.id;
   
   List <CampaignMember> cm2 = [select id from CampaignMember where leadid = :holder2 limit 1];
   system.AssertEquals(0,cm2.size());
                        
 }

}

 


hpatelhpatel
Great :-)
hemmhemm
It's kind of hard for me to be certain, but I activated the trigger and my leads from AppExchange and License Manager just stopped.  I didn't receive them for 2 days.  Then I just de-activated my trigger and got a License Manager lead within about 10 minutes.  My Auto vCard app typically gets ~5 new installs per day, which makes me think something is still wrong.

I was not receiving any error emails, though.  The issue could be that the License Manager user 1) is not a Marketing User and 2) cannot be granted permission to use my Apex classes since I can't see the profile that user uses.  It's all hidden from me.

It's really hard to test and be certain, but the circumstances lead me to believe there is still a problem.

I'm happy to test this with you at some point.
jeremy_wjeremy_w
I've also had this problem before and would really like to find out if this is fixed.

It was for a different object (Case) but exactly the same issue - LicenseManager does not have the privileges to see cases and I don't have the privileges to see/modify the licensemanager user (I have the System Administrator profile in SF).

I can't try in dev as I don't have google adwords in dev and do not want to put this live again only for it to fail.

Not sure if there's some other way to test this?

EDIT: FYI last time this was in production and it failed was: 10th April 2008




Message Edited by jeremy_w on 06-24-2008 10:30 AM
hpatelhpatel
The way to test it is to:

1. Enable your trigger on Leads in your LMO org.
2, Subscribe to a new DE org.
3. Install your package in DE org from step 2.
4. Within a few minutes, a lead and a license record should be created in your LMO org for the package install in the DE org.
5. If messages don't get delivered, disable the trigger. Wait at the most an hour and see if the license gets created.

We tested this internally and there doesn't seem to be a problem anymore. But, feel free to check yourself and let us know if there is an issue.
jeremy_wjeremy_w
Thanks. I think your advice was focussed towards the original posters code.
But I decided to try my trigger in production and it now works fine so far. No errors relating to LicenseManager not having the access to see Cases.

So, thank you.

I'd like to know where I can see a list of these bug fixes that get released quietly. Can you post a link to where this information is?

Jeremy
hemmhemm
Thanks.  Still testing.  Question...

Do I need to update my triggers/classes to use the 13.0 API for this to be fixed or was it all on the Salesforce side?
hpatelhpatel
It should work regardless of the api version.