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
Mayank_JoshiMayank_Joshi 

Compile Error: Loop variable must be of type Id

Hi ,

 

I am getting compile time error : Error: Compile Error: Loop variable must be of type Id at line 17 column 39 

 

 

trigger Trg_Model on Opportunity_Platform_Model__c (after insert) {

Set<ID> Id1 = new Set<ID>();
For(Opportunity_Platform_Model__c OpMd: trigger.new){
Id1.add(OpMd.Opportunity_name__c);

Set<id> Id2 = New Set<id> ();
For(Opportunity Opp :Id1){
Id2.add(Opp.Program__r.Forecast__r.Forecast_Platform__c);

set<Id> Id3 = new Set<Id> ();
Set<ID> Id4 = new Set<Id> ();
For(Platform_Model__c Pm :Id2){
Id4.add(Pm.id);
Id3.add(Pm.Platform_Master__c);

For(Opportunity_Platform_Model__c OpMd1:Id4){
if(OpMd1.Platform_Model_Selected__c != Pm.Id){
System.debug('Test');
}
}
}
}
}
}

 

Thanks,

Mayank Joshi 

bob_buzzardbob_buzzard

You have declared id1/id2/4 as sets of ids. but then you try to iterate them with control variables of Opportunity, Platform_Model__c or Opportunity_Platform_Model__c.

 

I would have thought you need to query back lists of those sobjects based on the ids and then iterate the lists.

 

 

Mayank_JoshiMayank_Joshi

Thanks Bob ,

 

I was working on it and able to resolve that and below is updated code .Trigger is working fine with Else Statement but returning error on IF at line Insert Id5 ; 

(Apex trigger Trg_Model caused an unexpected exception, contact your administrator: Trg_Model: execution of BeforeInsert caused by: System.SObjectException: DML statment cannot operate on trigger.new or trigger.old: Trigger.Trg_Model: line 32, column 1) : 

 

Also on After insert :

( execution of AfterInsert caused by: System.FinalException: Record is read-only: Trigger.Trg_Model: line 30, column 1)

I want to update lookup field Platform_Model_Selected__c if meeting criteria . How to resolve Read only error .

-----------------------------------------------------------------------------------------------

trigger Trg_Model on Opportunity_Platform_Model__c (before insert) {

Set<ID> Id1 = new Set<ID>();

For(Opportunity_Platform_Model__c OpMd: trigger.new){
Id1.add(OpMd.Opportunity_name__c);
system.debug('test1'+OpMd.Opportunity_name__c);

List<Opportunity> o1 =[Select Id,Program__r.Forecast__r.Forecast_Platform__c from Opportunity where Id IN:Id1];
Set<id> Id2 = New Set<id> ();
For(Opportunity Opp :o1){
Id2.add(Opp.Program__r.Forecast__r.Forecast_Platform__c);
System.debug('Test2'+Opp.Program__r.Forecast__r.Forecast_Platform__c);

List<Platform_Model__c> P1 =[Select Id,Platform_Master__c,Platform_Master__r.Name from Platform_Model__c where Platform_Master__c IN:Id2 ];
set<Id> Id3 = new Set<Id> ();
Set<ID> Id4 = new Set<Id> ();
For(Platform_Model__c Pm :P1){
Id4.add(Pm.id);
Id3.add(Pm.Platform_Master__c);
System.debug('Test3'+Pm.id);
System.debug('Test4'+Pm.Platform_Master__c);

List<Opportunity_Platform_Model__c> Id5 = new List<Opportunity_Platform_Model__c>();
List<Opportunity_Platform_Model__c> OPM1 = [Select id,Platform_Model_Selected__c from Opportunity_Platform_Model__c where Platform_Model_Selected__c IN:Id4 ];
For(Opportunity_Platform_Model__c OpMd1: Trigger.new){
if(OpMd1.Platform_Model_Selected__c == Pm.Id){

System.debug('Test5'+OpMd1.Platform_Model_Selected__c);
OpMd1.Platform_Model_Selected__c = Pm.Id;
Id5.add(OpMd1);
Insert Id5 ;
}
else if(OpMd1.Platform_Model_Selected__c != Pm.Id){
OpMd1.addError('Test Error');
}
}
}
}
}

}

 

Thanks

bob_buzzardbob_buzzard

As you are in a before insert, the records haven't been written to the database yet.  You can therefore change the contents of the record and your changes will be persisted.