• jasloz
  • NEWBIE
  • 5 Points
  • Member since 2008

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 13
    Replies
We have a requirement to create a custom field on the Opportunity object with a lookup to Contacts to store the BU Business Development contact (we have a requirement to make this a mandatory field).  We have created a field BU_Business_Development__c (we actually have a need for 4 other fields but I will come to that later). The business would like to create a Contact Role for the contact selected in the BU Business Development field linking back to the opportunity. I have created a trigger that successfully populates the OpportunityContactRole (and manages deletions & updates from the BU Business Development field. 

trigger Opportunity_NewBUBD on Opportunity (after insert, after update) { for (Opportunity o : Trigger.new) { if (trigger.isUpdate) { for (Integer i = 0; i < Trigger.old.size(); i++) { if (trigger.Old[i].BU_Business_Development__c != Null) { if (trigger.Old[i].BU_Business_Development__c != o.BU_Business_Development__c) { OpportunityContactRole [] oDWs = [select id from OpportunityContactRole where OpportunityId = :trigger.old[i].id and ContactId=:trigger.old[i].BU_Business_Development__c and Role = 'BU Business Development']; delete oDWs; } } Integer oDWs1 = [select count() from OpportunityContactRole where OpportunityId = :trigger.old[i].id and ContactId =:trigger.new[i].BU_Business_Development__c and Role = 'BU Business Development']; if (oDWs1 == 0 & o.BU_Business_Development__c != Null) { OpportunityContactRole OCR1 = new OpportunityContactRole (OpportunityId=o.id, ContactId =o.BU_Business_Development__c, Role = 'BU Business Development'); insert OCR1; } } } if (trigger.isInsert) { if (o.BU_Business_Development__c != Null) { OpportunityContactRole OCR1 = new OpportunityContactRole (OpportunityId=o.id, ContactId = o.BU_Business_Development__c, Role = 'BU Business Development'); insert OCR1; } } } }

 

 
  1. I would be very grateful on your views of the design of the apexcode?
  2. I believe that some of this code should be called using a function to facility the reuse of the code; can you provide some suggestions on this? (i.e. rows 26-30 and 38-42)
  3.  I would like to use dynamic code utilising an array of fields that should be ‘monitored’ so that one piece of code can be used to populate the OpportunityContactRole for many fields.  (This would need to have a variable for the Role and the fieldname.) Any ideas?
Thanks in advance Jason
  • November 03, 2009
  • Like
  • 0
Problem
We are looking to create a custom formula in the Opportunity Object.
 
For each opportunity we record the 'Opportunity Start Date' and 'Opportunity End Date'; these dates indicate the term of the opportunity which may be as short as a month or as long as 5 years.  We are looking for a formula to calculate (on a prorata basis) the Revenue expected in the Current Fiscal Year.
 
Fields that are relevent
'Opportunity Start Date' API = Opportunity_Start_Date__c - This is the expected works start date, not necessarily the same as the Close Date.
'Opportunity End Date' API = Opportunity_End_Date__c - This is the expected works end date.
'Amount' API = Amount - This is the order amount.
'In Year Value' API = In_Year_Value__c - This is the value of revenue expected from the opportunity in the current Fiscal Year.
 
Ideal Solution
The formula should be as simple as
 
In_Year_Value__c = (Amount / (Opportunity_End_Date__c – Opportunity_Start_Date__c)) * (min(Opportunity_End_Date__c, Current_Fiscal_Year_End) – max(Opportunity_Start_Date__c , Current_Fiscal_Year_Start))
 
However Salesforce has a number of issues with this formula.
 
1) Max & Min cannot be used on Dates.
2) There is no ability to use Current_Fiscal_Year_End in the formula
 
I have not created any custom development at this time but am happy to give anything a go to get this to work as it really is a business critical calculation.
 
Many thank in advance.
 
Jason


Message Edited by jasloz on 06-29-2008 02:43 AM

Hello,

We have added a field "Assign to" on Case (a lookup on user) to let Salesforce.com users the ability to change the owner of a case from the case list. The Case owner is then updated via a trigger (before update on case) that is copying the "Assign to" field value into the Case.owner field.

