+ Start a Discussion
Tabrez AlamTabrez Alam 

Taking a field name dynamically & assigning the value dynamically

I'm taking the records that meet the criteria in the list & then I'm updating the field

List<Object1__c> lstOfObject = [ SELECT Id, Object__c, Rule_Evaluation_Criteria__c, Status__c, (SELECT Id, Object2__r.Name,                      Object2__r.Object__c, Object2__r.Field__c, Object2__r.Operator__c, Object2__r.Value__c FROM Object1_Object2_JO__r), (SELECT Id, Object3__r.Rule_Name__c, Object3__r.Object_Name__c, Object3__r.Field_to_Update__c, Object3__r.Value_to_Update__c FROM Object1_Object2_JO__r) FROM Object1__c WHERE Object__c = 'Contact' AND Rule_Evaluation_Criteria__c = 'Record is Created' AND Status__c = 'Active' ];

This is the query that I'm using to get the records using the junction objects I've created a code if else condition to match the criteria that is entered
Once the criteria is met a field will be update so this is the code I'm using to update the field

for(Object1_Object3_JO__c objObject1Object3JO : objObject1.objObject1Object3JO __r)
{
if(isInCriteria==True)
{
a.put(objObject1Object3JO.Object3__r.Field_to_Update__c,objObject1Object3JO.Object3__r.Value_to_Update__c);
}
}

objObject1Object3JO.Object3__r.Field_to_Update__c
This variable stores the name of the field to update

objObject1Object3JO.Object3__r.Value_to_Update__c
This variable stores the value to assign to that field

I need the assign the Field that is stored in (objObject1Object3JO.Object3__r.Field_to_Update__c) the value that i have save in (objObject1Object3JO.Object3__r.Value_to_Update__c)

Please help me in achieve this
Thanks in advance
Jerome LusinchiJerome Lusinchi
Hi Tabrez,

try something like that : 

List<Object1_Object3_JO__c> Object1Object3JO_list = new List<Object1_Object3_JO__c>();

for(Object1_Object3_JO__c objObject1Object3JO : objObject1.objObject1Object3JO __r)
{
if(isInCriteria==True)
{
Object1_Object3_JO__c Object1_Object3_JO = new Object1_Object3_JO__c(Id = objObject1Object3JO.Id);
Object1_Object3_JO.set(objObject1Object3JO.Object3__r.Field_to_Update__c, objObject1Object3JO.Object3__r.Value_to_Update__c);

Object1Object3JO_list.add(Object1_Object3_JO );

}
}

database.update(Object1Object3JO_list);

Regards,
Jerome
Tabrez AlamTabrez Alam
HI Jerome

The functionality is the standard workflow field update that Im trying to achieve.
I've taken the getdescribe() method the get the objects of the org
Then i'm storing the object, the field that i need to update & the value that i want to assign to that field.
Object : Contact
Field : department (api)
value : test


The code is as follow
--------------------------------
if(Trigger.isInsert)

{
    List = query to get the list of records that have criteria as record is created. // getting the records in the list.
    
     for(Contact a : Trigger.new)
     {
         for(object instance : list) // pasing the list in the for loop
         Boolean isInCriteria = false;
         for()
         {
            Code to evalue the expression
             to make the isInCriteria true
             once the isInCriteria become true it wil come out of the loop
         }

          for(junctionobject instanceofjunction : instanceofparent.junctionobject__r)
          {
               if(isInCriteria==True)
               {
                   a.put(objRuleFieldUpdate.Field_Udpate__r.Field_to_Update__c, objRuleFieldUpdate.Field_Udpate__r.Value_to_Update__c);
                                        (field name that need to be updated)                                  (value that need to assign to the field)
               }
          }
     }
}

Then I'm writing a trigger on contact that will fire if the criteris is met that i have taken
When i created a contact that matches the criteria, it gives me the error as below else it gets saves normally

Error
-------
Apex trigger ContactFieldUpdate caused an unexpected exception, contact your administrator: ContactFieldUpdate: execution of BeforeInsert caused by:System.AssertException: Assertion Failed: Expected: department, Actual: FieldUpdate: Trigger.ContactFieldUpdate: line 221,column 1

This error appears on the line below
a.put(objRuleFieldUpdate.Field_Udpate__r.Field_to_Update__c, objRuleFieldUpdate.Field_Udpate__r.Value_to_Update__c);

I've done system.debug & i get both the values properly, for field name I get the api

Please suggest some thing.