+ Start a Discussion
Eric_GoehringEric_Goehring 

Opportunity Trigger with Dynamic Query

I could use some help here. You'll notice in the code below that I have hardcoded int the Contact rt query the Id. I want the query to be run with a dynamic Id. I have tried everything especially =:ContactId or even the long version =:o.ContactId__c. The update that sets o.Registered_Member__c = ContactId is working so I know the variable is being populated. I am new to Eclipse and it won't let me do the above variable queries.

 

The Trigger looks like this:

 

trigger UpdateMember on Opportunity (before insert) { for(Opportunity o:Trigger.new)

{

String ContactId=o.ContactId__c;

String Member=o.Registered_Member__c;

String RegisteredId=o.Registered_UserId__c;

if(ContactId!='' && o.Registered_Member__c==null)

{

o.Registered_Member__c = ContactId;

Contact rt = [select Id, FirstName from Contact where Id='0038000000lqpyL' limit 1];

String FirstName=rt.FirstName;

o.Registered_Member_First__c = FirstName;

}

if(RegisteredId!='' && o.Registering_Contact__c==null)

{

o.Registering_Contact__c = RegisteredId;

}

}

}

 

Thanks.

Best Answer chosen by Admin (Salesforce Developers) 
TehNrdTehNrd

There is nothing wrong with Eclipse. Your code is not following some best practices. You never want a SOQL query in a for loop. You want to populate your data in a map and then reference it there. I've re written your trigger.

 

 

trigger UpdateMember on Opportunity (before insert) { Set<Id> contactIds = new Set<Id>(); Map<Id,Contact> contactMap = new Map<Id,Contact>(); for(Opportunity o:Trigger.new){ contactIDs.add(o.ContactId); } for(Contact c : [select Id, FirstName from Contact where Id IN :contactIDs]){ contactMap.put(c.Id,c); } for(Opportunity o:Trigger.new){ String Member = o.Registered_Member__c; String RegisteredId = o.Registered_UserId__c; if(o.ContactId__c != null && o.Registered_Member__c == null){ o.Registered_Member__c = contactMap.get(o.Contact_Id__c).Id; o.Registered_Member_First__c = contactMap.get(o.Contact_Id__c).FirstName; } if(RegisteredId != '' && o.Registering_Contact__c == null){ o.Registering_Contact__c = RegisteredId; } } }

 

 

 

All Answers

JmBessonartJmBessonart

Hi, what is the error that you are getting?? an eclipse compilation errror? or when you try to create a new opportunity?

 

Regards,

J.

Eric_GoehringEric_Goehring

Could this be an Eclipse issue? I am just starting learn how to use it, but this seems strange. To answer your question, it is not on the opportunity, Eclipse never allows me to save the trigger. The error from the log is below:

 

Run Failures:
  UpdateOpp.myUpdateOpp System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, UpdateMember: execution of BeforeInsert

caused by: System.QueryException: List has no rows for assignment to SObject

Trigger.UpdateMember: line 11, column 15: []
  Average test coverage across all Apex Classes and Triggers is 71%, at least 75% test coverage is required

 

 

And I am using:

Eclipse Platform

 

Version: 3.4.2

Build id: M20090211-1700

TehNrdTehNrd

There is nothing wrong with Eclipse. Your code is not following some best practices. You never want a SOQL query in a for loop. You want to populate your data in a map and then reference it there. I've re written your trigger.

 

 

trigger UpdateMember on Opportunity (before insert) { Set<Id> contactIds = new Set<Id>(); Map<Id,Contact> contactMap = new Map<Id,Contact>(); for(Opportunity o:Trigger.new){ contactIDs.add(o.ContactId); } for(Contact c : [select Id, FirstName from Contact where Id IN :contactIDs]){ contactMap.put(c.Id,c); } for(Opportunity o:Trigger.new){ String Member = o.Registered_Member__c; String RegisteredId = o.Registered_UserId__c; if(o.ContactId__c != null && o.Registered_Member__c == null){ o.Registered_Member__c = contactMap.get(o.Contact_Id__c).Id; o.Registered_Member_First__c = contactMap.get(o.Contact_Id__c).FirstName; } if(RegisteredId != '' && o.Registering_Contact__c == null){ o.Registering_Contact__c = RegisteredId; } } }

 

 

 

This was selected as the best answer