• Jack Ins
  • NEWBIE
  • 25 Points
  • Member since 2008

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

Hello,

Been working on this too long and need some help.  I have a Master Detail relationship between Opportunity and a custom object.  I have added the Related list as I have added others to the VF Page but it is giving me the above error.  The code is very simple but I must be missing something.

Any help would be great.

Thanks Dwayne

 

<apex:outputPanel rendered="{!hasPolicy}" id="UpsellCampaignOppt">
	    <table style="width: 1100px;">
	    	<tr>
	    	<td colspan="1">
				<apex:pageBlock tabStyle="Campaign">
	    			<apex:pageBlockSection id="UpsellCO" title="Upsell Campaign Opportunities" collapsible="true" columns="1">
	    				<apex:relatedList list="Upsell_Campaign_Opportunities__r" subject="{!policy.Id}"/>
	   				</apex:pageBlockSection>
	   				<script>twistSection(document.getElementById("{!$Component.UpsellCO}").childNodes[0].childNodes[0]); </script>
	 			</apex:pageBlock>
			</td>
	 		</tr>
		</table>
	</apex:outputPanel>

 

Hi Everyone,

 

I am trying to build a VF Page that will sit in a standard Contact Page Layout.  I know that Account Contact Role relates to Account and there is a related list on Accounts.  What we are looking for is a way to show these Account Contact Roles on the Contact.

 

My best assumption is to create a VF Page that can be added to the standard page layout with a apex class related to the Account Contact Role where the ContactId from the AccountContactRole object is equal to the current Contact Id.

 

The following code is my VF page and Apex Class.  I feel there may already be a solution for this so I am totally open to new suggestions or fixes to my current code and logic.


Thank You Very Much.

 

<apex:page standardcontroller="Contact" recordSetVar="contacts" extensions="AccountContactRoles">
 <apex:pageBlock title="Account Contact Roles">
        <apex:form id="theForm1">
            <apex:pageBlockSection >
                <apex:dataList value="{!AccountContactRoles}" var="acr" type="1">
                    {!acr.Id}
                </apex:dataList>
            </apex:pageBlockSection>
            <apex:panelGrid columns="2">
                <apex:commandLink action="{!previous}">Previous</apex:commandlink>
                <apex:commandLink action="{!next}">Next</apex:commandlink>
            </apex:panelGrid>
        </apex:form> 
    </apex:pageBlock>
</apex:page>

 This is my VF Page Coding

 

public with sharing class AccountContactRoles {
	private final Contact acr;
	public Id cntId{get;set;}
	
	public AccountContactRoles(ApexPages.StandardSetController controller){
		this.acr = (Contact)controller.getRecord();
	}
	public ApexPages.Standardsetcontroller acrRecords{
		get{
			if(acrRecords == null){
				cntId = ApexPages.currentPage().getParameters().get('Id');
				acrRecords = new ApexPages.Standardsetcontroller(
				Database.getQueryLocator([Select Id, AccountId, ContactId, Role, IsPrimary From AccountContactRole 
				Where ContactId=:cntId]));
			}
			return acrRecords;
		}
		private set;
	}
	public List<AccountContactRole> getAccountContactRoles(){
		return (List<AccountContactRole>) acrRecords.getRecords();
	}
}

 This is my Apex Class.

Hi Everyone. 

I have a trigger on the Contract object that runs After Update.  My question is the trigger is only good for updating particular records but will fire after every record is updated.  To minimize the code that is running the first section of the code runs a set<String> to determine the Id's of the Contract records that will be effected.  After the Set is returned I have an if statement of:

 

if(!setC.isEmpty()) { do my stuff }

 

is this good practice so that the code section of "do my stuff" does not always run if the records being updated, based on the criteria of a "if" statement in my set<>, does not match?


Thanks :)

Hello All,

I have been able to write a trigger with "bulkification" but I am having problems understanding how to write the test class to cover all lines.

 

Here is the code that I am trying to cover.

 

