• Valerio_81
  • NEWBIE
  • 0 Points
  • Member since 2013

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

Hi,

 

I'm not a developer but I need an help for a Trigger that I tried to do.

 

I need to store all the product code(s) linked to an opportunity after insert/delete/update/undelete of a Product.

 

I've created a field in the Opportunity table (Product_Code_Selected__C) in order to store this information.

 

I'm able to get the PricebookEntryID but I need to store the product code. I Know that I have to do a sub query, but I tried without success. 

I have also an issue if I try to delete a product from an opportunity (I'm expecting that the field will be updated without the product deleted) but I'm receiving this error message:

 

OppProductCode_trigger: execution of AfterDelete

caused by: System.NullPointerException: Attempt to de-reference a null object

Trigger.OppProductCode_trigger: line 5, column 1

 

Could you please help me? This is the Aper Trigger

 

trigger OppProductCode_trigger on OpportunityLineItem (after delete, after insert, after undelete, 
after update) {
    //Setup the array to hold the Id to Iterate through
    Set<Id> CurrentOppId = new Set<Id>();
        for (OpportunityLineItem OppLnItem : Trigger.new){
        	CurrentOppId.add(OppLnItem.OpportunityId);
    }
 
    	// Create List of One Opportunity Id to Update
	    List<Opportunity> OppId = 
	    	[Select o.Id from Opportunity o
	    		where o.Id in: CurrentOppId];

    		// Create List of Opportunity Products to iterate through later
		    List<OpportunityLineItem> relatedOLIs =
			    [Select oli.Id, oli.PricebookEntryId from OpportunityLineItem oli
	    			where oli.OpportunityId in: CurrentOppId];

    // Used to not add ',' on first OpportunityLineItem PricebookEntryID
    Boolean isFirst = true;
       
    //Iterate through the Opportunity to Update (should be just one record)
    for (Opportunity opp2 : OppId){
	    opp2.Product_Code_Selected__c = '';

	    //Iterate through the line items to add PricebookEntryID data to the Opportunity
	    for (OpportunityLineItem oli2 : relatedOLIs){
	                   	if(isFirst) {
	                		opp2.Product_Code_Selected__c += oli2.PricebookEntryId;
	                		isFirst = false;
	            		}
	            		else {
	                		opp2.Product_Code_Selected__c += ',' + oli2.PricebookEntryId; 
		            	}
	    }
    }
	try{
        update OppId;
    }catch(DMLException d){
        system.debug('\n\nERROR UPDATING Opportunity: '+d.getDMLMessage(0));
    }
}

 Many thanks in advance

Regards

Valerio

 

Hi,
I would like to know if someone created a VF page where starting from 2 values in a Contact [Relationship (1 to 10) and Influence (1 to 10)] used it as coordinates for a Graph in order to create a Matrix where I can visualize all the contacts linked to an Account in the related position.
Here you can find the Example of my expected result.
Many thanks in advance for your help.
Regards
Valerio

Hi,

 

I'm not a developer but I need an help for a Trigger that I tried to do.

 

I need to store all the product code(s) linked to an opportunity after insert/delete/update/undelete of a Product.

 

I've created a field in the Opportunity table (Product_Code_Selected__C) in order to store this information.

 

I'm able to get the PricebookEntryID but I need to store the product code. I Know that I have to do a sub query, but I tried without success. 

I have also an issue if I try to delete a product from an opportunity (I'm expecting that the field will be updated without the product deleted) but I'm receiving this error message:

 

OppProductCode_trigger: execution of AfterDelete

caused by: System.NullPointerException: Attempt to de-reference a null object

Trigger.OppProductCode_trigger: line 5, column 1

 

Could you please help me? This is the Aper Trigger

 

trigger OppProductCode_trigger on OpportunityLineItem (after delete, after insert, after undelete, 
after update) {
    //Setup the array to hold the Id to Iterate through
    Set<Id> CurrentOppId = new Set<Id>();
        for (OpportunityLineItem OppLnItem : Trigger.new){
        	CurrentOppId.add(OppLnItem.OpportunityId);
    }
 
    	// Create List of One Opportunity Id to Update
	    List<Opportunity> OppId = 
	    	[Select o.Id from Opportunity o
	    		where o.Id in: CurrentOppId];

    		// Create List of Opportunity Products to iterate through later
		    List<OpportunityLineItem> relatedOLIs =
			    [Select oli.Id, oli.PricebookEntryId from OpportunityLineItem oli
	    			where oli.OpportunityId in: CurrentOppId];

    // Used to not add ',' on first OpportunityLineItem PricebookEntryID
    Boolean isFirst = true;
       
    //Iterate through the Opportunity to Update (should be just one record)
    for (Opportunity opp2 : OppId){
	    opp2.Product_Code_Selected__c = '';

	    //Iterate through the line items to add PricebookEntryID data to the Opportunity
	    for (OpportunityLineItem oli2 : relatedOLIs){
	                   	if(isFirst) {
	                		opp2.Product_Code_Selected__c += oli2.PricebookEntryId;
	                		isFirst = false;
	            		}
	            		else {
	                		opp2.Product_Code_Selected__c += ',' + oli2.PricebookEntryId; 
		            	}
	    }
    }
	try{
        update OppId;
    }catch(DMLException d){
        system.debug('\n\nERROR UPDATING Opportunity: '+d.getDMLMessage(0));
    }
}

 Many thanks in advance

Regards

Valerio