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
URVASHIURVASHI 

Instance of string to instance of schema.sobjectfield in salesforce

hi,

i have a question can anyone please help.

how to convert instance of string to instance of schema.sobjectfield in salesforce.

Is there any method for it?

Please give a small example of the method.

 

Thanks

neeedhelpneeedhelp
Can you post your code so that it will be Very clear on what you want to get
URVASHIURVASHI

thanks for the reply.Above is the code:

 

public string[] selectedValue2{get;set;}
                Contact c = new Contact();
                for(Integer i=0;i<=headerXLS.size();i++)
                {
                     for(Integer j=0;j<=selectedValue1.size();j++)
                     {
                         if (headerXLS[i] == selectedValue1[j])
                             {
                             c.selectedValue2[j] = inputvalues[i];
                             }
                     }
                 }

here selectedvalue2 is a value getting selected in listbox in visualforce page.

when i use this code im getting error saying that "Compile Error: Invalid field selectedValue2 for SObject Contact"

as c is an object instance and selectedvalue2 is a string.

what is the solution for this.

How do i use a string as Sobjectfield?

 

Thanks.

neeedhelpneeedhelp

U can't pass the values like that.If you want to pass the value into a Particular field of contact then you can specify as

 

c.Field__c = inputValues[i];

 

Here can i know what is the datatype of inputValues[i].

URVASHIURVASHI

inputvalues is a string array

 String[] inputvalues = new String[]{};

and can i know what is field__c?

neeedhelpneeedhelp

Here Field__c is a custom field in your contact object where you can store the values of Inputvalues[i] into it as

                            c.Field__c = inputValues[i];

 

I don't know what excatly you are trying to do.You can pass values like this also

 

                            if (headerXLS[i] == selectedValue1[j])
                             {
                             selectedValue2[i] = inputvalues[i];
                             }

 

But you can't pass values as c.selectedValue2[j] = inputvalues[i];

URVASHIURVASHI

i will explain you what im trying to do.

My Requirement is something like i want to save value of 'inputvalues' which is a string array to objectname.fieldname

but  my fieldname is coming from an another string array named selectedvalue2.

So i cant use both the options you gave me.

Can this be done through any other way like getGlobalDescribe() or something else?

chioikbhchioikbh
Try to do it

c.put(selectedValue2[j], inputvalues[i]);
update c
URVASHIURVASHI

c.put isnt working.Please provide other solution.
I wll explain my problem once again

Contact c = new Contact();
 c.FirstName = inputvalues[0];
 c.LastName = inputvalues[1];

 Instead of the above code, i want it in the following way:

Contact c = new Contact();
   for(Integer j=0;i<HeaderXLS.size();j++)
   {
    c.put(HeaderXLS[j],inputvalues[j]);
    }

 Here HeaderXLS and Inputvalues both are String Array.
 But c.put isnt working.
 HeaderXLS[j] has field names for a particular object.
 I want

   c.HeaderXLS[j] = inputvalues[j];

 but even this isnt working.Please provide a solution for the problem.


Thanks.

chioikbhchioikbh

what means c.put isn't working? Can you save code succesfully which means there is no grammar error?

URVASHIURVASHI

yea.no syntax errors.

but i am not getting the output.

chioikbhchioikbh

first try to debug print out c.put(XX), for checking it is correct text to put

if it is correct, try to debug c.XXfield and check the value

Next, if it is empty value, try to use generic sobject to create contact, something like sobject c= new Contact();