trigger ContactOpportunityAssoc on Contact (after insert) {
set<String> custnmbr = new set<String>();
		for (Contact c: Trigger.new){
            if (c.cnt_plcsc_CustNmbr_ExtID__c != null){custnmbr.add(c.cnt_plcsc_Customer_Number__c);}
        }
        
Map<String,Id> mapOpps = new Map<String,Id>();
/*List all Contacts that have the customer number the same as the loaded custnmbr set*/      
		for(Opportunity opp: [Select Id, Customer_Number__c 
							From Opportunity 
							Where Customer_Number__c IN: custnmbr]){mapOpps.put(opp.Customer_Number__c , opp.Id);}

set<String> oppids = new set<String>();
		for(Opportunity opp2: [Select Id, Customer_Number__c 
								From Opportunity 
								Where Customer_Number__c IN: custnmbr]){oppids.add(opp2.Id);}

Map<String,Id> mapCnts = new Map<String,Id>();
/*List all Contacts that have the customer number the same as the loaded custnmbr set*/      
		for(Contact cnt: Trigger.new){mapCnts.put(cnt.cnt_plcsc_Customer_Number__c , cnt.Id);}
		
/*Loop through the main list of Cnts*/
List<OpportunityContactRole> ocr = new List<OpportunityContactRole>();
List<Opportunity> oppsList = [Select Id, Customer_Number__c From Opportunity Where Customer_Number__c In: custnmbr];

			for(Opportunity o2: oppsList){
				if(mapOpps.containsKey(o2.Customer_Number__c)){
						OpportunityContactRole ocr2 = new OpportunityContactRole();
						ocr2.ContactId = mapCnts.get(o2.Customer_Number__c);
						ocr2.OpportunityId = o2.Id;
						ocr2.IsPrimary = true;
						ocr2.Role = 'Primary Insured';
						ocr.add(ocr2);
				}
			}
/*Insert the new list of OCR's*/
	if(!ocr.isEmpty()){
		insert ocr;}
}

 Here is my sad attempt at code coverage:

Contact cntExisting = new Contact(LastName = 'Tester');
			cntExisting.FirstName = 'Test';
			cntExisting.RecordTypeId = '01240000000DVfaAAG';
			cntExisting.Email='test@hanover.com';
			cntExisting.cnt_plcsc_CustNmbr_ExtID__c = '123456789INS01';
			cntExisting.cnt_plcsc_Customer_Number__c='123456789';
			cntExisting.MailingPostalCode = '01453';
			String cntId = cntExisting.Id;
		insert cntExisting;
		
		cntExisting = [Select Id, Name, cnt_plcsc_Customer_Number__c from Contact Where Id=:cntExisting.Id];
    	System.assertEquals('123456789', cntExisting.cnt_plcsc_Customer_Number__c);
    	
    	Map<String,Id> mapCntpRecs = new Map<String,Id>();
    		for(Contact mapcnts:[Select Id, cnt_plcsc_Customer_Number__c from Contact Where cnt_plcsc_Customer_Number__c=:cntExisting.cnt_plcsc_Customer_Number__c]){
    			mapCntpRecs.put(mapcnts.cnt_plcsc_Customer_Number__c, mapcnts.Id);}
    		
    	Contact cntExisting2 = new Contact(LastName = '2Tester');
			cntExisting2.FirstName = 'Test';
			cntExisting2.RecordTypeId = '01240000000DVfaAAG';
			cntExisting2.Email='test2@hanover.com';
			cntExisting2.cnt_plcsc_CustNmbr_ExtID__c = '223456789INS01';
			cntExisting2.cnt_plcsc_Customer_Number__c='223456789';
			cntExisting2.MailingPostalCode = '01453';
			String cntId2 = cntExisting2.Id;
    	insert cntExisting2;
    	
    	Opportunity otherPol1 = new Opportunity(Name = '1Other Pol Name');
				otherPol1.customer_number__c = '1234abcd';
				otherPol1.Policy_Id__c = '1XX111111';
				otherPol1.LOB__c = 'Auto';
				otherPol1.stageName = 'Qualified';
				otherPol1.CloseDate = Date.today();
				otherPol1.AccountId = otherAct.Id;
				otherPol1.X1st_Nmd_Insd__c = 'Joe Test1';
				otherPol1.Zip__c = '01453';
				otherPol1.Consumer_Email__c = '1test@hanover.com';
				otherPol1.opp_plcsc_Integration_ID__c = 'A1XX111111';
				insert otherPol1;
    	System.assertEquals('A1XX111111', otherPol1.opp_plcsc_Integration_ID__c);
    	
    	Opportunity otherPol = new Opportunity(Name = 'Other Pol Name');
				otherPol.customer_number__c = '1234abcd';
				otherPol.Policy_Id__c = 'XXX111111';
				otherPol.LOB__c = 'Other';
				otherPol.stageName = 'Qualified';
				otherPol.CloseDate = Date.today();
				otherPol.AccountId = otherAct.Id;
				otherPol.X1st_Nmd_Insd__c = 'Joe Test';
				otherPol.Zip__c = '01453';
				otherPol.Consumer_Email__c = 'test@hanover.com';
		insert otherPol;
						
		Map<String,Id> mapOpps = new Map<String,Id>();
    	for(Opportunity opp: [Select Id, Customer_Number__c From Opportunity Where Customer_Number__c =: otherPol1.Customer_Number__c]) 
    	{mapOpps.put(opp.Customer_Number__c,opp.Id);}
    	
    	set<String> oppids = new set<String>(); 
		for(Opportunity opp2: [Select Id, Customer_Number__c From Opportunity 
			Where Customer_Number__c =: otherPol.Id]){oppids.add(opp2.Id);} 
			
    	OpportunityContactRole ocrInsert = new OpportunityContactRole(OpportunityId = otherPol1.Id, ContactId = cntExisting2.Id);
    			ocrInsert.IsPrimary = true;
    			ocrInsert.Role = 'Primary Insured';
    			insert ocrInsert;
    	
    	Map<String,Id> mapOppRecs = new Map<String,Id>();
    		for(OpportunityContactRole mapoppscntr:[Select Id, ContactId, OpportunityId from OpportunityContactRole Where OpportunityId=:otherPol1.Id]){
    			mapOppRecs.put(mapoppscntr.OpportunityId, mapoppscntr.Id);} 
    						     
		List<OpportunityContactRole> ocrs=new List<OpportunityContactRole>();
		List<Opportunity> oppsList = [Select Id, Customer_Number__c From Opportunity Where Id In: oppids];
		for(Opportunity o2:oppsList){
			if(mapOppRecs.containsKey(o2.Customer_Number__c)){
				OpportunityContactRole ocr2 = new OpportunityContactRole(OpportunityId = otherPol.Id, ContactId = cntExisting2.Id);
		        		ocr2.Role = 'Primary Insured';
		        		ocr2.IsPrimary = false;
		        		ocrs.add(ocr2);
		}}
		insert ocrs;

 Any help would be greatly appreciated.  :)

 

Hi all,

I need some help with covering a trigger.  My test calss gets me 58% of the way there but I would relly like to get closer to 100%.

My trigger is not big and should be easy help.  Just need pointed in the right direction.

 

Trigger that I am trying to cover:

OpportunityContactAssoc (Code Covered: 58%)
 line  source 
 1   trigger OpportunityContactAssoc on Opportunity (before insert, before update) { 
 2   List<Contact> cntList = new List<Contact>(); 
 3   List<OpportunityContactRole> oppCntRole = new List<OpportunityContactRole>(); 
 4   public String cntID {get;set;} 
 5    
 6    for (Opportunity o: Trigger.new) { 
 7    if (o.Customer_Number__c != null) 
 8    { 
 9    try {Contact cnts = [select Id, cnt_plcsc_CustNmbr_ExtID__c, cnt_plcsc_Customer_Number__c 
 10    from Contact 
 11    where cnt_plcsc_Customer_Number__c =: o.Customer_Number__c 
 12    And cnt_plcsc_CustNmbr_ExtID__c =: o.Customer_Number__c + 'Ins01' limit 1]; 
 13    cntID = cnts.Id; 
 14    cntList.add(cnts);} catch (Exception e) {System.debug('Contact Record Does Not Exist.' + cntID);} 
 15    
 16    if(cntID != null) 
 17    { 
 18    OpportunityContactRole ocr = new OpportunityContactRole(ContactId=cntID, OpportunityID = o.Id, 
 19    Role = 'Primary Insured', IsPrimary = true); 
 20    upsert new List<OpportunityContactRole>{ocr}; 
 21    
 22    for(OpportunityContactRole oppcnts : [select Id, ContactId, OpportunityId, Role, IsPrimary 
 23    from OpportunityContactRole 
 24    where IsPrimary =: false And Role =: 'Primary Insured' And OpportunityId =: o.Id]) 
 25    { 
 26    delete oppcnts; 
 27    } 
 28    
 29    } 
 30    } 
 31    else { 
 32    try { Contact cnts = [select Id, MailingStreet, MailingState, cnt_plcsc_Customer_Number__c, 
 33    MailingPostalCode, MailingCity, LastName, FirstName, Email 
 34    from Contact 
 35    where Email =: o.Consumer_Email__c And MailingPostalCode =: o.Zip__c And RecordTypeId =: '01240000000DVfa' limit 1]; 
 36    cntID = cnts.Id; 
 37    o.Customer_Number__c = cnts.cnt_plcsc_Customer_Number__c; 
 38    cntList.add(cnts);} catch (Exception e) {System.debug('Contact Record Does Not Exist.');} 
 39    
 40    if(cntID != null){ 
 41    OpportunityContactRole ocr = new OpportunityContactRole(ContactId=cntID, OpportunityID = o.Id, 
 42    Role = 'Primary Insured', IsPrimary = true); 
 43    upsert new List<OpportunityContactRole>{ocr}; 
 44    } 
 45    } 
 46    } 
 47   } 

 test class (please don't laugh:smileywink:):

@isTest
private class TestOpportunityContactRole {

    static testMethod void TestmyOpportunityContactRoleTrigger() {
        
// TO DO: implement unit test
        Test.startTest();
        
		Contact cntExisting = new Contact(
        FirstName = 'Test', LastName = 'Tester', RecordTypeId = '01240000000DVfaAAG', Email='test@hanover.com', 
        cnt_plcsc_CustNmbr_ExtID__c = '123456789INS01', cnt_plcsc_Customer_Number__c='123456789');
    	insert cntExisting;
    	
    	Contact cntExisting2 = new Contact(
        FirstName = 'Test', LastName = 'Tester2', RecordTypeId = '01240000000DVfaAAG', Email='test2@hanover.com', 
        cnt_plcsc_CustNmbr_ExtID__c = '2123456789INS01', cnt_plcsc_Customer_Number__c='2123456789');
    	insert cntExisting;
    	
    	Opportunity myOppPolicyTest = new Opportunity(StageName='Quoted', CloseDate=Date.today(), Name='Tester1', 
    	Policy_Id__c='ABC1234567', Pol_Sym__c='ABC', Pol_No__c='1234567',LOB__c='Auto', 
    	opp_plcsc_Integration_ID__c='A1234567', Customer_Number__c = '123456789');
		insert myOppPolicyTest;
    	                
        OpportunityContactRole ocr = new OpportunityContactRole(OpportunityId = myOppPolicyTest.Id,
		ContactId = cntExisting.Id, Role = 'Primary Insured', IsPrimary = true);
		upsert new List<OpportunityContactRole> {ocr};
		
		System.assert(myOppPolicyTest.Customer_Number__c == '2123456789');
        Test.stopTest();
    }
}

 

 

Hi all,

I need to create a trigger to relate a lead after update or insert to an existing contact record.  Both will have an identifier to make the relationship.  Just not sure how to write the trigger.  My code is attached.  I have started the trigger but I am now stuck.

 

Any help would be great.

Thanks Dwayne

trigger LeadCnsmrContactAssoc on Lead (after insert, after update) {
    List<Contact> cntList = new List<Contact>();  
        for (Lead l: Trigger.new)  {    
            if (l.ld_plcsc_Customer_Number__c != null)

                {      
                    try {        
                    Contact cnt = [select id, cnt_plcsc_CustNmbr_ExtID__c from Contact where cnt_plcsc_CustNmbr_ExtID__c =: l.ld_plcsc_Customer_Number__c limit 1];     
                    //cntList.Lead = l.Id;//
                    cntList.add(cnt);}
                    
                    catch (Exception e) {System.debug('Contact Record Does Not Exist.');}  
                   } 
                } 
    if (cntList.size() > 0)  
        update cntList;   
}

 

Hi,

I need to do the following in an email template.  

 

I have a field Account.Phone that I need to apply a format for phone number to in an email template.  

 

Example:  (800) 111-1111

 

What formula would I use?  Also, this will be inbedded in an if statement.

{!IF( ISBLANK(field), "Formatted Account.Phone", field)}

 

Any help would be great.

Thanks Dwayne

Hello,

I need some help with the following.  I have been able to successfully set up a page redirect but I am having issues with th url.  The url takes me to the correct page but it adds 25 in certain places which prevents me from moving to the next page.  Below you will see the proper url (1)  and the url that is being sent from my button(2)...I have also posted my code that creates the url.

 

1. correct URL

https://tapp0.salesforce.com/setup/ui/recordtypeselect.jsp?ent=01IT000000058Gq&retURL=0064000000EkBjTAAV&save_new_url=%2Fa0s%2Fe%3FCF00NT0000001GcgE%3DELESLIE%2BE%2BSCHMIDTKE%2B-%2BAuto%26CF00NT0000001GcgE_lkid%3D0064000000EkBjTAAV%26retURL%3D%252F0064000000EkBjTAAV

 

2.  being passed by button

https://tapp0.salesforce.com/setup/ui/recordtypeselect.jsp?ent=01IT000000058Gq&retURL=0064000000EkBjTAAV&save_new_url=%252Fa0s%252Fe%253FCF00NT0000001GcgE%253DELESLIE%2BE%2BSCHMIDTKE%2B-%2BAuto%2526CF00NT0000001GcgE_lkid%253D0064000000EkBjTAAV%2526retURL%253D%252F0064000000EkBjTAAV

 

 

3.Apex Class coding

    public PageReference ServiceNotifications()
    {
    String strUrl= 'https://tapp0.salesforce.com/setup/ui/recordtypeselect.jsp?ent=01IT000000058Gq&retURL=' + policy.Id + '&save_new_url=%2Fa0s%2Fe%3FCF00NT0000001GcgE%3DE' + EncodingUtil.urlEncode(policy.Name, 'UTF-8') + '%26CF00NT0000001GcgE_lkid%3D' + policy.Id + '%26retURL%3D%2F' + policy.Id;
    PageReference pageRef = new PageReference(strURL);
    pageRef.setRedirect(true);
    return pageRef;
    }

 

ANY help would be greatly appreciated.  I feel I am so very close but just missing something.

 

FYI  Seems the 25's are being added starting after the 'save_new_url' section.

 

Thank You Dwayne

 

First I need to state that for some reason this page for enetering a new message does not work properly for me. It is center aligned and no graphics. Which meens that I can not post my code but I can email it. OK so I have a VF Page that has InputFields and I have created a button that I need to have update the opportunity with the new values that have been entered. On click of the save button I am going to the apex class to run the save action. In this public call I list the opportunity and in a FOR I run a select query to find the correct record. Just need to reference the inputfield value from the VF page to update the opportunity. Any help would be greatly appreciated. Thanks Dwayne
Hello, I need some help with the follwing code: I am getting an error when I add the functionality to the second output panel that is shown. Any help would be greatly appreciated. Thanks Dwayne

Hello,

Been working on this too long and need some help.  I have a Master Detail relationship between Opportunity and a custom object.  I have added the Related list as I have added others to the VF Page but it is giving me the above error.  The code is very simple but I must be missing something.

Any help would be great.

Thanks Dwayne

 

<apex:outputPanel rendered="{!hasPolicy}" id="UpsellCampaignOppt">
	    <table style="width: 1100px;">
	    	<tr>
	    	<td colspan="1">
				<apex:pageBlock tabStyle="Campaign">
	    			<apex:pageBlockSection id="UpsellCO" title="Upsell Campaign Opportunities" collapsible="true" columns="1">
	    				<apex:relatedList list="Upsell_Campaign_Opportunities__r" subject="{!policy.Id}"/>
	   				</apex:pageBlockSection>
	   				<script>twistSection(document.getElementById("{!$Component.UpsellCO}").childNodes[0].childNodes[0]); </script>
	 			</apex:pageBlock>
			</td>
	 		</tr>
		</table>
	</apex:outputPanel>

 

Hi Everyone,

 

I am trying to build a VF Page that will sit in a standard Contact Page Layout.  I know that Account Contact Role relates to Account and there is a related list on Accounts.  What we are looking for is a way to show these Account Contact Roles on the Contact.

 

My best assumption is to create a VF Page that can be added to the standard page layout with a apex class related to the Account Contact Role where the ContactId from the AccountContactRole object is equal to the current Contact Id.

 

The following code is my VF page and Apex Class.  I feel there may already be a solution for this so I am totally open to new suggestions or fixes to my current code and logic.


Thank You Very Much.

 

<apex:page standardcontroller="Contact" recordSetVar="contacts" extensions="AccountContactRoles">
 <apex:pageBlock title="Account Contact Roles">
        <apex:form id="theForm1">
            <apex:pageBlockSection >
                <apex:dataList value="{!AccountContactRoles}" var="acr" type="1">
                    {!acr.Id}
                </apex:dataList>
            </apex:pageBlockSection>
            <apex:panelGrid columns="2">
                <apex:commandLink action="{!previous}">Previous</apex:commandlink>
                <apex:commandLink action="{!next}">Next</apex:commandlink>
            </apex:panelGrid>
        </apex:form> 
    </apex:pageBlock>
</apex:page>

 This is my VF Page Coding

 

public with sharing class AccountContactRoles {
	private final Contact acr;
	public Id cntId{get;set;}
	
	public AccountContactRoles(ApexPages.StandardSetController controller){
		this.acr = (Contact)controller.getRecord();
	}
	public ApexPages.Standardsetcontroller acrRecords{
		get{
			if(acrRecords == null){
				cntId = ApexPages.currentPage().getParameters().get('Id');
				acrRecords = new ApexPages.Standardsetcontroller(
				Database.getQueryLocator([Select Id, AccountId, ContactId, Role, IsPrimary From AccountContactRole 
				Where ContactId=:cntId]));
			}
			return acrRecords;
		}
		private set;
	}
	public List<AccountContactRole> getAccountContactRoles(){
		return (List<AccountContactRole>) acrRecords.getRecords();
	}
}

 This is my Apex Class.

Hi Everyone. 

I have a trigger on the Contract object that runs After Update.  My question is the trigger is only good for updating particular records but will fire after every record is updated.  To minimize the code that is running the first section of the code runs a set<String> to determine the Id's of the Contract records that will be effected.  After the Set is returned I have an if statement of:

 

if(!setC.isEmpty()) { do my stuff }

 

is this good practice so that the code section of "do my stuff" does not always run if the records being updated, based on the criteria of a "if" statement in my set<>, does not match?


Thanks :)

