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
ShirinShirin 

Need help for triger on updating

H, I have written a code to insert records in a custom object from opportunitylineitemschedule. Can anybody help me write the update trigger for the same. My code is as seen below:

 

 

trigger RevenueForecastInsert on OpportunityLineItem (After Insert) {
 For (OpportunityLineItem OppLi : Trigger.New)
    {
list <OpportunityLineItemSchedule> schedule = [select Id, ScheduleDate,Revenue from 
                                               OpportunityLineItemSchedule where
                                               OpportunityLineItemId = :OppLi.Id ];
For( Integer i=0; i< (schedule.size()) ;i++)
{
 Forecast__c F = New Forecast__c (Amount__c = schedule[i].Revenue, 
                               Record_Id_BE__c = schedule[i].Id, 
                               Date__c = schedule[i].ScheduleDate,
                               Status__c = 'Pipeline',
                               Forecast_Owner__c = OppLi.Opportunity_Owner_Id__c,
                               Business_Unit_Head__c = OppLi.Business_Unit_Head_Id__c,
                               Business_Unit__c = OppLi.Resource_Business_Unit__c,
                               Opportunity_Type__c = 'T&M',
                               Related_Opportunity__c = OppLi.Opportunity_IdNumber__c);
Insert F;
}

}
}

 

Imran MohammedImran Mohammed

Add after update event to trigger and use the below code.

The code for insert as well as update is not bulkified and will not address if many records fire at a time.

 

 

 

trigger RevenueForecastInsert on OpportunityLineItem (After Insert, after update) {
if(trigger.isinsert)
{
 For (OpportunityLineItem OppLi : Trigger.New)
    {
list <OpportunityLineItemSchedule> schedule = [select Id, ScheduleDate,Revenue from 
                                               OpportunityLineItemSchedule where
                                               OpportunityLineItemId = :OppLi.Id ];
For( Integer i=0; i< (schedule.size()) ;i++)
{
 Forecast__c F = New Forecast__c (Amount__c = schedule[i].Revenue, 
                               Record_Id_BE__c = schedule[i].Id, 
                               Date__c = schedule[i].ScheduleDate,
                               Status__c = 'Pipeline',
                               Forecast_Owner__c = OppLi.Opportunity_Owner_Id__c,
                               Business_Unit_Head__c = OppLi.Business_Unit_Head_Id__c,
                               Business_Unit__c = OppLi.Resource_Business_Unit__c,
                               Opportunity_Type__c = 'T&M',
                               Related_Opportunity__c = OppLi.Opportunity_IdNumber__c);
Insert F;
}
}
}
if(trigger.isUpdate)
{
Forecast__c[] updateForecastList = new Forecast__c[]();
 For (OpportunityLineItem OppLi : Trigger.New)
    {
Map <ID,OpportunityLineItemSchedule> schedule = new Map<ID,OpportunityLineItemSchedule>([select Id, ScheduleDate,Revenue from OpportunityLineItemSchedule where OpportunityLineItemId = :OppLi.Id ]);
updateForecastList = [select Amount__c, other fields from Forecast__c where Record_Id_BE__c in :schedule.keyset()];
for(Forecast__c f: updateForecastList)
{
 //Make changes to field accordingly
}
update updateForecastList;
}
}
}

 

Chamil MadusankaChamil Madusanka

Hi,

 

Try this code.

 

 

trigger RevenueForecastInsert on OpportunityLineItem (after update) {
 For (OpportunityLineItem OppLi : Trigger.New)
    {
list <OpportunityLineItemSchedule> schedule = [select Id, ScheduleDate,Revenue from OpportunityLineItemSchedule where OpportunityLineItemId = :OppLi.Id ];

For( Integer i=0; i< (schedule.size()) ;i++){
 Forecast__c F = [Select Amount__c,Date__c,Status__c,Forecast_Owner__c,Business_Unit_Head__c,Business_Unit__c,Opportunity_Type__c,Related_Opportunity__c from Forecast__c where Record_Id_BE__c=:schedule[i].Id];
 
 		F.Amount__c = schedule[i].Revenue;
        F.Record_Id_BE__c = schedule[i].Id; 
        F.Date__c = schedule[i].ScheduleDate;
        //F.Status__c = 'Pipeline';
        F.Forecast_Owner__c = OppLi.Opportunity_Owner_Id__c;
        F.Business_Unit_Head__c = OppLi.Business_Unit_Head_Id__c;
        F.Business_Unit__c = OppLi.Resource_Business_Unit__c;
       // F.Opportunity_Type__c = 'T&M';
        F.Related_Opportunity__c= OppLi.Opportunity_IdNumber__c);
		
		update F;
}

}
}

 If a reply to a post answers your question or resolves your problem, please mark it as the solution to the post so that others may benefit.

 

ShirinShirin

Thanks I will try this and respond back. If the insert & update is not a bulkified code how do we managein case many records are updated/ inserted at a time

 

ShirinShirin

Chamil: I am getting the following error, can you help?

 

 

"Apex trigger ForecastInsertTest caused an unexpected exception, contact your administrator: ForecastInsertTest: execution of AfterUpdate caused by: System.QueryException: List has no rows for assignment to SObject: Trigger.ForecastInsertTest: line 7, column 18".

Imran MohammedImran Mohammed

Its always best practice to use a List while querying the object.

The query issued has returned no records to assign to Forecast object.

Try this 

Forecast[] f = [query];

//then check

if(f.size() > 0)

{

// do the process here

}

ShirinShirin

Hi Imran, I am getting this error message. can you help me on this?

 

Error: Compile Error: Initial term of field expression must be a concrete SObject: MAP<Id,OpportunityLineItemSchedule> at line 33 column 16

ShirinShirin

I am writing the code as follows for updating the record in the custom object Forecast.

 

 

trigger ForecastInsertTest on OpportunityLineItem (after update) {
 For (OpportunityLineItem OppLi : Trigger.New)
    {
list <OpportunityLineItemSchedule> schedule = [select Id, ScheduleDate,Revenue from OpportunityLineItemSchedule where OpportunityLineItemId = :OppLi.Id ];

For( Integer i=0; i< (schedule.size()) ;i++){
 Forecast__c F = [Select Amount__c,Date__c,Status__c,Forecast_Owner__c,Business_Unit_Head__c,Business_Unit__c,Opportunity_Type__c,Related_Opportunity__c from Forecast__c where Record_Id_BE__c=:schedule[i].Id];
 
        F.Amount__c = schedule[i].Revenue;
        F.Record_Id_BE__c = schedule[i].Id; 
        F.Date__c = schedule[i].ScheduleDate;
        //F.Status__c = 'Pipeline';
        F.Forecast_Owner__c = OppLi.Opportunity_Owner_Id__c;
        F.Business_Unit_Head__c = OppLi.Business_Unit_Head_Id__c;
        F.Business_Unit__c = OppLi.Resource_Business_Unit__c;
       // F.Opportunity_Type__c = 'T&M';
        F.Related_Opportunity__c= OppLi.Opportunity_IdNumber__c;
        
        update F;
}

}
}

 

 

 

I am getting an error as: "Apex trigger ForecastInsertTest caused an unexpected exception, contact your administrator: ForecastInsertTest: execution of AfterUpdate caused by: System.QueryException: List has no rows for assignment to SObject: Trigger.ForecastInsertTest: line 7, column 18".

 

I am unable to understand how to resolve this please help

ShirinShirin

Thanks Imran this actually helped me write the code and it works fine now.

 

thanks for the help