• SteveOrg
  • NEWBIE
  • 0 Points
  • Member since 2013

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 4
    Questions
  • 7
    Replies

When I attempt to pull in the Name of an Account that is related to an Opportunity:  

 

string s = trigger.newMap.get(oppty.AccountId).name;

 

I get the following error:

 

 

Error:Apex trigger updateOpptyCustomerWons caused an unexpected exception, contact your administrator: updateOpptyCustomerWons: execution of AfterUpdate caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.updateOpptyCustomerWons: line 31, column 1

 

Any ideas what I might be doing wrong?  

 

trigger updateOpptyCustomerWons on Opportunity (after update) { 

    //make a set to hold opportunity ids that we need
    Set<Id> opptyIds = new Set<Id>();
    
    for(Opportunity o : trigger.new)
        {
        //check to see if our field has been changed
        if(o.StageName != trigger.oldMap.get(o.Id).StageName){
            
            opptyIds.add(o.Id);
            
        }

       }
    
    if(!opptyIds.isEmpty()){
    
        //get any opptys associated with the accounts that have been edited
        
        //Opportunity opp
        
        
        //Opportunity oppty = [SELECT Id,Name, StageName FROM Opportunity WHERE StageName = 'Closed Won' AND Id IN :opptyIds limit 1];
        List<Opportunity> opptyList = [SELECT Id,StageName,Name, AccountId FROM Opportunity WHERE StageName = 'Closed Won' AND Id IN :opptyIds ]; //include Name
        for(Opportunity oppty: opptyList){
        Opportunity opp = new Opportunity();
        
        
        //string s = oppty.name;
        string s = trigger.newMap.get(oppty.AccountId).name;
        
        OpportunityUpdateURL.hitTheServer(s);
        
        
        
     } 
       
    }


}

 

 

 

Hi there, when I test my trigger by updating an opportunity to be 'closed won', I am getting an error back specifying that my trigger isn't returning any rows.  I'm hoping there is something quick that I missed:

 

Error:

 

Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger updateOpptyCustomerWons caused an unexpected exception, contact your administrator: updateOpptyCustomerWons: execution of AfterUpdate caused by: System.QueryException: List has no rows for assignment to SObject: Trigger.updateOpptyCustomerWons: line 24, column 1

 

Trigger:

 

trigger updateOpptyCustomerWons on Opportunity (after update) { 

    //make a set to hold opportunity ids that we need
    Set<Id> opptyIds = new Set<Id>();
    
    for(Opportunity o : trigger.new)
        {
        //check to see if our field has been changed
        if(o.StageName != trigger.oldMap.get(o.Id).StageName){
            
            opptyIds.add(o.Id);
            
        }

       }
    
    if(!opptyIds.isEmpty()){
    
        
        //Opportunity opp
        
        
        Opportunity oppty = [SELECT Id,StageName FROM Opportunity WHERE StageName = 'Closed Won' AND Id IN :opptyIds limit 1];
        
        Opportunity opp = new Opportunity();
        
        if (oppty != null) {
        string s = oppty.name;
        OpportunityUpdateURL.hitTheServer(s);
        }    
        
      
       
    }


}

 

Hi there, I created a trigger that would send an external post request when an opportunity stage was updated to "Closed Won" and for some reason my http request is not posting.  I was wondering if there is a mistake in my code.  Any help would be greatly appreciated.

 

Here is the Class (I hardcoded the URL for now).

 

public class OpportunityUpdateURL {
public static void hitTheServer(Opportunity[] opp) {
for (Opportunity o:opp){
HttpRequest req = new HttpRequest();
req.setEndpoint ('http://url.com/events?type=customer_won&advocate[email]=steve@influitive.com&advocate[name]=Steve%20Organ&params[Opportunity Amount]=1000');
req.setMethod ('POST');
}
}
}

 

 

Trigger:

 

trigger updateOpptyCustomerWons on Opportunity (after update) { 

    //make a set to hold opportunity ids that we need
    Set<Id> opptyIds = new Set<Id>();
    
    for(Opportunity o : trigger.new)
        {
        //check to see if stage field has been changed
        if(o.StageName != trigger.oldMap.get(o.Id).StageName){
            
            opptyIds.add(o.Id);
            
        }

       }
    
    if(!opptyIds.isEmpty()){
    
        //get any opptys that have been edited
        List<Opportunity> opptys = [SELECT Id,StageName FROM Opportunity WHERE StageName = 'Closed Won' AND Id IN :opptyIds];
        
        for(Opportunity o : opptys){
            Opportunity[] opp = Trigger.new;
            OpportunityUpdateURL.hitTheServer(opp);
            }
        
      
       
    }


}

 

I also added a remote site for my URL.

 

Thanks so much,

Steve

I have created a simple trigger that would update all associated opportunities with a field on the account record upon update, however I'm not sure what the test method needs to be since all the examples I found relate to a new record being created.  Would appreciate any help.

 

Here is the code for my trigger:

 

trigger accountTrigger on Account (after insert,after update) 
{ 
  
List<account> accountWithOpptys = [select id,name,customaccount__c, (select id, name, customopportunities__c from Opportunities where accountId IN :Trigger.newMap.keySet()) 

from Account where Id IN :Trigger.newMap.keySet()]; 

for(Account a : accountWithOpptys)
  { 
  for(Opportunity o: a.Opportunities)
    { 
    o.customopportunities__c = a.customaccount__c; 
    update o;
    } 
  } 
}

 I was hoping there is someone that has done something similar where I could leverage your test case.  

 

 