Hello All,

I have been able to write a trigger with "bulkification" but I am having problems understanding how to write the test class to cover all lines.

 

Here is the code that I am trying to cover.

 

trigger ContactOpportunityAssoc on Contact (after insert) {
set<String> custnmbr = new set<String>();
		for (Contact c: Trigger.new){
            if (c.cnt_plcsc_CustNmbr_ExtID__c != null){custnmbr.add(c.cnt_plcsc_Customer_Number__c);}
        }
        
Map<String,Id> mapOpps = new Map<String,Id>();
/*List all Contacts that have the customer number the same as the loaded custnmbr set*/      
		for(Opportunity opp: [Select Id, Customer_Number__c 
							From Opportunity 
							Where Customer_Number__c IN: custnmbr]){mapOpps.put(opp.Customer_Number__c , opp.Id);}

set<String> oppids = new set<String>();
		for(Opportunity opp2: [Select Id, Customer_Number__c 
								From Opportunity 
								Where Customer_Number__c IN: custnmbr]){oppids.add(opp2.Id);}

Map<String,Id> mapCnts = new Map<String,Id>();
/*List all Contacts that have the customer number the same as the loaded custnmbr set*/      
		for(Contact cnt: Trigger.new){mapCnts.put(cnt.cnt_plcsc_Customer_Number__c , cnt.Id);}
		
/*Loop through the main list of Cnts*/
List<OpportunityContactRole> ocr = new List<OpportunityContactRole>();
List<Opportunity> oppsList = [Select Id, Customer_Number__c From Opportunity Where Customer_Number__c In: custnmbr];

			for(Opportunity o2: oppsList){
				if(mapOpps.containsKey(o2.Customer_Number__c)){
						OpportunityContactRole ocr2 = new OpportunityContactRole();
						ocr2.ContactId = mapCnts.get(o2.Customer_Number__c);
						ocr2.OpportunityId = o2.Id;
						ocr2.IsPrimary = true;
						ocr2.Role = 'Primary Insured';
						ocr.add(ocr2);
				}
			}
/*Insert the new list of OCR's*/
	if(!ocr.isEmpty()){
		insert ocr;}
}

 Here is my sad attempt at code coverage:

