• Keokhan
  • NEWBIE
  • 30 Points
  • Member since 2013

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

How do I write a test class to check whether a new Opportunity Contact Role was created from a custom field based off of an Opportunity insert or update?

 

I've written a Trigger (mostly repurposed code) to create an Opportunity Contact Role when an Opportunity is created or updated with a Primary Contact (custom Contact Lookup field).  I'm not sure how to write a test class for it, though.  The trigger seems to work well enough.

 

Here's what it does: If a new Opp is created with a Primary Contact (required field), a new OCR is created as a Primary Contact and with a Decision Maker Role.  If the Primary Contact is changed, the old OCR is deleted, then the recreated as an OCR with the Influencer Role, while the new Primary Contact is created as the Primary with a Decision Maker Role.  If the insert fails, a friendly error message is thrown.

 

trigger OnOpportunityPrimaryContactRole on Opportunity (after insert, after update) {    
    
    List<OpportunityContactRole> newCRList = new List<OpportunityContactRole>();
    List<OpportunityContactRole> oldCRList = new List<OpportunityContactRole>();
    Set<Id> OppId = new Set<Id>();
    Set<Id> ContactId = new Set<Id>();  
    
    for(Opportunity o: Trigger.new)
    {
        //Checks if the Opportunity is being inserted
        if(Trigger.isInsert)
        {
            if(o.Primary_Contact__c != null)
            {
                //Creates the new OCR
                newCRList.add(new OpportunityContactRole (ContactId=o.Primary_Contact__c, OpportunityId=o.Id, Role='Decision Maker',IsPrimary=TRUE));                                                                       
            }
        }
        else if(o.Primary_Contact__c != null && Trigger.oldMap.get(o.Id).Primary_Contact__c != null)
            	{
                	//Gets the Contact and Opportunity Id from the prior values and adds to this set              
                	Opportunity oldOppObj=Trigger.oldMap.get(o.Id);
                    OppId.add(OldoppObj.id);
                	ContactId.add(oldOppObj.Primary_Contact__c);
                    	//Selects old OCRs
        				if (OppId.size()>0) oldCRList=[Select Id from OpportunityContactRole where ContactId in : ContactId  and OpportunityId in : OppId];
      
        				//Deletes old OCRs
       					if (oldCRList.size()>0) delete oldCRList;
                    
                		if(oldOppObj.Primary_Contact__c != o.Primary_Contact__c)
                    	{
                        	newCRList.add(new OpportunityContactRole (ContactId=o.Primary_Contact__c, OpportunityId=o.Id, Role='Decision Maker',IsPrimary=TRUE));
                            newCRList.add(new OpportunityContactRole (ContactId=oldOppObj.Primary_Contact__c, OpportunityId=o.Id, Role='Influencer',IsPrimary=False));
                    	}
                }      
    }  
    try
    {
        //Inserts new OCRs
        if(newCRList.size()>0) insert newCRList;
    }    
    catch(Exception e)
    {
        System.debug(e);
        trigger.new[0].addError('Uh Oh...  A technical error has occurred creating the Opportunity Contact Role. Please email the SFDC admin or try again later.');
    }
}

 

 

I have two fields FirstName , LastName.  i have written 2 workflows for proper formation of contacts like if we give First name Like "aNVesh" it returns as "Anvesh" . Like that for Last Name also 'pAiDakuLA' it returns "Paidakula". it is fine but it is not updating for middle name means in my FirstName Text box if i give  "aNVesH kUMar" , it returns "Anvesh kUMar".

like that if i give in Last Name Field Text box "pAIdaKUla  sRiP". it returns Paidakula sRiP. please give me sollution for this. here is formulaes.i have added a formulae with space but it not works. for the fields if i give space and enter any text it should also correct in proper case like "aNVesH kUMar" to "Anvesh Kumar"

for firstname field : UPPER(LEFT(FirstName,1))&Mid(LOWER(FirstName), 2, Len(FirstName)-1)

for Lastname field : UPPER(LEFT(LastName,1))&Mid(LOWER(LastName), 2, Len(LastName)-1)

we dont have field for Middle name.

so i have written formulae something like this to reflect the word after space. but not  working.

UPPER(LEFT(FirstName,1))&Mid(LOWER(FirstName), 2, Len(FirstName)-1)& &Find(' ',UPPER(Left(Firstname,1)&Mid(Lower(FirstName))2,Len(FirstName)-1))))
  • September 02, 2013
  • Like
  • 0

How do I write a test class to check whether a new Opportunity Contact Role was created from a custom field based off of an Opportunity insert or update?

 

I've written a Trigger (mostly repurposed code) to create an Opportunity Contact Role when an Opportunity is created or updated with a Primary Contact (custom Contact Lookup field).  I'm not sure how to write a test class for it, though.  The trigger seems to work well enough.

 

Here's what it does: If a new Opp is created with a Primary Contact (required field), a new OCR is created as a Primary Contact and with a Decision Maker Role.  If the Primary Contact is changed, the old OCR is deleted, then the recreated as an OCR with the Influencer Role, while the new Primary Contact is created as the Primary with a Decision Maker Role.  If the insert fails, a friendly error message is thrown.

 

trigger OnOpportunityPrimaryContactRole on Opportunity (after insert, after update) {    
    
    List<OpportunityContactRole> newCRList = new List<OpportunityContactRole>();
    List<OpportunityContactRole> oldCRList = new List<OpportunityContactRole>();
    Set<Id> OppId = new Set<Id>();
    Set<Id> ContactId = new Set<Id>();  
    
    for(Opportunity o: Trigger.new)
    {
        //Checks if the Opportunity is being inserted
        if(Trigger.isInsert)
        {
            if(o.Primary_Contact__c != null)
            {
                //Creates the new OCR
                newCRList.add(new OpportunityContactRole (ContactId=o.Primary_Contact__c, OpportunityId=o.Id, Role='Decision Maker',IsPrimary=TRUE));                                                                       
            }
        }
        else if(o.Primary_Contact__c != null && Trigger.oldMap.get(o.Id).Primary_Contact__c != null)
            	{
                	//Gets the Contact and Opportunity Id from the prior values and adds to this set              
                	Opportunity oldOppObj=Trigger.oldMap.get(o.Id);
                    OppId.add(OldoppObj.id);
                	ContactId.add(oldOppObj.Primary_Contact__c);
                    	//Selects old OCRs
        				if (OppId.size()>0) oldCRList=[Select Id from OpportunityContactRole where ContactId in : ContactId  and OpportunityId in : OppId];
      
        				//Deletes old OCRs
       					if (oldCRList.size()>0) delete oldCRList;
                    
                		if(oldOppObj.Primary_Contact__c != o.Primary_Contact__c)
                    	{
                        	newCRList.add(new OpportunityContactRole (ContactId=o.Primary_Contact__c, OpportunityId=o.Id, Role='Decision Maker',IsPrimary=TRUE));
                            newCRList.add(new OpportunityContactRole (ContactId=oldOppObj.Primary_Contact__c, OpportunityId=o.Id, Role='Influencer',IsPrimary=False));
                    	}
                }      
    }  
    try
    {
        //Inserts new OCRs
        if(newCRList.size()>0) insert newCRList;
    }    
    catch(Exception e)
    {
        System.debug(e);
        trigger.new[0].addError('Uh Oh...  A technical error has occurred creating the Opportunity Contact Role. Please email the SFDC admin or try again later.');
    }
}