• LiorG
  • NEWBIE
  • 0 Points
  • Member since 2011

  • Chatter
    Feed
  • 0
    Best Answers
  • 1
    Likes Received
  • 0
    Likes Given
  • 15
    Questions
  • 11
    Replies

Check it out here: http://www.liorgot.com/1/post/2013/03/using-a-trigger-as-rollup-field-for-lookup-relationship.html

 

 

trigger TotalChargedCalculator on OpportunityLineItem (after insert, after update, after delete) {
 	
 	Set<Id> customObjIds = new Set<Id>();
 	
 	// Grab Ids of all the Custom Object records affected by operation
 	for (OpportunityLineItem oli : trigger.new == null ? trigger.old : trigger.new) {
 		if (trigger.isDelete) {
 			if (oli.Custom_Object__c != null)
 				customObjIds.add(oli.Custom_Object__c);
		 		} else {
 			if (oli.Custom_Object__c != null)
 				customObjIds.add(oli.Custom_Object__c);
 			if (trigger.isUpdate) {
 				if (trigger.oldMap.get(oli.Id).Custom_Object__c != null && oli.Custom_Object__c == null) {
 					customObjIds.add(trigger.oldMap.get(oli.Id).Custom_Object__c);
 				}
 				 			}
			 		}
 	}
 
 	if (customObjIds.size() > 0) {
 		Map<Id, Custom_Object__c> customObjsToUpdateMap = new Map<Id, Custom_Object__c>([
 			Select Id, Total_Charged__c From Custom_Object__c Where Id IN :customObjIds]);
 
 		// Query for an Aggregate Result List finds the sum of all the Opp line Item's total price per Custom Object
 	    AggregateResult[] customObjIdTotalChargedAgg = [
 	    	Select SUM(TotalPrice) totalCharged, Custom_Object__c
 	        From OpportunityLineItem
  	        Where Custom_Object__c = :customObjIds
 	        Group By Custom_Object__c];
       
 		// Create Map of custom object Id and sum of total price of related Opp Line Items
 		Map<Id, Decimal> customObjIdToTotalChargedMap = new Map<Id, Decimal>();
 		for (AggregateResult agg : customObjIdTotalChargedAgg)
  			customObjIdToTotalChargedMap.put((Id)agg.get('Custom_Object__c'), (Decimal)agg.get('totalCharged'));
 
 	    // Get new total charge value from customObjIdToTotalChargedMap, if it doesn't exist in the map, it means TotalPrice Sum is 0
  	    for (Id cId : customObjsToUpdateMap.keySet())
 	    	customObjsToUpdateMap.get(cId).Total_Charged__c = customObjIdToTotalChargedMap.get(cId) != null ? customObjIdToTotalChargedMap.get(cId) : 0;
 
 	    update customObjsToUpdateMap.values();
 
			 	}
 	 }

 

  • March 14, 2013
  • Like
  • 1
  • November 07, 2012
  • Like
  • 0

When creating an email task under the opp level, I'd like to pre-fill the "To" field with the primary contact. Is this possible?

  • July 20, 2012
  • Like
  • 0

I have a trigger on Opportunities that makes an update on a list of Accounts, after looping through the Opps. How do I handle an exception thrown at the DML statement - update Accounts and show the error on the right opp?

 

 

  • July 06, 2012
  • Like
  • 0

Sometimes I have trigger errors pop up with a validation rule error embedded inside. The reason is because the trigger got an error because the validation rule raised an error. How do I make only the validation rule error pop up?

  • May 03, 2012
  • Like
  • 0

My trigger is this:

 

trigger RBOppClone on Opportunity (after update) {
        
        Set<Opportunity> oppSet = new Set<Opportunity>();
        for (Opportunity oppRec: trigger.new) {
            oppSet.add(oppRec);
        }
        
        for (Opportunity oppRec: oppSet) {
            Opportunity oldOpp = trigger.oldMap.get(oppRec.Id);
            if (oppRec.Product_Sold__c == 'Business Product' &&
               oppRec.StageName == 'Stage 5 - Closed - Won' &&
               oldOpp.StageName != 'Stage 5 - Closed - Won') {
                Opportunity newOpp = oppRec.clone();
                newOpp.StageName = 'Referral Credit - Dummy Stage';
                if (oppRec.Amount == null || oppRec.Amount < 50.00) {
                    newOpp.Amount = 500;
                } else {
                    newOpp.Amount = 1000;
                }
                insert newOpp;
            }
        }

}

 

Sometimes 2 opportunities are inserted instead of one in the same update. Anyone know why this might be happening? Also, I'm aware this trigger isn't bulk-uploads-safe.

  • April 18, 2012
  • Like
  • 0