Contact cntExisting = new Contact(LastName = 'Tester');
			cntExisting.FirstName = 'Test';
			cntExisting.RecordTypeId = '01240000000DVfaAAG';
			cntExisting.Email='test@hanover.com';
			cntExisting.cnt_plcsc_CustNmbr_ExtID__c = '123456789INS01';
			cntExisting.cnt_plcsc_Customer_Number__c='123456789';
			cntExisting.MailingPostalCode = '01453';
			String cntId = cntExisting.Id;
		insert cntExisting;
		
		cntExisting = [Select Id, Name, cnt_plcsc_Customer_Number__c from Contact Where Id=:cntExisting.Id];
    	System.assertEquals('123456789', cntExisting.cnt_plcsc_Customer_Number__c);
    	
    	Map<String,Id> mapCntpRecs = new Map<String,Id>();
    		for(Contact mapcnts:[Select Id, cnt_plcsc_Customer_Number__c from Contact Where cnt_plcsc_Customer_Number__c=:cntExisting.cnt_plcsc_Customer_Number__c]){
    			mapCntpRecs.put(mapcnts.cnt_plcsc_Customer_Number__c, mapcnts.Id);}
    		
    	Contact cntExisting2 = new Contact(LastName = '2Tester');
			cntExisting2.FirstName = 'Test';
			cntExisting2.RecordTypeId = '01240000000DVfaAAG';
			cntExisting2.Email='test2@hanover.com';
			cntExisting2.cnt_plcsc_CustNmbr_ExtID__c = '223456789INS01';
			cntExisting2.cnt_plcsc_Customer_Number__c='223456789';
			cntExisting2.MailingPostalCode = '01453';
			String cntId2 = cntExisting2.Id;
    	insert cntExisting2;
    	
    	Opportunity otherPol1 = new Opportunity(Name = '1Other Pol Name');
				otherPol1.customer_number__c = '1234abcd';
				otherPol1.Policy_Id__c = '1XX111111';
				otherPol1.LOB__c = 'Auto';
				otherPol1.stageName = 'Qualified';
				otherPol1.CloseDate = Date.today();
				otherPol1.AccountId = otherAct.Id;
				otherPol1.X1st_Nmd_Insd__c = 'Joe Test1';
				otherPol1.Zip__c = '01453';
				otherPol1.Consumer_Email__c = '1test@hanover.com';
				otherPol1.opp_plcsc_Integration_ID__c = 'A1XX111111';
				insert otherPol1;
    	System.assertEquals('A1XX111111', otherPol1.opp_plcsc_Integration_ID__c);
    	
    	Opportunity otherPol = new Opportunity(Name = 'Other Pol Name');
				otherPol.customer_number__c = '1234abcd';
				otherPol.Policy_Id__c = 'XXX111111';
				otherPol.LOB__c = 'Other';
				otherPol.stageName = 'Qualified';
				otherPol.CloseDate = Date.today();
				otherPol.AccountId = otherAct.Id;
				otherPol.X1st_Nmd_Insd__c = 'Joe Test';
				otherPol.Zip__c = '01453';
				otherPol.Consumer_Email__c = 'test@hanover.com';
		insert otherPol;
						
		Map<String,Id> mapOpps = new Map<String,Id>();
    	for(Opportunity opp: [Select Id, Customer_Number__c From Opportunity Where Customer_Number__c =: otherPol1.Customer_Number__c]) 
    	{mapOpps.put(opp.Customer_Number__c,opp.Id);}
    	
    	set<String> oppids = new set<String>(); 
		for(Opportunity opp2: [Select Id, Customer_Number__c From Opportunity 
			Where Customer_Number__c =: otherPol.Id]){oppids.add(opp2.Id);} 
			
    	OpportunityContactRole ocrInsert = new OpportunityContactRole(OpportunityId = otherPol1.Id, ContactId = cntExisting2.Id);
    			ocrInsert.IsPrimary = true;
    			ocrInsert.Role = 'Primary Insured';
    			insert ocrInsert;
    	
    	Map<String,Id> mapOppRecs = new Map<String,Id>();
    		for(OpportunityContactRole mapoppscntr:[Select Id, ContactId, OpportunityId from OpportunityContactRole Where OpportunityId=:otherPol1.Id]){
    			mapOppRecs.put(mapoppscntr.OpportunityId, mapoppscntr.Id);} 
    						     
		List<OpportunityContactRole> ocrs=new List<OpportunityContactRole>();
		List<Opportunity> oppsList = [Select Id, Customer_Number__c From Opportunity Where Id In: oppids];
		for(Opportunity o2:oppsList){
			if(mapOppRecs.containsKey(o2.Customer_Number__c)){
				OpportunityContactRole ocr2 = new OpportunityContactRole(OpportunityId = otherPol.Id, ContactId = cntExisting2.Id);
		        		ocr2.Role = 'Primary Insured';
		        		ocr2.IsPrimary = false;
		        		ocrs.add(ocr2);
		}}
		insert ocrs;

 Any help would be greatly appreciated.  :)

 

