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
BobPBobP 

Invalid loop variable type expected SObject

I am working on a trigger and keep getting the following error but i cant seem to find the issue with the code. Any help on this would be greatly appreciated.
 
public class supportClassForRequestUpdateOpps {
     
       Map<ID, Opportunity> parentOpps = new Map<ID, Opportunity>(); 
    List<Id> listIds = new List<Id>();{

    for (Request_for_Special_Payment__c relRec: trigger.new) 
    {
    listIds.add(relRec.Opportunity_Name__c);
    }

    parentOpps = new Map<Id, Opportunity>([SELECT id, Special_Payment_Terms__c 
                                            FROM Opportunity 
                                             WHERE ID IN :listIds]);

    for (Request_for_Special_Payment__c r :Trigger.new){
    Opportunity myParentOpp = parentOpps.get(r.Opportunity);

    if(r.Special_Payment_Terms__c == '')
    {
     myParentOpp.Special_Payment_Terms__c = r.Requested_Payment_Terms__c;
    }
    
 }

  update parentOpps.values();
  }
}

 
Raj VakatiRaj Vakati
Two changes 

1 . You need to wrap You logic in method  like below 
 
public class supportClassForRequestUpdateOpps {
	
	
	public static void updateOpp(List<Request_for_Special_Payment__c> listOfRerSer){
     
    Map<ID, Opportunity> parentOpps = new Map<ID, Opportunity>(); 
    List<Id> listIds = new List<Id>();{

    for(Request_for_Special_Payment__c relRec: listOfRerSer) 
    {
    listIds.add(relRec.Opportunity_Name__c);
    }

    parentOpps = new Map<Id, Opportunity>([SELECT id, Special_Payment_Terms__c 
                                            FROM Opportunity 
                                             WHERE ID IN :listIds]);

    for (Request_for_Special_Payment__c r :listOfRerSer){
    Opportunity myParentOpp = parentOpps.get(r.Opportunity__c);

    if(r.Special_Payment_Terms__c == '')
    {
     myParentOpp.Special_Payment_Terms__c = r.Requested_Payment_Terms__c;
    }
    
	}

	update parentOpps.values();
  }
	}
}

2 .You need to use Opportunity__c from Request_for_Special_Payment__c
Opportunity myParentOpp = parentOpps.get(r.Opportunity__c);


Here is the compleet code
 
public class supportClassForRequestUpdateOpps {
	
	
	public static void updateOpp(List<Request_for_Special_Payment__c> listOfRerSer){
     
    Map<ID, Opportunity> parentOpps = new Map<ID, Opportunity>(); 
    List<Id> listIds = new List<Id>();{

    for(Request_for_Special_Payment__c relRec: listOfRerSer) 
    {
    listIds.add(relRec.Opportunity_Name__c);
    }

    parentOpps = new Map<Id, Opportunity>([SELECT id, Special_Payment_Terms__c 
                                            FROM Opportunity 
                                             WHERE ID IN :listIds]);

    for (Request_for_Special_Payment__c r :listOfRerSer){
    Opportunity myParentOpp = parentOpps.get(r.Opportunity__c);

    if(r.Special_Payment_Terms__c == '')
    {
     myParentOpp.Special_Payment_Terms__c = r.Requested_Payment_Terms__c;
    }
    
	}

	update parentOpps.values();
  }
	}
}

 
BobPBobP
Thank you for your help but i am getting the following error when i try to compile the code.


Error: Compile Error: Variable does not exist: Opportunity__c at line 19 column 48
Raj VakatiRaj Vakati
Can u check what is the API name for  Opportunity__c on Request_for_Special_Payment__c  object 
Maharajan CMaharajan C
HI Bob,

In the Line 19 please use your Opportunity Lookup Field API Name from Request_for_Special_Payment__c Object:

Opportunity myParentOpp = parentOpps.get( Here Use your Opportunity Field Api Name );  

Thanks,
Maharajan.C
BobPBobP
The field is Opportunity_Name__c but when i changed the field name it gave me the following error.

Error: Compile Error: Variable does not exist: Special_Payment_Terms__c at line 21 column 10
public class supportClassForRequestUpdateOpps {
    
    
    public static void updateOpp(List<Request_for_Special_Payment__c> listOfRerSer){
     
    Map<ID, Opportunity> parentOpps = new Map<ID, Opportunity>(); 
    List<Id> listIds = new List<Id>();{

    for(Request_for_Special_Payment__c relRec: listOfRerSer) 
    {
    listIds.add(relRec.Opportunity_Name__c);
    }

    parentOpps = new Map<Id, Opportunity>([SELECT id, Special_Payment_Terms__c 
                                            FROM Opportunity 
                                             WHERE ID IN :listIds]);

    for (Request_for_Special_Payment__c r :listOfRerSer){
    Opportunity myParentOpp = parentOpps.get(r.Opportunity_Name__c);

    if(r.Special_Payment_Terms__c == '')
    {
     myParentOpp.Special_Payment_Terms__c = r.Requested_Payment_Terms__c;
    }
    
    }

    update parentOpps.values();
  }
    }
}
BobPBobP
Hello,
 I updated the code below and I was able to save it but nothing happening when I try to update the Opportunity the Special_Payment_Terms__c field remains blank. I did have a incorrect feild name so I changed it to Required_Payment_Terms__c
 