I'm trying to build a test class for a class that modifies the price of a product on an opportunity. This requires programmatically adding a product to an opportunity I made in my test class. I made the opportunity, but I don't know how to add a product to it.

  • April 02, 2012
  • Like
  • 0

I'm trying to add the equivalent of {!ApprovalRequest.Internal_URL} on my VF email template.

  • February 16, 2012
  • Like
  • 0

I have a custom object: Refund Approval Form, that's in a master relationship with Opportunity. I have an approval process connected to it and I'd like the approval requests email alerts to have the approval history in the body. 

 

I think I have to create a VF email template and a controller to query the history with the object ProcessInstance, but I don't know where to start.

 

Thanks

  • February 15, 2012
  • Like
  • 0

I'm trying to fetch opportunities with the opp owner's full name

 

List<Opportunity> opps = [SELECT Amount, CloseDate, (SELECT Fullname, Id from User where Id = :OwnerId)
            FROM Opportunity
            LIMIT 100];

 I know my query is wrong, but it may help explain what I'm trying to achieve.

  • February 10, 2012
  • Like
  • 0

I have an Opportunity Controller that deletes each product on the Opportunity saved. Below is the snippet of code that does this. Olis comes out blank from this query. What am I doing wrong?

 

List<OpportunityLineItem> olis = [Select Id, pricebookentryid From OpportunityLineItem Where Id = :ApexPages.currentPage().getParameters().get('id')];
for (OpportunityLineItem oli : olis) {
    delete oli;
}

 

  • January 04, 2012
  • Like
  • 0

Whenever an opportunity's closed won, we have several workflows in place that send out emails using a template. 

 

My question: can I make a trigger (or workflow) on emails sent out that creates an activity with the email's conent in the subject/comments? 

  • December 15, 2011
  • Like
  • 0

When a user clicks "new" to make a new opportunity, they are faced with an entry form to enter in information. Is it possible to pre-populate one of these fields on this entry form using a trigger? 

  • October 14, 2011
  • Like
  • 0

 I need to deactivate or delete a trigger from production asap. Can someone help me out?

 

I am the sys admin with the Unlimited Edition.

  • October 12, 2011
  • Like
  • 0

I'm a newbie to apex and not quite sure how to bulkify my trigger. Help would be appreciated!

 

 

 

 

trigger opp_to_acct_update on Opportunity (before insert, before update) {
   

List<Opportunity> oppupdate= new List<Opportunity>();      

for(Opportunity oppRec: trigger.new){           

Id acctId = oppRec.AccountId;       

Account acctRec = [select Optimus_User_ID__c, Username__c , OwnerId from Account where Id = :acctId];                        

if (oppRec.Account_Owner__c == null) {           

oppRec.Account_Owner__c = acctRec.OwnerId;       

} else {           

acctRec.OwnerId = oppRec.Account_Owner__c;       

}               

 

if (acctRec.Optimus_User_ID__c == null || acctRec.Username__c == null) {           acctRec.Optimus_User_ID__c = oppRec.Optimus_User_ID__c;           

acctRec.Username__c = oppRec.Username__c;       

} else {           

oppRec.Optimus_User_ID__c = acctRec.Optimus_User_ID__c;           

oppRec.Username__c = acctRec.Username__c;       

}               

 

oppupdate.add(oppRec);               

 

update acctRec;       

}      

 }

 

  • October 10, 2011
  • Like
  • 0

Check it out here: http://www.liorgot.com/1/post/2013/03/using-a-trigger-as-rollup-field-for-lookup-relationship.html

 

 