The trigger works fine and the case owner is updated correctly.

The issue is that when the case owner is updated via that procedure, an automatic email is sent to the new Case owner to warn him that he s now assigned to the case. We do NOT want this email to be sent.

How can we prevent that email to be sent? Is there a piece of code available for that?


Here is the code I'm using currently:

trigger AssignToCaseOwner on Case (before update) {
	Integer i = 0;
	for(Case theCase : System.Trigger.new)
	{
		// only executed if the case owner is not changed as well 
		// and if the case owner is not already the "Assign to" user
		if(theCase.OwnerId == System.Trigger.old.get(i).OwnerId && 
			theCase.Assign_to__c != null && 
			theCase.Assign_to__c != System.Trigger.old.get(i).Assign_to__c && 
			theCase.OwnerId != theCase.Assign_to__c)
		{
			theCase.OwnerId = theCase.Assign_to__c;
			// set DML options to remove the auto email -> NOT WORKING
			Database.DMLOptions dlo = new Database.DMLOptions();
			dlo.EmailHeader.triggerUserEmail = false;
			theCase.setOptions(dlo);
		}
		
		// Make the "Assign to" field empty as we do not need the value 
		// to be stored in the record
		theCase.Assign_to__c = null;
	
		i++;
	}
}

 

 

Thanks on beforehand,
Martin HUBERT.


I have a simple trigger on Leads that needs to know if the Owner is a User or a Queue.

 

Can anyone show me the (proably very) simple solution to this? My code is below, with the 'real world' question if Owner = Queue.

 

 

 Set<id> ownerIds = new Set<id>();
    for (Lead l : Trigger.new)
        ownerIds.add(l.OwnerId);
        
    Map<id, User> owners = new Map<id, User>([Select Id from User Where Id in :ownerIds]);      
    
    for (Lead l : Trigger.new)
        if(l.Owner = queue){
        //l.Owner_Usable__c = null;
        } else {
        //l.Owner_Usable__c  = owners.get(l.OwnerId).Id;
        }

 

 

 

Hi I have code that successfully clones the opportunity related list for opportunity partner.  Now that we have included products for the opportunity I see a new button that has a little picklist option arrow on it.  This is built in and allows cloning the opportunity by itself or with the products. 

 

This is an issue for my custom cloning button that was cloning the opportunity with related partner record(s)!

I posted that code at: http://boards.developerforce.com/t5/Apex-Code-Development/Code-behind-the-Clone-Button/m-p/201476#M34627

 

Does anyone have code that clones the related products for a given opportunity?  I need to override the default clone button or hide it and use my own and figure out how to incorporate my code and the products cloning code so that all records successfully save or roll-back.

 

Does anyone have cloning code for products or another related list likeattachments, activities etc?  Products is on the plate now but I can see the future where users want to clone more related data!!!

