0

ayudad need. I want to create a batch class to update values ​​from another object. this is my code but it is giving me this error

anyone can help? Thanks.

This is the start and execute section.

global Database.Querylocator start(Database.BatchableContext bc){
    return Database.getQueryLocator([SELECT ID, Comentarios_Invitados__c,EventRelationId__c,Estado_Invitado__c,fecha_del_evento__c FROM Informe_icex__c where fecha_del_evento__c>=LAST_N_DAYS:1]);
}

global void execute(Database.BatchableContext BC, List<sObject> scope) {
    // Create a map for the chunk of records passed into method.
    Map<ID, Informe_icex__c> iMap = new Map<ID, Informe_icex__c>((List<Informe_icex__c>)scope); 

    List<EventRelation> eRList = [SELECT Id,response,status FROM EventRelation WHERE Id IN :iMap.keySet()];

    for(Informe_icex__c i : jobMap.values()) {
        if(!eRList.isEmpty()) {
            i.Comentarios_Invitados__c = eRList.response;
            i.Estado_Invitado__c = eRList.status;
        }
    }
    update iMap;
}
2
  • You have created a map of Informe_icex__c object and in the next line you're querying the eventRelation object where you're using the id's from map (which are of type Informe_icex__c object) and also why are you using the jobMap in for loop, shouldn't you use the iMap in for loop?
    – Sam
    Commented Jul 16, 2013 at 8:55
  • Thanks @TheGreatDanton, I can explain that I do, I'm new to this and am learning.
    – Jesus
    Commented Jul 16, 2013 at 9:01

2 Answers 2

2

eRList is a list of 0 to many objects. i.Comentarios_Invitados__c is a single field. You can't fit a list into a field.

Where you have

i.Comentarios_Invitados__c = eRList.response;

Should look more like

i.Comentarios_Invitados__c = eRList[0].response;

Although I think your looping logic is a bit confused! But that attempted assignment of a List into a field which can hold only a single sObject reference is the reason for the error message you see.

1

You are attempting to access a list as if it is an sObject, you need to iterate over the list to first access the sObjects.

From your example it's a bit hard to understand what you are trying to achieve, so you may have to figure out the rest yourself.

Also, you can't give the update statement a map. You can however just call values() which returns the list of values from a map.

update iMap.values();
3
  • Thanks Tom Gangemi, but the error is in the line 20 i.Comentarios_Invitados__c = eRList.response; I make the change but I have the same error, Thanks
    – Jesus
    Commented Jul 16, 2013 at 8:37
  • My mistake, I've updated my answer accordingly. Commented Jul 16, 2013 at 9:05
  • I make the change, not have error but not update the records Map<ID, EventRelation> erMap = new Map<ID, EventRelation>([SELECT Id,response,status FROM EventRelation WHERE Id IN :iMap.keySet()]); for(Informe_icex__c i : iMap.values()) { EventRelation er = erMap.get(i.Id); if(er != null) { i.Comentarios_Invitados__c =er.response; i.Estado_Invitado__c = er.status; } } update iMap.values(); }
    – Jesus
    Commented Jul 16, 2013 at 13:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .