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
REKREK 

Trigger on Lookup

I have one lookup for User  in custom object as well as in Opportunity object.

 

Want to copy that user from opportunity to custom object through Trigger.

 

can anyone help in this.

 

Thanks in advance.

 

Best Answer chosen by Admin (Salesforce Developers) 
Ritesh AswaneyRitesh Aswaney

trigger commopstest on Opportunity (After Insert, After Update)
{
 //Added StageName, Name and CloseDate to the query. YOU NEED TO LOOK AT WHAT IS ON LINE 9 OF Trigger.commopS - WHICH SEEMS LIKE ANOTHER TRIGGER YOU //HAVE. PERHAPS YOU NEED TO ASSIGN THESE FIELDS TO CORRESPONDING FIELDS ON THE POSITION

 

Opportunity[] opps = [Select Id, Comops_leader__c, Name, StageName, CloseDate, (Select Id, comops__c from Positions__r ) from Opportunity where Id in :trigger.New];
List<Position__c> ToUpdate = new List<Position__c>{};
for (Opportunity opp : opps)
{
     for(Position__c post : opp.Positions__r)
           if(post.comops__c != opp.Comops_leader__c)
          {
             post.comops__c = opp.Comops_leader__c;
               ToUpdate.add(post);
           }
}
 
if(ToUpdate != null && !ToUpdate.isEmpty())
Database.update(ToUpdate);
}

All Answers

Ritesh AswaneyRitesh Aswaney

How are Opportunity and Custom Object related ?

REKREK

Opportunity contains that custom object relatedlist.

Ritesh AswaneyRitesh Aswaney

trigger OpportunityAfter on Opportunity (After Insert, After Update)

{

 

Opportunity[] opps = [Select Id, User__c, (Select Id, User__c from Custom__r) from Opportunity where Id in :trigger.New];

List<Custom__c> customObjectsToUpdate = new List<Custom__c>{};

for (Opportunity opp : opps)

{

     for(Custom__c cust : opp.Custom__r)

           if(cust.User__c != opp.User__c)

          {

             cust.User__c = opp.User__c; 

             customObjectsToUpdate.add(cust);

           }

 

if(customObjectsToUpdate != null && !customObjectsToUpdate.isEmpty())

Database.update(customObjectsToUpdate);

}

REKREK

This is my Trigger:

trigger commopstest on Opportunity (After Insert, After Update)
{
 
Opportunity[] opps = [Select Id, Comops_leader__c from Opportunity where Id in :trigger.New];
List<Position__c> ToUpdate = new List<Position__c>{};
for (Opportunity opp : opps)
{
     for(Position__c post : opp.Positions__r)
           if(post.comops__c != opp.Comops_leader__c)
          {
             post.comops__c = opp.Comops_leader__c;
               ToUpdate.add(post);
           }
}
 
if(ToUpdate != null && !ToUpdate.isEmpty())
Database.update(ToUpdate);
}

 

 

In Opportunity,when i am entering data without comops leader(nothing but user),its not showing any error.

But if i am enterting user and saving...its showing ...Error:Apex trigger commopstest caused an unexpected exception, contact your administrator: commopstest: execution of AfterUpdate caused by: System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: Opportunity.Positions__r: Trigger.commopstest: line 8, column 29

 

 

In Position Object,when i am entering data without comops leader(nothing but user),its not showing any error.But if i am enterting user and saving...its showing ...

Error:Apex trigger commops caused an unexpected exception, contact your administrator: commops: execution of AfterUpdate caused by: System.DmlException: Upsert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Opportunity Name, Stage, Close Date]: [Opportunity Name, Stage, Close Date]: Trigger.commops: line 9, column 1
Ritesh AswaneyRitesh Aswaney

Hey,

I've added to the query, you had ommitted the subquery needed as you interrogating a field on Positions__r. THis should get rid of the first error. 

 

trigger commopstest on Opportunity (After Insert, After Update)
{
 //YOU NEED TO SELECT COMOPS__C FROM POSITION__R IN THIS QUERY TO BE ABLE TO USE IT
Opportunity[] opps = [Select Id, Comops_leader__c, (Select Id, comops__c from Positions__r ) from Opportunity where Id in :trigger.New];
List<Position__c> ToUpdate = new List<Position__c>{};
for (Opportunity opp : opps)
{
     for(Position__c post : opp.Positions__r)
           if(post.comops__c != opp.Comops_leader__c)
          {
             post.comops__c = opp.Comops_leader__c;
               ToUpdate.add(post);
           }
}
 
if(ToUpdate != null && !ToUpdate.isEmpty())
Database.update(ToUpdate);
}

 

The 2nd error is owing to some different trigger you have on Positions presumably. CHeck that the related Opportunity has these fields populated, which are reported in the error

Opportunity Name, Stage, Close Date

REKREK

Hi...

 

