• Rebecca York
  • NEWBIE
  • 0 Points
  • Member since 2015

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 2
    Replies
Hi,

I am pretty new to SalesForce coding and hoping you can help.

I am getting the following error: Error:Apex trigger CreateCompetitor caused an unexpected exception, contact your administrator: CreateCompetitor: execution of AfterUpdate caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.CreateCompetitor: line 5, column 1. 

My research seems to be telling me that it is throwing an error when the field I am referencing is blank. Sometimes this field will need to be blank and I don't want to run the trigger in that case, can someone please help?

Below is my code (which will eventually need to be built out some more to include more competitors)

trigger CreateCompetitor on Opportunity (after insert, after update) {
 for(Opportunity o: trigger.new){ 
    String opptyId = o.Id;
    Opportunity oldOpp = Trigger.oldMap.get(o.Id);
    if(o.Competitors__c.contains('Competitor 1') && !oldOpp.Competitors__c.contains('Competitor 1')){
    Competitor__c[] OLI = [Select Id, Name  
                           From Competitor__c
                           where Name = 'Competitor 1'];
      Link_Competitor_with_an_Opportunity__c[] ast = new Link_Competitor_with_an_Opportunity__c[]{};
         Link_Competitor_with_an_Opportunity__c m = new Link_Competitor_with_an_Opportunity__c();
                  for(Competitor__c ol: OLI){
            m = new Link_Competitor_with_an_Opportunity__c();    
            m.Opportunity__c = o.Id;
            m.Competitor__c = ol.Id;
            ast.add(m);
            update OLI;
            insert ast;
    if(o.Competitors__c.contains('Competitor 2') && !oldOpp.Competitors__c.contains('Competitor 2')){
    Competitor__c[] OLI2 = [Select Id, Name  
                           From Competitor__c
                           where Name = 'Competitor 2'];
      Link_Competitor_with_an_Opportunity__c[] ast2 = new Link_Competitor_with_an_Opportunity__c[]{};
         Link_Competitor_with_an_Opportunity__c m2 = new Link_Competitor_with_an_Opportunity__c();
                  for(Competitor__c ol2: OLI2){
            m2 = new Link_Competitor_with_an_Opportunity__c();    
            m2.Opportunity__c = o.Id;
            m2.Competitor__c = ol2.Id;
            ast2.add(m2);
            update OLI;
            insert ast2;
            }}}}}}

Thanks in advance!
To give you a bit of context, we have a requirement to convert contacts back to leads if they are no longer being actively worked by our sales team.

I found an app on the Appexchange that does exactly this, except that it works off an Opportunity instead of a Contact! (https://appexchange.salesforce.com/listingDetail?listingId=a0N3000000B4HnnEAF)

Since my users are quite lazy, I know they are not going to be happy about creating a dummy opp, to decovert a lead, so my next thought was to create a trigger to create the dummy opp for them, and run the app off that.

The trigger I have created is this:
trigger CreateOpportunitytoRunUnconvert on Contact (after insert, after update) {
for(Contact c: trigger.new){ 
      if(c.Convert_back__c==true){
      String ContactId = c.Id;
         Opportunity[] ast = new Opportunity[]{};
         Opportunity o = new Opportunity();
         {
            o = new Opportunity();
            o.Name = 'Dummy Opp to Unconvert';
            o.Account = c.Account;
            o.AccountId = c.AccountId;
            o.LeadSource = c.LeadSource;
            o.Paid_in_Close_Month__c = 0;
            o.StageName = 'Identifying an Opportunity';
            o.CloseDate = System.Today();
            ast.add(o); 
       }
       insert ast;
     }
}
}

And the button I've created simply marks the 'convert back' checkbox which runs the trigger.

The next step to this would be to open up the newly created opportunity so we could 'unconvert' (the app I've found) from there, but I'm completely stumped on how to do this. The second option would be to run the trigger I've created, and then run their trigger without needing to navigate to the Opportunity (this would probably be preferable).

Any advice on how to achieve this, (or even a different way to achieve the same result) would be amazing!

Thanks for your help!
Hi,

I am pretty new to SalesForce coding and hoping you can help.

I am getting the following error: Error:Apex trigger CreateCompetitor caused an unexpected exception, contact your administrator: CreateCompetitor: execution of AfterUpdate caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.CreateCompetitor: line 5, column 1. 

My research seems to be telling me that it is throwing an error when the field I am referencing is blank. Sometimes this field will need to be blank and I don't want to run the trigger in that case, can someone please help?

Below is my code (which will eventually need to be built out some more to include more competitors)

trigger CreateCompetitor on Opportunity (after insert, after update) {
 for(Opportunity o: trigger.new){ 
    String opptyId = o.Id;
    Opportunity oldOpp = Trigger.oldMap.get(o.Id);
    if(o.Competitors__c.contains('Competitor 1') && !oldOpp.Competitors__c.contains('Competitor 1')){
    Competitor__c[] OLI = [Select Id, Name  
                           From Competitor__c
                           where Name = 'Competitor 1'];
      Link_Competitor_with_an_Opportunity__c[] ast = new Link_Competitor_with_an_Opportunity__c[]{};
         Link_Competitor_with_an_Opportunity__c m = new Link_Competitor_with_an_Opportunity__c();
                  for(Competitor__c ol: OLI){
            m = new Link_Competitor_with_an_Opportunity__c();    
            m.Opportunity__c = o.Id;
            m.Competitor__c = ol.Id;
            ast.add(m);
            update OLI;
            insert ast;
    if(o.Competitors__c.contains('Competitor 2') && !oldOpp.Competitors__c.contains('Competitor 2')){
    Competitor__c[] OLI2 = [Select Id, Name  
                           From Competitor__c
                           where Name = 'Competitor 2'];
      Link_Competitor_with_an_Opportunity__c[] ast2 = new Link_Competitor_with_an_Opportunity__c[]{};
         Link_Competitor_with_an_Opportunity__c m2 = new Link_Competitor_with_an_Opportunity__c();
                  for(Competitor__c ol2: OLI2){
            m2 = new Link_Competitor_with_an_Opportunity__c();    
            m2.Opportunity__c = o.Id;
            m2.Competitor__c = ol2.Id;
            ast2.add(m2);
            update OLI;
            insert ast2;
            }}}}}}

Thanks in advance!

Hi,

 

We would like to trigger a "Thanks Badge" everytime a record is created that meets a specific criteria (i.e score = 10 out of 10).

 

Any idea what the Feeditem is within Apex ? How do I specify the badge?

 

appreciate everyones insight.

 

Kind regards,

 

Leslie