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
Force.platformForce.platform 

trigger on opportunity to create child record

Hello All, I have trigger to create Revenueshare(child of opportunity) record if opportunity  stage=closewon and owner of opportunity and account is same, but trigger is not wrking.
trigger createRevenueShare on Opportunity (after insert, after update) {
   List<Revenue_Share__c> share= new List<Revenue_Share__c>();
   
   for(Opportunity opp:trigger.new)
   {
   if( opp.StageName== 'Close Won' && (opp.OwnerId == opp.Account.OwnerId))
   {
   Revenue_Share__c revShare= new Revenue_Share__c();
   revShare.Opportunity__c=opp.Id;
   share.add(revShare);
   }
   }
   insert share;
}
Best Answer chosen by Force.platform
Pradeep SinghPradeep Singh
Hi,
You won't get Account's OwnerID in trigger.new. You have use SOQL query to get that.

trigger createRevenueShare on Opportunity (after insert, after update) {
   List<Revenue_Share__c> share= new List<Revenue_Share__c>();
   
   for(Opportunity opp:[select id,name,stageName,OwnerId,Account.OwnerId from Opportunity where id in: trigger.new])
   {
   if( opp.StageName== 'Close Won' && (opp.OwnerId == opp.Account.OwnerId))
   {
   Revenue_Share__c revShare= new Revenue_Share__c();
   revShare.Opportunity__c=opp.Id;
   share.add(revShare);
   }
   }
   insert share;
}
Check if this works for you.

All Answers

Pradeep SinghPradeep Singh
Hi, Stage name for Opportunity is Closed Won not close won. Check if this works..!
Force.platformForce.platform
HI Pradeep,   i corrected it, still not working....
Pradeep SinghPradeep Singh
Hi,
You won't get Account's OwnerID in trigger.new. You have use SOQL query to get that.

trigger createRevenueShare on Opportunity (after insert, after update) {
   List<Revenue_Share__c> share= new List<Revenue_Share__c>();
   
   for(Opportunity opp:[select id,name,stageName,OwnerId,Account.OwnerId from Opportunity where id in: trigger.new])
   {
   if( opp.StageName== 'Close Won' && (opp.OwnerId == opp.Account.OwnerId))
   {
   Revenue_Share__c revShare= new Revenue_Share__c();
   revShare.Opportunity__c=opp.Id;
   share.add(revShare);
   }
   }
   insert share;
}
Check if this works for you.
This was selected as the best answer
Gustavo BertolinoGustavo Bertolino
You're trying to set Revenue ID as equal to Opportunity ID, but ID is read-only data. Maybe that's the reason for not still working.