Hi all,

I need some help with covering a trigger.  My test calss gets me 58% of the way there but I would relly like to get closer to 100%.

My trigger is not big and should be easy help.  Just need pointed in the right direction.

 

Trigger that I am trying to cover:

OpportunityContactAssoc (Code Covered: 58%)
 line  source 
 1   trigger OpportunityContactAssoc on Opportunity (before insert, before update) { 
 2   List<Contact> cntList = new List<Contact>(); 
 3   List<OpportunityContactRole> oppCntRole = new List<OpportunityContactRole>(); 
 4   public String cntID {get;set;} 
 5    
 6    for (Opportunity o: Trigger.new) { 
 7    if (o.Customer_Number__c != null) 
 8    { 
 9    try {Contact cnts = [select Id, cnt_plcsc_CustNmbr_ExtID__c, cnt_plcsc_Customer_Number__c 
 10    from Contact 
 11    where cnt_plcsc_Customer_Number__c =: o.Customer_Number__c 
 12    And cnt_plcsc_CustNmbr_ExtID__c =: o.Customer_Number__c + 'Ins01' limit 1]; 
 13    cntID = cnts.Id; 
 14    cntList.add(cnts);} catch (Exception e) {System.debug('Contact Record Does Not Exist.' + cntID);} 
 15    
 16    if(cntID != null) 
 17    { 
 18    OpportunityContactRole ocr = new OpportunityContactRole(ContactId=cntID, OpportunityID = o.Id, 
 19    Role = 'Primary Insured', IsPrimary = true); 
 20    upsert new List<OpportunityContactRole>{ocr}; 
 21    
 22    for(OpportunityContactRole oppcnts : [select Id, ContactId, OpportunityId, Role, IsPrimary 
 23    from OpportunityContactRole 
 24    where IsPrimary =: false And Role =: 'Primary Insured' And OpportunityId =: o.Id]) 
 25    { 
 26    delete oppcnts; 
 27    } 
 28    
 29    } 
 30    } 
 31    else { 
 32    try { Contact cnts = [select Id, MailingStreet, MailingState, cnt_plcsc_Customer_Number__c, 
 33    MailingPostalCode, MailingCity, LastName, FirstName, Email 
 34    from Contact 
 35    where Email =: o.Consumer_Email__c And MailingPostalCode =: o.Zip__c And RecordTypeId =: '01240000000DVfa' limit 1]; 
 36    cntID = cnts.Id; 
 37    o.Customer_Number__c = cnts.cnt_plcsc_Customer_Number__c; 
 38    cntList.add(cnts);} catch (Exception e) {System.debug('Contact Record Does Not Exist.');} 
 39    
 40    if(cntID != null){ 
 41    OpportunityContactRole ocr = new OpportunityContactRole(ContactId=cntID, OpportunityID = o.Id, 
 42    Role = 'Primary Insured', IsPrimary = true); 
 43    upsert new List<OpportunityContactRole>{ocr}; 
 44    } 
 45    } 
 46    } 
 47   } 

 test class (please don't laugh:smileywink:):

@isTest
private class TestOpportunityContactRole {

    static testMethod void TestmyOpportunityContactRoleTrigger() {
        
// TO DO: implement unit test
        Test.startTest();
        
		Contact cntExisting = new Contact(
        FirstName = 'Test', LastName = 'Tester', RecordTypeId = '01240000000DVfaAAG', Email='test@hanover.com', 
        cnt_plcsc_CustNmbr_ExtID__c = '123456789INS01', cnt_plcsc_Customer_Number__c='123456789');
    	insert cntExisting;
    	
    	Contact cntExisting2 = new Contact(
        FirstName = 'Test', LastName = 'Tester2', RecordTypeId = '01240000000DVfaAAG', Email='test2@hanover.com', 
        cnt_plcsc_CustNmbr_ExtID__c = '2123456789INS01', cnt_plcsc_Customer_Number__c='2123456789');
    	insert cntExisting;
    	
    	Opportunity myOppPolicyTest = new Opportunity(StageName='Quoted', CloseDate=Date.today(), Name='Tester1', 
    	Policy_Id__c='ABC1234567', Pol_Sym__c='ABC', Pol_No__c='1234567',LOB__c='Auto', 
    	opp_plcsc_Integration_ID__c='A1234567', Customer_Number__c = '123456789');
		insert myOppPolicyTest;
    	                
        OpportunityContactRole ocr = new OpportunityContactRole(OpportunityId = myOppPolicyTest.Id,
		ContactId = cntExisting.Id, Role = 'Primary Insured', IsPrimary = true);
		upsert new List<OpportunityContactRole> {ocr};
		
		System.assert(myOppPolicyTest.Customer_Number__c == '2123456789');
        Test.stopTest();
    }
}

 

 

Hi all,

I need to create a trigger to relate a lead after update or insert to an existing contact record.  Both will have an identifier to make the relationship.  Just not sure how to write the trigger.  My code is attached.  I have started the trigger but I am now stuck.

 

Any help would be great.

Thanks Dwayne

trigger LeadCnsmrContactAssoc on Lead (after insert, after update) {
    List<Contact> cntList = new List<Contact>();  
        for (Lead l: Trigger.new)  {    
            if (l.ld_plcsc_Customer_Number__c != null)

                {      
                    try {        
                    Contact cnt = [select id, cnt_plcsc_CustNmbr_ExtID__c from Contact where cnt_plcsc_CustNmbr_ExtID__c =: l.ld_plcsc_Customer_Number__c limit 1];     
                    //cntList.Lead = l.Id;//
                    cntList.add(cnt);}
                    
                    catch (Exception e) {System.debug('Contact Record Does Not Exist.');}  
                   } 
                } 
    if (cntList.size() > 0)  
        update cntList;   
}

 

I am leveraging the cookbook solution to prevent duplicate leads in order to stop duplicate contacts from being created. That is working just fine at this point, but here's the twist.  Contacts are private, so even if they search, they may not find the contact in question.  When they try to create a contact that is a duplicate, we want to automatically share the original contact with them using sharing rules and present a link to the newly shared contact.

I am leveraging the cookbook solution to prevent duplicate leads in order to stop duplicate contacts from being created. That is working just fine at this point, but here's the twist.  Contacts are private, so even if they search, they may not find the contact in question.  When they try to create a contact that is a duplicate, we want to automatically share the original contact with them using sharing rules and present a link to the newly shared contact.

 

 

Independantly the two actions work.  I can insert a sharing rule, I can stop the duplicte from being created, but when I try to do both together, it appears that the addError on the contact rolls back the insert of the sharing record.

 

 

trigger ContactDuplicateTrigger on Contact (before insert, before update) {
	Map<String, Contact> contactMap = new Map<String, Contact>();
    
    for (Contact c : trigger.new) {
         
        // Make sure we don't treat an email address that     
        // isn't changing during an update as a duplicate. 
     
        if ((c.Email != null) && (trigger.isInsert || (c.Email != trigger.oldMap.get(c.Id).Email))) {
         
            // Make sure another new lead isn't also a duplicate      
            if (contactMap.containsKey(c.Email)) {
                c.Email.addError('Another new contact has the same email address.');
            } else {	
                contactMap.put(c.Email, c);
            }
       }
    }
     
    // Using a single database query, find all the leads in 
    // the database that have the same email address as any 
    // of the leads being inserted or updated. 
     
    for (Contact c : [SELECT Email, Owner.Name FROM contact WHERE Email IN :contactMap.KeySet()]) {
        Contact newContact = contactMap.get(c.Email);       	
        ContactShare cShare = new ContactShare();
        cShare.ContactId=c.id;
        cShare.UserOrGroupId = UserInfo.getUserId(); 
        cShare.ContactAccessLevel = 'Edit';		
		
		newContact.addError('A contact with this email address already exists and is owned by ' + c.Owner.Name + 
                            '.  You have been granted access to this record and ' + c.Owner.Name + ' has been notfied.' +
                            'The Link for the existing contact is: <a href="/' + c.id + '"> Link </a>' ); 
		Database.insert(cShare,false);      		          	      	        
    }
}
  • December 03, 2010
  • Like
  • 0
Hi, I have a need for a button from a VF page to rediert me to the landing page for record type selection for creating a new record on a custom object. Custom object has a master detail relationship with Opportunity. I have created the button but need help with the APEX coding in the class for the VF page. Any help would be greatful. Thanks Dwayne