+ Start a Discussion
Claire RookClaire Rook 

Trigger and Intergration interaction

I have a problem with an integration job and a trigger that seem to be colliding and I was wondering if anyone could help me. There is an integration job running in PowerCenter that takes opportunities and creates contacts from the information on the opportunity. There is also a trigger that runs to update the contact roles when these contacts are created. The trigger is causing problems with the power center job and the power center log shows this error:

2015-06-16 02:58:33 : ERROR : (28559 | WRITER_1_*_1) : (IS | Int_Serv_pcpw_Unicode) : node01_pcpw : WRT_8164 : Error loading into target [Contact] : Error received from salesforce.com. Fields []. Status code [CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY]. Message [ContactOpportunityAssoc: execution of AfterInsert caused by: System.DmlException: Insert failed. First exception on row 200; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [ContactId]: [ContactId]

This is causing some contacts not to get created.

The trigger is called ContactOpportunityAssoc and here's the code:
trigger ContactOpportunityAssoc on Contact (after insert) {
set<String> custnmbr = new set<String>();
Map<String,Id> mapCnts = new Map<String,Id>();
for (Contact c: Trigger.new){
if (c.cnt_plcsc_CustNmbr_ExtID__c != null)
/*List all Contacts that have the customer number the same as the loaded custnmbr set*/ 
mapCnts.put(c.cnt_plcsc_Customer_Number__c , c.Id);

List<OpportunityContactRole> ocr = new List<OpportunityContactRole>();
/*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]){

OpportunityContactRole ocr2 = new OpportunityContactRole();
ocr2.ContactId = mapCnts.get(opp.Customer_Number__c);
ocr2.OpportunityId = opp.Id;
ocr2.IsPrimary = true;
ocr2.Role = 'Primary Insured';
/*System.debug('DLB932 - '+ocr2);*/

/*Insert the new list of OCR's*/
{insert ocr;}

Thank you!
MJ Kahn / OpFocusMJ Kahn / OpFocus
Are you sure that the values in c.cnt_plcsc_Customer_Number__c are exactly the same as the values in opp.Customer_Number__c, including case? If there's a difference in case, then it's possible that your Opportunity query will return a record with a Customer_Number__c that's not in your map, which would cause mapCnts.get(opp.Customer_Number__c) to return null, which would cause your ocr2.ContactId to be set to null, which would cause the error you describe.