after adding that subquery  i m getting this error:

Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger commopstest caused an unexpected exception, contact your administrator: commopstest: execution of AfterUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id a0090000000kMlsAAE; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, commops: execution of AfterUpdate caused by: System.DmlException: Upsert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Opportunity Name, Stage, Close Date]: [Opportunity Name, Stage, Close Date] Trigger.commops: line 9, column 1: []: Trigger.commopstest: line 17, column 1

Ritesh AswaneyRitesh Aswaney

trigger commopstest on Opportunity (After Insert, After Update)
{
 //Added StageName, Name and CloseDate to the query. YOU NEED TO LOOK AT WHAT IS ON LINE 9 OF Trigger.commopS - WHICH SEEMS LIKE ANOTHER TRIGGER YOU //HAVE. PERHAPS YOU NEED TO ASSIGN THESE FIELDS TO CORRESPONDING FIELDS ON THE POSITION

 

Opportunity[] opps = [Select Id, Comops_leader__c, Name, StageName, CloseDate, (Select Id, comops__c from Positions__r ) from Opportunity where Id in :trigger.New];
List<Position__c> ToUpdate = new List<Position__c>{};
for (Opportunity opp : opps)
{
     for(Position__c post : opp.Positions__r)
           if(post.comops__c != opp.Comops_leader__c)
          {
             post.comops__c = opp.Comops_leader__c;
               ToUpdate.add(post);
           }
}
 
if(ToUpdate != null && !ToUpdate.isEmpty())
Database.update(ToUpdate);
}

This was selected as the best answer
REKREK

Thanks a lot Ritesh....That error is because of another trigger...

 

 

 

REKREK

Thanks a lot Ritesh....That error is because of another trigger...




Ritesh AswaneyRitesh Aswaney

Cheers ! :D

REKREK

Hi

 

This is my Trigger ,From this code I need to copy opportunity comops leader to comops in position object,From this code I am able to see,when Opportunity comops  leader is edited r opportunity edited then only comops getting populated from opportunity,

 

When I am creating Position for the opportunity,what ever in the opportunity comops leader should get prepopulated on comops on Position.How can it be done?

 

trigger commopstest on Opportunity (After Insert,After Update,Before Insert,Before Update)
{
 
Opportunity[] opps = [Select Id, Comops_leader__c, (Select Id, comops__c from Positions__r ) from Opportunity where Id in :trigger.New];
List<Position__c> ToUpdate = new List<Position__c>{};
for (Opportunity opp : opps)
{
     for(Position__c post : opp.Positions__r)
           if(post.comops__c != opp.Comops_leader__c)
          {
             post.comops__c = opp.Comops_leader__c;
               ToUpdate.add(post);
           }
}
 
if(ToUpdate != null && !ToUpdate.isEmpty())
Database.update(ToUpdate);
}

Ritesh AswaneyRitesh Aswaney

Hey,

Isn't that what the trigger is doing anyway?

 

Also, it doesn't need to be before insert and before update - just after should suffice

 

trigger commopstest on Opportunity (After Insert,After Update)

REKREK

Hi Ritesh

 

I removed that before insert & Updae,

 

That commops on position is not directly prepopulating from opportunity commops leader.

 

 

Ritesh AswaneyRitesh Aswaney

I'm guessing the trigger should be on Position rather than Opportunity ?

 

Coz I'd written the trigger in mind for the update to pass from Opportunity to Position - so if you edit the associated Opportunity, the lookup will populate on the Position.

REKREK

Hi Ritesh

 

If i write that trigger on Position object,an error is cmng...Error: Compile Error: Didn't understand relationship 'Opportunity' in FROM part of query call. If you are attempting to use a custom relationship, be sure to append the '__r' after the custom relationship name.

 

Can you please give me the solution for this....

Ritesh AswaneyRitesh Aswaney

Hey Rekha,

Here you go... (Note how this is a before insert before update trigger now, coz ur modifying a field on Position)

 

 

trigger commopstest on Position__c (before Insert,before Update)
{
Id[] oppIds = new Id[]{};
for(Position__c pos : trigger.new)
oppIds.add(pos.Opportunity__c);     // check the api name of the opportunity lookup on Position__c
 
Map<Id,Opportunity> oppsMap = new Map<Id, Opportunity>([Select Id, Comops_leader__c from Opportunity where Id in :oppIds]);
 for(Position__c post : trigger.New)
      post.comops__c = oppsMap.get(pos.Opportunity__c).Comops_leader__c;
}

trigger commopstest on Position__c (before Insert,before Update){Id[] oppIds = new Id[]{};
for(Position__c pos : trigger.new)oppIds.add(pos.Opportunity__c);     // check the api name of the opportunity lookup on Position__c Map<Id,Opportunity> oppsMap = new Map<Id, Opportunity>([Select Id, Comops_leader__c from Opportunity where Id in :oppIds]);

 for(Position__c post : trigger.New)      post.comops__c = oppsMap.get(pos.Opportunity__c).Comops_leader__c;
}