+ Start a Discussion
jblon1973jblon1973 

Help with save error - Incompatible element type

Hopefully someone can help me out here. I have written the following code:

 

 

public with sharing class insertPatientProtocolSchedule {
  //added an instance varaible for the standard controller
     private ApexPages.StandardController controller {get; set;}
         
     // initialize the controller
     public insertPatientProtocolSchedule(ApexPages.StandardController controller) {
 
        //initialize the stanrdard controller
        this.controller = controller;
        
        //Define Master Objects
  Protocol_Arm__c pa = new Protocol_Arm__c();
  Patient_Protocol_Record__c ppr = new Patient_Protocol_Record__c();
  //Create a list of objects.  Populate it with a query to the database.
  list<Protocol_Arm_Activities__c> pactList = [SELECT Name FROM Protocol_Arm_Activities__c WHERE Protocol_Arm__r.Id = :ppr.Protocol_Arm__r.Id];
//Cycle through the list, and for each object create a new one, the only difference being the MasterId
  list<Protocol_Arm_Activities__c> NewList = new list<Protocol_Arm_Activities__c>();
  for (Protocol_Arm_Activities__c paa : pactList) {
     Patient_Protocol_Activity__c ppa = new Patient_Protocol_Activity__c();
     ppa.Name = paa.Name;
     ppa.Patient_Protocol_Record__c = ppr.Id;
     NewList.add(ppa); // Error Line
  }
// insert new records
  insert NewList;
    }
 
    // method called from the VF's action attribute to clone the schedule
    public PageReference addSchedule() {
}
}
And I am getting the following Error In Eclipse on the line I have marked with //Error Line:
Save error: Incompatible element type SOBJECT:Patient_Protocol_Activity__c for collection of SOBJECT:Protocol_Arm_Activities__c
I am not seeing any issues.
Both Patient_Protocol_Activity__c and Protocol_Arm_Activities__c are the Detail in a Master-Detail relationship.
Thank you for any help you can provide.

 

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

You are hitting this problem because Apex lists are strongly typed.

 

Your newlist is declared as:

 

 

List<Protocol_Arm_Activities__c> NewList = new list<Protocol_Arm_Activities__c>();

 

 

Which means that it can only contain instances of Protocol_Arm_Activities__c.  The compiler is rejecting the line of code that attempts to add an instance of Patient_Protocol_Activity__c.

 

While from the relationship perspective these are the same, in that they are "children" of a Protocol_Arm__c record, they are different types as far as Apex is concerned and thus you may not combine them.

 

Looking at your code, you are only ading records to the list, so you can simply change the type of object to that in the list declaration, e.g.

 

 

List<Patient_Protocol_Activity__c > NewList = new list<Patient_Protocol_Activity__c >();

 

 

All Answers

bob_buzzardbob_buzzard

You are hitting this problem because Apex lists are strongly typed.

 

Your newlist is declared as:

 

 

List<Protocol_Arm_Activities__c> NewList = new list<Protocol_Arm_Activities__c>();

 

 

Which means that it can only contain instances of Protocol_Arm_Activities__c.  The compiler is rejecting the line of code that attempts to add an instance of Patient_Protocol_Activity__c.

 

While from the relationship perspective these are the same, in that they are "children" of a Protocol_Arm__c record, they are different types as far as Apex is concerned and thus you may not combine them.

 

Looking at your code, you are only ading records to the list, so you can simply change the type of object to that in the list declaration, e.g.

 

 

List<Patient_Protocol_Activity__c > NewList = new list<Patient_Protocol_Activity__c >();

 

 

This was selected as the best answer
jblon1973jblon1973

Thank you. that is a great help.

ApexDev129ApexDev129

Hi Bob

 

I have almost landed in the same problem.Could you please suggest me a solution.The error i am getting is "Compile Error: Invalid bind expression type of SOBJECT:Activities__c for Id field of SObject ServiceRequest__c at line 8 column 136".I am trying to update the status field of the master record (Service Request) to close when the subsequent child activities are closed.

 

trigger ChangeStatus on Activities__c (after update) {
List<Activities__c> SR_Ids = new List<Activities__c>{};
for (Activities__c act : Trigger.New) {
if(act.ServiceRequest__r.Id!=null)
SR_Ids.add([SELECT ServiceRequest__r.Id FROM Activities__c where Status__c='Close']);
}

for(ServiceRequest__c[] SR_Status:[select Status__c,(select Status__c from Activities__r) from ServiceRequest__c where Id IN:SR_Ids ]);
{

//code goes here
}
}

 

 

bob_buzzardbob_buzzard

SR_Ids is a list of activities, so using it in the following soql;

 

for(ServiceRequest__c[] SR_Status:[select Status__c,(select Status__c from Activities__r) from ServiceRequest__c where Id IN:SR_Ids ]);
{

 

Will attempt to pull back all Service_Request__c objects whose ID matches that of an activity.  You will need to iterate the SR_Ids collection and extract the Service_Request__r.id field into a separate collection, then use that in the SOQL query above.

ApexDev129ApexDev129

Hi Bob,

 

Thanks for replying.I tried the following code.But it still get  a compile error:Illegal assignment from Id to LIST<Id> .I am new to apex,please help me.

 

Integer i=0;
List<Id> my_list= new List<Id>();
for (i=0;i<size(SR_Ids);i++){
my_list= SR_Ids.get(i).Id;
}

 

neeedhelpneeedhelp

Integer i=0;
Id my_list; //change this line.
for (i=0;i<size(SR_Ids);i++){
my_list= SR_Ids.get(i).Id;
}

Aarya Tiwary 12Aarya Tiwary 12
Set<Id> Ac_Id = new Set<Id>();
        For(Account acct : trigger.old){
            Ac_Id.add(acct);
        }

Same error appeared  "Incompatible element type Account for collection of Id"