0
//ICRE
Map<String,String> objName = new Map<String,String>();
for(Schema.SObjectType f : Schema.getGlobalDescribe().Values()){
    if(f.getDescribe().isCustom()){
        objName.put(f.getDescribe().getLabel(),f.getDescribe().getName());
    }
}                                    

if(objName.containsKey('ICRE')){
    Map<Id,sObject> mapClonedICRE = new Map<Id,sObject>();
    string sObj = objName.get('ICRE');
    system.debug('sObj::'+sObj);
    sObject oInstance;                                    
    sDynamicQuery = this.dynamicQuery(sObj);
    query = 'SELECT ' + sDynamicQuery + ' FROM '+ sObj +' WHERE Product__c IN :lstProdOfSourceEE limit 5000';
    lstofICRE = Database.query(query);
    system.debug('lstofICRE::'+lstofICRE);

    for(sObject oICRE :lstofICRE){        
        oInstance = Schema.getGlobalDescribe().get(sObj).newSObject();                                         
        oInstance = oICRE.clone(false, false, false, false); 
        system.debug('oInstanceNew::'+oInstance);  
        //oInstance.oInstance.get('ownerId') = userinfo.getuserid();
        /*oInstance.ownerId = userinfo.getuserid();                                                                                                                           
          if(mapClonedPrdwithSourcePrdId.containskey(oICRE.Product__c)){
            oInstance.Product__c = mapClonedPrdwithSourcePrdId.get(oICRE.Product__c).Id;
          }else{
            oInstance.Product__c = null;
          }

          if(oICRE.Start_Date__c != null)
            oInstance.Start_Date__c = String.valueOf(oTargetEE.get('Start_Date__c'));                                            

          if(oICRE.End_Date__c != null)
            oInstance.End_Date__c = oTargetEE.End_Date__c;

          if(oInstance.Product__c != null){
            mapClonedICRE.put(oICRE.Id, oInstance);
          }*/                                       
    }
    if(!mapClonedICRE.isEmpty()){
        Insert mapClonedICRE.values();
        system.debug('ICRE::'+mapClonedICRE.values().size());
        system.debug('ICRECon::'+mapClonedICRE.values());
    }
}

Error on this Line -

oInstance.Product__c = mapClonedPrdwithSourcePrdId.get(oICRE.Product__c).Id;

2
  • Can you please tell what is the map key and value type? Jul 5 '17 at 10:56
  • Map Key - Product Id and Value - product object instance(Map<Id, Product2> mapClonedPrdwithSourcePrdId = new Map<Id, Product2>();) Jul 5 '17 at 11:23
0

The error is pretty descriptive of your issue.

A 'field expression' is something like myObjectInstance.myField, using dot notation on an object instance to get or set the value of a field.

As the error states, you aren't allowed to do this on generic SObject instances.

Taking a look at some very specific parts of your provided code, we see this

for(sObject oICRE :lstofICRE){
    // other code

    oInstance.Product__c = mapClonedPrdwithSourcePrdId.get(oICRE.Product__c).Id;

    // more code
}

oICRE and oInstance are generic sObjects in this loop, so oInstance.Product__c and oICRE.Product__c are the specific parts of the line giving you your error.

To work around that, you need to use the SObject.get(_field_) method (when you need to fetch a value from an SObject) and the SObject.put(_field_, _value_) (when you need to set a value on an SObject) methods instead

for(sObject oICRE :lstofICRE){
    // other code

    // Instead of using dot notation, you'll need to use the get() and put() methods
    //   and pass a string matching the API name of your target field
    oInstance.put('Product__c',  mapClonedPrdwithSourcePrdId.get(oICRE.get('Product__c')).Id);

    // more code
}

There are a few other instances in your provided class where you attempt to directly reference a field on a generic SObject (like if(oICRE.Start_Date__c != null)). You'll need to do the same thing (use get() and/or put()) to those as well.

-1

Please use

//ICRE
Map<String,String> objName = new Map<String,String>();
for(Schema.SObjectType f : Schema.getGlobalDescribe().Values()){
    if(f.getDescribe().isCustom()){
        objName.put(f.getDescribe().getLabel(),f.getDescribe().getName());
    }
}                                    

if(objName.containsKey('ICRE')){
    Map<Id,sObject> mapClonedICRE = new Map<Id,sObject>();
    string sObj = objName.get('ICRE');
    system.debug('sObj::'+sObj);
    sObject oInstance;                                    
    sDynamicQuery = this.dynamicQuery(sObj);
    query = 'SELECT ' + sDynamicQuery + ' FROM '+ sObj +' WHERE Product__c IN :lstProdOfSourceEE limit 5000';
    lstofICRE = Database.query(query);
    system.debug('lstofICRE::'+lstofICRE);

    for(sObject oICRE :lstofICRE){        
        oInstance = Schema.getGlobalDescribe().get(sObj).newSObject();                                         
        oInstance = oICRE.clone(false, false, false, false); 
        system.debug('oInstanceNew::'+oInstance);  
        //oInstance.oInstance.get('ownerId') = userinfo.getuserid();
        /*oInstance.ownerId = userinfo.getuserid();                                                                                                                           
          if(mapClonedPrdwithSourcePrdId.containskey(oICRE.Product__c)){
            oInstance.put('Product__c', mapClonedPrdwithSourcePrdId.get(oICRE.Product__c).Id);
            //oInstance.Product__c = mapClonedPrdwithSourcePrdId.get(oICRE.Product__c).Id;
          }else{
            oInstance.put('Product__c', null);
            //oInstance.Product__c = null;
          }

          if(oICRE.Start_Date__c != null)
            oInstance.put('Start_Date__c', String.valueOf(oTargetEE.get('Start_Date__c'));
            //oInstance.Start_Date__c = String.valueOf(oTargetEE.get('Start_Date__c'));                                            

          if(oICRE.End_Date__c != null)
            oInstance.put('End_Date__c', oTargetEE.End_Date__c);
            //oInstance.End_Date__c = oTargetEE.End_Date__c;

          if(oInstance.get('Product__c') != null){
            mapClonedICRE.put(oICRE.Id, oInstance);
          }*/                                       
    }
    if(!mapClonedICRE.isEmpty()){
        Insert mapClonedICRE.values();
        system.debug('ICRE::'+mapClonedICRE.values().size());
        system.debug('ICRECon::'+mapClonedICRE.values());
    }
}
6
  • I think it should be: oInstance.put('Product__c', mapClonedPrdwithSourcePrdId.get(oICRE.Product__c).Id);
    – Liron C
    Jul 5 '17 at 11:39
  • I got the error,Method does not exist(sobject).put[string] Jul 5 '17 at 11:47
  • yes you are right its my bad it will be oInstance.put('Product__c', mapClonedPrdwithSourcePrdId.get(oICRE.Product__c).Id); Jul 5 '17 at 11:47
  • I changed the the line but still facing the fields expression error. Jul 5 '17 at 11:55
  • Hi made changes for sobject expression in whole of the code please check my above edited code Jul 5 '17 at 11:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.