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
Daniel MasonDaniel Mason 

Compile Error: Invalid field Materials for SObject Materials_Junction__C

Hi All, 

I am hoping you can help me.

This is my Controller, however when i am saving i am getting the the following error message "Error: Compile Error: Invalid field Materials for SObject Materials_Junction__c at line 33 column 17"

i have looked through the relevant examples on wrapper classes however i am a now stuck. 

Really appreciated you help 

public with sharing class testWrapper
{
    public List<Materials__c> Materials {get;set;} 
    public List<materialWrapper> materialWrapperList {get;set;} 
    
    public testWrapper()
    {
        Materials = [select ID,name,Product__c, Item__c,Quanity__c, Active__c from Materials__c where Active__c =true limit 10];
        for(Materials__c obj : Materials)
        {
            materialWrapper tempObj= new materialWrapper();
            tempObj.recordId = obj.id;
            tempObj.name = obj.name;
            tempObj.product = obj.Product__c;
            tempObj.item = obj.Item__c;
            tempObj.quantity = obj.Quanity__c;
            tempObj.selectB = false;
            materialWrapperList.add(tempObj);
        }
    }

    //save method
    public void save()
    {
        list<Materials_Junction__c> recordToInsert = new list<Materials_Junction__c>();
        
        for(materialWrapper obj : materialWrapperList)
        {
            if(obj.selectB == true)
            {
                Materials_Junction__c temp = new Materials_Junction__c();
                temp.sales_and_marketing__c = '01I20000000rV6V';
                temp.Materials= obj.recordIdId;
                temp.quantity = obj.quantity; 
            }
            recordToInsert.add(obj);
        }
        insert recordToInsert;
    }
    
    
    public class materialWrapper
    {
        public string recordId {get; set;}
        public string name {get; set;}
        public string product {get; set;}
        public string item {get; set;}
        public Decimal quantity {get; set;}
        public boolean selectB {get; set;}
        
        public void materialWrapper()
        {
            recordId = '';
            name = '';
            product = '';
            item = '';
            quantity = 0.0;
            selectB = false;
        }
    }
}

Materials Object API 

Materials Object API

Materials Object Fields 

Materials Object Fields

Materials_Junction API 

Materials Junction Object API

Materials_Junction Fields 
Materials Junction Object Fields
 

RAM AnisettiRAM Anisetti
33           temp.Materials__C= obj.recordIdId;
34           temp.quantity__C = obj.quantity;
modify the above lines.please use API name of fields not labels
 
Daniel MasonDaniel Mason

HI Ram, 

I think get the following error "Compile Error: Incompatible element type testWrapper.materialWrapper for collection of Materials_Junction__c at line 36 column 1" 

RAM AnisettiRAM Anisetti
Hi,

Could you change the below line and try once.
 
​48        public Number quantity {get; set;}

 
Daniel MasonDaniel Mason
That gives me "Compile Error: Invalid type: Number at line 48 column 16" . quanitiy on the Materials_Junction__c has the following data type Number(18, 0)
Daniel MasonDaniel Mason

Hi ,

 

I have made the following edits and i can now save the apex class 

 

Line 36 :

recordToInsert.add(obj);

I then changed the following 

for(materialWrapper obj : materialWrapperList)
        {
            if(obj.selectB == true)
            {
                Materials_Junction__c temp = new Materials_Junction__c();
                temp.sales_and_marketing__c = '01I20000000rV6V';
                temp.Materials= obj.recordIdId;
                temp.quantity = obj.quantity; 
            }
            recordToInsert.add(obj);
        }
        insert recordToInsert;
    }

To 
for(materialWrapper obj : materialWrapperList)
        {
         Materials_Junction__c temp ;
            if(obj.selectB == true)
            {
                temp = new Materials_Junction__c();
                temp.sales_and_marketing__c = '01I20000000rV6V';
                temp.Materials__C= obj.recordId;
                temp.quantity__C = obj.quantity; 
            }
            recordToInsert.add(temp);
        }
RAM AnisettiRAM Anisetti
try once,
 
public with sharing class testWrapper
{
    public List<Materials__c> Materials {get;set;} 
    public List<materialWrapper> materialWrapperList {get;set;} 
    
    public testWrapper()
    {
        Materials = [select ID,name,Product__c, Item__c,Quanity__c, Active__c from Materials__c where Active__c =true limit 10];
        for(Materials__c obj : Materials)
        {
            materialWrapper tempObj= new materialWrapper();
            tempObj.recordId = obj.id;
            tempObj.name = obj.name;
            tempObj.product = obj.Product__c;
            tempObj.item = obj.Item__c;
            tempObj.quantity = obj.Quanity__c;
            tempObj.selectB = false;
            materialWrapperList.add(tempObj);
        }
    }

    //save method
    public void save()
    {
        list<Materials_Junction__c> recordToInsert = new list<Materials_Junction__c>();
        
        for(materialWrapper obj : materialWrapperList)
        {
            if(obj.selectB == true)
            {
                Materials_Junction__c temp = new Materials_Junction__c();
                temp.sales_and_marketing__c = '01I20000000rV6V';
                temp.Materials= obj.recordIdId;
                temp.quantity = Integer.valueOf(obj.quantity); 
            }
          // i changed below line
            recordToInsert.add(temp);
        }
        insert recordToInsert;
    }
    
    
    public class materialWrapper
    {
        public string recordId {get; set;}
        public string name {get; set;}
        public string product {get; set;}
        public string item {get; set;}
        public Decimal quantity {get; set;}
        public boolean selectB {get; set;}
        
        public void materialWrapper()
        {
            recordId = '';
            name = '';
            product = '';
            item = '';
            quantity = 0.0;
            selectB = false;
        }
    }
}