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
MascotteMascotte 

Just a litle code that can help you !?

Hi,

For my personnel needs I have done generic "update" and "delete" functions.

Code:
public string Create(string type, Hashtable fields)
{
  try
  {
    Assembly A = Assembly.GetExecutingAssembly();
    Type[] Ts = A.GetExportedTypes();
    Type T = null;

    // Type
    object ob = null;
    foreach (Type t in Ts)
    if (t.Name == type)
    {
      T = t;
      break;
    }
    if (T == null)
      return null;

    // Object
    ob = Activator.CreateInstance(T);
    IDictionaryEnumerator Enum = fields.GetEnumerator();
    while (Enum.MoveNext())
    if (T.GetField(Enum.Key.ToString()) != null && Enum.Value != null)
    {
      FieldInfo info = T.GetField(Enum.Key.ToString());
      info.SetValue(ob, Enum.Value);
    }
    SaveResult[] Result = S.create(new sObject[]{(sObject)ob});
    if (Result[0].success == true)
      return Result[0].id;
    else {return null;}
  }
  catch (Exception ex){return null;}


I hope it will help you !
Just past the type of the object to create (ex: "Lead") and a HashTable
wish associates FieldName and Value (ex: "City" / "New York");
It manage fieldsToNull

Please tell me what you think about it !
SuperfellSuperfell
Have you tried using the partner API, that's pretty generic and doesn't need all the reflection stuff.
MascotteMascotte
Is it available to any organization ? or we must be a SalesForce Partner ?
SuperfellSuperfell
Anyone who can use the enterprise API can also use the partner API.
MascotteMascotte
Thank you I will try this !!!