When I attempt to pull in the Name of an Account that is related to an Opportunity:  

 

string s = trigger.newMap.get(oppty.AccountId).name;

 

I get the following error:

 

 

Error:Apex trigger updateOpptyCustomerWons caused an unexpected exception, contact your administrator: updateOpptyCustomerWons: execution of AfterUpdate caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.updateOpptyCustomerWons: line 31, column 1

 

Any ideas what I might be doing wrong?  

 

trigger updateOpptyCustomerWons on Opportunity (after update) { 

    //make a set to hold opportunity ids that we need
    Set<Id> opptyIds = new Set<Id>();
    
    for(Opportunity o : trigger.new)
        {
        //check to see if our field has been changed
        if(o.StageName != trigger.oldMap.get(o.Id).StageName){
            
            opptyIds.add(o.Id);
            
        }

       }
    
    if(!opptyIds.isEmpty()){
    
        //get any opptys associated with the accounts that have been edited
        
        //Opportunity opp
        
        
        //Opportunity oppty = [SELECT Id,Name, StageName FROM Opportunity WHERE StageName = 'Closed Won' AND Id IN :opptyIds limit 1];
        List<Opportunity> opptyList = [SELECT Id,StageName,Name, AccountId FROM Opportunity WHERE StageName = 'Closed Won' AND Id IN :opptyIds ]; //include Name
        for(Opportunity oppty: opptyList){
        Opportunity opp = new Opportunity();
        
        
        //string s = oppty.name;
        string s = trigger.newMap.get(oppty.AccountId).name;
        
        OpportunityUpdateURL.hitTheServer(s);
        
        
        
     } 
       
    }


}

 

 

 

Hi there, when I test my trigger by updating an opportunity to be 'closed won', I am getting an error back specifying that my trigger isn't returning any rows.  I'm hoping there is something quick that I missed:

 

Error:

 

Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger updateOpptyCustomerWons caused an unexpected exception, contact your administrator: updateOpptyCustomerWons: execution of AfterUpdate caused by: System.QueryException: List has no rows for assignment to SObject: Trigger.updateOpptyCustomerWons: line 24, column 1

 

Trigger:

 

trigger updateOpptyCustomerWons on Opportunity (after update) { 

    //make a set to hold opportunity ids that we need
    Set<Id> opptyIds = new Set<Id>();
    
    for(Opportunity o : trigger.new)
        {
        //check to see if our field has been changed
        if(o.StageName != trigger.oldMap.get(o.Id).StageName){
            
            opptyIds.add(o.Id);
            
        }

       }
    
    if(!opptyIds.isEmpty()){
    
        
        //Opportunity opp
        
        
        Opportunity oppty = [SELECT Id,StageName FROM Opportunity WHERE StageName = 'Closed Won' AND Id IN :opptyIds limit 1];
        
        Opportunity opp = new Opportunity();
        
        if (oppty != null) {
        string s = oppty.name;
        OpportunityUpdateURL.hitTheServer(s);
        }    
        
      
       
    }


}

 

Hi there, I created a trigger that would send an external post request when an opportunity stage was updated to "Closed Won" and for some reason my http request is not posting.  I was wondering if there is a mistake in my code.  Any help would be greatly appreciated.

 

Here is the Class (I hardcoded the URL for now).

 

public class OpportunityUpdateURL {
public static void hitTheServer(Opportunity[] opp) {
for (Opportunity o:opp){
HttpRequest req = new HttpRequest();
req.setEndpoint ('http://url.com/events?type=customer_won&advocate[email]=steve@influitive.com&advocate[name]=Steve%20Organ&params[Opportunity Amount]=1000');
req.setMethod ('POST');
}
}
}

 

 

Trigger:

 

trigger updateOpptyCustomerWons on Opportunity (after update) { 

    //make a set to hold opportunity ids that we need
    Set<Id> opptyIds = new Set<Id>();
    
    for(Opportunity o : trigger.new)
        {
        //check to see if stage field has been changed
        if(o.StageName != trigger.oldMap.get(o.Id).StageName){
            
            opptyIds.add(o.Id);
            
        }

       }
    
    if(!opptyIds.isEmpty()){
    
        //get any opptys that have been edited
        List<Opportunity> opptys = [SELECT Id,StageName FROM Opportunity WHERE StageName = 'Closed Won' AND Id IN :opptyIds];
        
        for(Opportunity o : opptys){
            Opportunity[] opp = Trigger.new;
            OpportunityUpdateURL.hitTheServer(opp);
            }
        
      
       
    }


}

 

I also added a remote site for my URL.

 

Thanks so much,

Steve

I have created a simple trigger that would update all associated opportunities with a field on the account record upon update, however I'm not sure what the test method needs to be since all the examples I found relate to a new record being created.  Would appreciate any help.

 

Here is the code for my trigger:

 

trigger accountTrigger on Account (after insert,after update) 
{ 
  
List<account> accountWithOpptys = [select id,name,customaccount__c, (select id, name, customopportunities__c from Opportunities where accountId IN :Trigger.newMap.keySet()) 

from Account where Id IN :Trigger.newMap.keySet()]; 

for(Account a : accountWithOpptys)
  { 
  for(Opportunity o: a.Opportunities)
    { 
    o.customopportunities__c = a.customaccount__c; 
    update o;
    } 
  } 
}

 I was hoping there is someone that has done something similar where I could leverage your test case.