You need to sign in to do that
Don't have an account?
Force.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;
}
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;
}
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
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.