public class supportClassForRequestUpdateOpps {
    
    
    public static void updateOpp(List<Request_for_Special_Payment__c> listOfRerSer){
     
    Map<ID, Opportunity> parentOpps = new Map<ID, Opportunity>(); 
    List<Id> listIds = new List<Id>();{

    for(Request_for_Special_Payment__c relRec: listOfRerSer) 
    {
    listIds.add(relRec.Opportunity_Name__c);
    }

    parentOpps = new Map<Id, Opportunity>([SELECT id, Special_Payment_Terms__c 
                                            FROM Opportunity 
                                             WHERE ID IN :listIds]);

    for (Request_for_Special_Payment__c r :listOfRerSer){
    Opportunity myParentOpp = parentOpps.get(r.Opportunity_Name__c);

    if(r.Opportunity_Name__r.Special_Payment_Terms__c == '')
    {
     myParentOpp.Special_Payment_Terms__c = r.Required_Payment_Terms__c;
    }
    
    }

    update parentOpps.values();
  }
    }
}



 
Maharajan CMaharajan C
Hi Bob,

Try the below updated code:

public class supportClassForRequestUpdateOpps {
    
    public static void updateOpp(List<Request_for_Special_Payment__c> listOfRerSer){

        Map<ID, Opportunity> parentOpps = new Map<ID, Opportunity>(); 
        Set<Id> setIds = new set<Id>();
        
        for(Request_for_Special_Payment__c relRec: listOfRerSer) 
        {
            setIds.add(relRec.Opportunity_Name__c);
        }
        
        parentOpps = new Map<Id, Opportunity>([SELECT id, Special_Payment_Terms__c 
                                               FROM Opportunity 
                                               WHERE ID IN :setIds]);
        
        for (Request_for_Special_Payment__c r :listOfRerSer){
            
            if(parentOpps.containsKey(r.Opportunity_Name__c))
            {
                if(parentOpps.get(r.Opportunity_Name__c).Special_Payment_Terms__c == '')
                {
                    parentOpps.get(r.Opportunity_Name__c).Special_Payment_Terms__c = r.Requested_Payment_Terms__c;
                }
            }
        }
        
        if(!parentOpps.isEmpty())
        update parentOpps.values();
        
    }
}


Can you please Let me know if it helps or not!!!

If it helps don't forget to mark this as a best answer!!!


Thanks,
Maharajan.C
 
BobPBobP
Hello Maharajan C,
Thank you for your help. Igettingthe following error

Error: Compile Error: Variable does not exist: Requested_Payment_Terms__c at line 23 column 88
Raj VakatiRaj Vakati
Its looks like API name is  not correct for Requested_Payment_Terms__c  change code as below 
 
public class supportClassForRequestUpdateOpps {
    
    public static void updateOpp(List<Request_for_Special_Payment__c> listOfRerSer){

        Map<ID, Opportunity> parentOpps = new Map<ID, Opportunity>(); 
        Set<Id> setIds = new set<Id>();
        
        for(Request_for_Special_Payment__c relRec: listOfRerSer) 
        {
            setIds.add(relRec.Opportunity_Name__c);
        }
        
        parentOpps = new Map<Id, Opportunity>([SELECT id, Special_Payment_Terms__c 
                                               FROM Opportunity 
                                               WHERE ID IN :setIds]);
        
        for (Request_for_Special_Payment__c r :listOfRerSer){
            
            if(parentOpps.containsKey(r.Opportunity_Name__c))
            {
                if(parentOpps.get(r.Opportunity_Name__c).Special_Payment_Terms__c == '')
                {
                    parentOpps.get(r.Opportunity_Name__c).Special_Payment_Terms__c = r.<GET THE CORRECT API NAME>;
                }
            }
        }
        
        if(!parentOpps.isEmpty())
        update parentOpps.values();
        
    }
}


 
Barry allen 4Barry allen 4
I fixed the field and i was able to compile and save. But nothing is happening when i edit the opportunity or create a new request for payment terms record. https://get-shareit.com
 
BobPBobP
I ended up creating a ne wprocess in process builder it is doing what i need it to. 
Thank you everyone for your help