trigger TotalChargedCalculator on OpportunityLineItem (after insert, after update, after delete) {
 	
 	Set<Id> customObjIds = new Set<Id>();
 	
 	// Grab Ids of all the Custom Object records affected by operation
 	for (OpportunityLineItem oli : trigger.new == null ? trigger.old : trigger.new) {
 		if (trigger.isDelete) {
 			if (oli.Custom_Object__c != null)
 				customObjIds.add(oli.Custom_Object__c);
		 		} else {
 			if (oli.Custom_Object__c != null)
 				customObjIds.add(oli.Custom_Object__c);
 			if (trigger.isUpdate) {
 				if (trigger.oldMap.get(oli.Id).Custom_Object__c != null && oli.Custom_Object__c == null) {
 					customObjIds.add(trigger.oldMap.get(oli.Id).Custom_Object__c);
 				}
 				 			}
			 		}
 	}
 
 	if (customObjIds.size() > 0) {
 		Map<Id, Custom_Object__c> customObjsToUpdateMap = new Map<Id, Custom_Object__c>([
 			Select Id, Total_Charged__c From Custom_Object__c Where Id IN :customObjIds]);
 
 		// Query for an Aggregate Result List finds the sum of all the Opp line Item's total price per Custom Object
 	    AggregateResult[] customObjIdTotalChargedAgg = [
 	    	Select SUM(TotalPrice) totalCharged, Custom_Object__c
 	        From OpportunityLineItem
  	        Where Custom_Object__c = :customObjIds
 	        Group By Custom_Object__c];
       
 		// Create Map of custom object Id and sum of total price of related Opp Line Items
 		Map<Id, Decimal> customObjIdToTotalChargedMap = new Map<Id, Decimal>();
 		for (AggregateResult agg : customObjIdTotalChargedAgg)
  			customObjIdToTotalChargedMap.put((Id)agg.get('Custom_Object__c'), (Decimal)agg.get('totalCharged'));
 
 	    // Get new total charge value from customObjIdToTotalChargedMap, if it doesn't exist in the map, it means TotalPrice Sum is 0
  	    for (Id cId : customObjsToUpdateMap.keySet())
 	    	customObjsToUpdateMap.get(cId).Total_Charged__c = customObjIdToTotalChargedMap.get(cId) != null ? customObjIdToTotalChargedMap.get(cId) : 0;
 
 	    update customObjsToUpdateMap.values();
 
			 	}
 	 }

 

  • March 14, 2013
  • Like
  • 1

My trigger is this:

 

trigger RBOppClone on Opportunity (after update) {
        
        Set<Opportunity> oppSet = new Set<Opportunity>();
        for (Opportunity oppRec: trigger.new) {
            oppSet.add(oppRec);
        }
        
        for (Opportunity oppRec: oppSet) {
            Opportunity oldOpp = trigger.oldMap.get(oppRec.Id);
            if (oppRec.Product_Sold__c == 'Business Product' &&
               oppRec.StageName == 'Stage 5 - Closed - Won' &&
               oldOpp.StageName != 'Stage 5 - Closed - Won') {
                Opportunity newOpp = oppRec.clone();
                newOpp.StageName = 'Referral Credit - Dummy Stage';
                if (oppRec.Amount == null || oppRec.Amount < 50.00) {
                    newOpp.Amount = 500;
                } else {
                    newOpp.Amount = 1000;
                }
                insert newOpp;
            }
        }

}

 

Sometimes 2 opportunities are inserted instead of one in the same update. Anyone know why this might be happening? Also, I'm aware this trigger isn't bulk-uploads-safe.

  • April 18, 2012
  • Like
  • 0

I'm trying to add the equivalent of {!ApprovalRequest.Internal_URL} on my VF email template.

  • February 16, 2012
  • Like
  • 0

I have an Opportunity Controller that deletes each product on the Opportunity saved. Below is the snippet of code that does this. Olis comes out blank from this query. What am I doing wrong?

 

List<OpportunityLineItem> olis = [Select Id, pricebookentryid From OpportunityLineItem Where Id = :ApexPages.currentPage().getParameters().get('id')];
for (OpportunityLineItem oli : olis) {
    delete oli;
}

 

  • January 04, 2012
  • Like
  • 0

 I need to deactivate or delete a trigger from production asap. Can someone help me out?

 

I am the sys admin with the Unlimited Edition.

  • October 12, 2011
  • Like
  • 0

I'm a newbie to apex and not quite sure how to bulkify my trigger. Help would be appreciated!

 

 

 

 

trigger opp_to_acct_update on Opportunity (before insert, before update) {
   

List<Opportunity> oppupdate= new List<Opportunity>();      

for(Opportunity oppRec: trigger.new){           

Id acctId = oppRec.AccountId;       

Account acctRec = [select Optimus_User_ID__c, Username__c , OwnerId from Account where Id = :acctId];                        

if (oppRec.Account_Owner__c == null) {           

oppRec.Account_Owner__c = acctRec.OwnerId;       

} else {           

acctRec.OwnerId = oppRec.Account_Owner__c;       

}               

 

if (acctRec.Optimus_User_ID__c == null || acctRec.Username__c == null) {           acctRec.Optimus_User_ID__c = oppRec.Optimus_User_ID__c;           

acctRec.Username__c = oppRec.Username__c;       

} else {           

oppRec.Optimus_User_ID__c = acctRec.Optimus_User_ID__c;           

oppRec.Username__c = acctRec.Username__c;       

}               

 

oppupdate.add(oppRec);               

 

update acctRec;       

}      

 }

 

  • October 10, 2011
  • Like
  • 0