Can the upsert with relationships be used with Data Loader's command line?  If so, are there any examples of an SOQL statement that will do this?  I am attempting to pull Customer data from Oracle R12 and migrate this to SF but need to also include location and owner (account manager) data.  One is a reference from Account and the other is a reference to Account (child).  I can pull the data from Oracle using DataLoader's command line and upsert SF but I don't know how to get the owner id (unknown in Oracle) or the location id (also unknown in Oracle).  This is a one way interface.
We have a requirement to create a custom field on the Opportunity object with a lookup to Contacts to store the BU Business Development contact (we have a requirement to make this a mandatory field).  We have created a field BU_Business_Development__c (we actually have a need for 4 other fields but I will come to that later). The business would like to create a Contact Role for the contact selected in the BU Business Development field linking back to the opportunity. I have created a trigger that successfully populates the OpportunityContactRole (and manages deletions & updates from the BU Business Development field. 

trigger Opportunity_NewBUBD on Opportunity (after insert, after update) { for (Opportunity o : Trigger.new) { if (trigger.isUpdate) { for (Integer i = 0; i < Trigger.old.size(); i++) { if (trigger.Old[i].BU_Business_Development__c != Null) { if (trigger.Old[i].BU_Business_Development__c != o.BU_Business_Development__c) { OpportunityContactRole [] oDWs = [select id from OpportunityContactRole where OpportunityId = :trigger.old[i].id and ContactId=:trigger.old[i].BU_Business_Development__c and Role = 'BU Business Development']; delete oDWs; } } Integer oDWs1 = [select count() from OpportunityContactRole where OpportunityId = :trigger.old[i].id and ContactId =:trigger.new[i].BU_Business_Development__c and Role = 'BU Business Development']; if (oDWs1 == 0 & o.BU_Business_Development__c != Null) { OpportunityContactRole OCR1 = new OpportunityContactRole (OpportunityId=o.id, ContactId =o.BU_Business_Development__c, Role = 'BU Business Development'); insert OCR1; } } } if (trigger.isInsert) { if (o.BU_Business_Development__c != Null) { OpportunityContactRole OCR1 = new OpportunityContactRole (OpportunityId=o.id, ContactId = o.BU_Business_Development__c, Role = 'BU Business Development'); insert OCR1; } } } }

 

 
  1. I would be very grateful on your views of the design of the apexcode?
  2. I believe that some of this code should be called using a function to facility the reuse of the code; can you provide some suggestions on this? (i.e. rows 26-30 and 38-42)
  3.  I would like to use dynamic code utilising an array of fields that should be ‘monitored’ so that one piece of code can be used to populate the OpportunityContactRole for many fields.  (This would need to have a variable for the Role and the fieldname.) Any ideas?
Thanks in advance Jason
  • November 03, 2009
  • Like
  • 0
Hi
I have written an apex trigger in Sandbox. It is working fine. I have written apex class to test the trigger and when i run the tests, it is showing 94% coverage.
But when i try to deploy it into production using eclipse force.com ide and validate deployment, it throws an error saying
Test coverage of selected Apex Trigger is 0%, at least 1% test coverage is required
I have gone through dev community and tried many options like changing api version to 11.0 in xml files but it was of no use.

Please suggest me a solution for this asap.

Thanks.
Iqbal
  • July 13, 2008
  • Like
  • 0
Problem
We are looking to create a custom formula in the Opportunity Object.
 
For each opportunity we record the 'Opportunity Start Date' and 'Opportunity End Date'; these dates indicate the term of the opportunity which may be as short as a month or as long as 5 years.  We are looking for a formula to calculate (on a prorata basis) the Revenue expected in the Current Fiscal Year.
 
Fields that are relevent
'Opportunity Start Date' API = Opportunity_Start_Date__c - This is the expected works start date, not necessarily the same as the Close Date.
'Opportunity End Date' API = Opportunity_End_Date__c - This is the expected works end date.
'Amount' API = Amount - This is the order amount.
'In Year Value' API = In_Year_Value__c - This is the value of revenue expected from the opportunity in the current Fiscal Year.
 
Ideal Solution
The formula should be as simple as
 
In_Year_Value__c = (Amount / (Opportunity_End_Date__c – Opportunity_Start_Date__c)) * (min(Opportunity_End_Date__c, Current_Fiscal_Year_End) – max(Opportunity_Start_Date__c , Current_Fiscal_Year_Start))
 
However Salesforce has a number of issues with this formula.
 
1) Max & Min cannot be used on Dates.
2) There is no ability to use Current_Fiscal_Year_End in the formula
 
I have not created any custom development at this time but am happy to give anything a go to get this to work as it really is a business critical calculation.
 
Many thank in advance.
 
Jason


Message Edited by jasloz on 06-29-2008 02:43 AM
Hi Experts,
 
  Could any one help me forming the below join?
 I need to retrive an User record  from User table and then get the all the users who are below this user role in the heirarchy.  
For this the logic would be: get the userroleid from USER table and read the roleid from USERROLE table where parent roleid equals the roleid from user table. by this we can get the users one step below in the heirarchy and then user all the returned roleid's as parent roleid's and get the next level of users. Like this drill down to the bottom and get all the roleid's below the user record then retrive all the userid's linked to these roleid's.
 
 
Normal SQL would be like
select roleid from userrole where parentroleid = (Select roleid from user where username like 'x%').
 
Also how to write the nested sql(get next level of roleid's in the heirarchy)?
Can any one help me writing salesforce sql for this?
 
Thanks,
Anil