function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
NetDNetD 

Help: Auto Populate a a custom look-up field on a custom object

Hi, I am not a coder so I would very much appreciate some help on how to code the follow simple trigger (i think):

 

I have a custom object that embedded in the Opportunity object with master-detail relationship.

 

On this custom object I have created a custom look-up field to relate with the User object.

 

What I need is a trigger to auto-populate this "User " look-up field with the "Opportunity Owner" whenever a record is created on this custom object.

 

Please help me! thank you!!

RbnRbn
Can you please explain in detail...
NetDNetD

Thanks for your response Rabi.

 

I have a custom object named "Stage Payments" that has master-detail relationship with Opportunity object, basically this custom object stores payment/invoice info inside each opportunity, so for exmaple if there is one invoice for this opportunity, there will be one "stage payment" record. For sales performance calculation reasons, I need to link "stage payment" record to "User" object, so the system can identify the amount of payment inside this stage payment is related to a particular "User" profile, and this user is the "Opportunity Owner" in the Opportunity object.

 

I have created a look-up relationship field inside the "stage payment" custom object called "Quota User", and related the field to "User" object.

 

I need a trigger to auto-populate the "Quota User" field with the "Opportunity Owner" of the opportunity that this "Stage payment" is related to - whenever a stage payment record is created.

 

Hope this explains a little bit better.....

Thanks!

Ajay.DixitAjay.Dixit

Hi,

 

We cannot auto populate through triggers how ever we can do it with the help of URL encoding.

 

see this article:

http://raydehler.com/cloud/clod/salesforce-url-hacking-to-prepopulate-fields-on-a-standard-page-layout.html

 

Over ride button and pass values of fields as parameters.

 

Thanks

Bhawani SharmaBhawani Sharma
You need to write a after insert trigger on your custom object, where what you will do is,
Query opportunity's owner and stamp it in custom lookup field.
NetDNetD

I have created the below trigger code for the custom object, to query the opportunity owner. 

 

Comes back with error: unexpected token: 'Map' line 8..

 

Could someone please help?

 

For reference the code below

 

trigger autopopulatequotauser on Milestone__c (before Insert, before Update) {

List<Id> oppIds = new List<Id>();

for(Milestone__c m :trigger.new){
oppIds.add(m.Opportunity__c);
}
Map<Id,Opportunity__r> = new Map <Id, Opportunity__r> ([select Owner, StageName, Ready_for_Quote_Generation__c from Opportuntiy__r where id in:oppIds]);
for(Milestone__c mi: trigger.new){
if(!oppMap.IsEmpty()){
String stage = oppMap.get(mi.Opportunity__r).StageName;
Boolean review = oppMap.get(mi.Opportunity__r).Ready_for_Quote_Generation__c;
if ((stage ='50% - Final Two' && review ='True')||(stage ='75% - Verbal' && review ='True')||(stage ='90% - Contracts/ Order Forms' && review ='True')|| (stage ='Closed Won' && review ='True'))
{
if(!oppMap.get(mi.Opportunity__r).Owner = mi.User__c)
{
mi.User__c = oppMap.get(mi.Opportunity__r).Owner;
}
}
}
}
}

Bhawani SharmaBhawani Sharma
You did not define instance for map:
Map<Id,Opportunity__c> oppMap = new Map <Id, Opportunity__c> ([select Owner, StageName, Ready_for_Quote_Generation__c from Opportuntiy__c where id in:oppIds]);
Bhawani SharmaBhawani Sharma
Ohh one more thing, this should be Opportunity only not Opportunity__r
NetDNetD

i have changed the coding accordingly, yet there is still the same error.

 

Code::

trigger autopopulatequotauser on Milestone__c (before Insert, before Update) {
List<Id> oppIds = new List<Id>();
for(Milestone__c m :trigger.new){
oppIds.add(m.Opportunity__c);
}
Map<Id,Opportunity__c> = new Map <Id, Opportunity__c> ([select Owner, StageName, Ready_for_Quote_Generation__c from Opportuntiy__c where id in:oppIds]);
for(Milestone__c mi: trigger.new){
if(!oppMap.IsEmpty()){
String stage = oppMap.get(mi.Opportunity__c).StageName;
Boolean review = oppMap.get(mi.Opportunity__c).Ready_for_Quote_Generation__c;
if ((stage ='50% - Final Two' && review ='True')||(stage ='75% - Verbal' && review ='True')||(stage ='90% - Contracts/ Order Forms' && review ='True')|| (stage ='Closed Won' && review ='True'))
{
if(!oppMap.get(mi.Opportunity__c).Owner = mi.User__c)
{
mi.User__c = oppMap.get(mi.Opportunity__c).Owner;
}
}
}
}
}

Bhawani SharmaBhawani Sharma
I do not see the changes in your code. Define a variable for map and use Opportunity only, not Opportunity__c