1

I am integrating Salesforce with my third party app and we both need to share a common mapping attribute for Account Object.

These are the steps I follow:

  1. I created a Custom Setting called XYZ_Custom_Settings which has a Mapping Attribute of type Text XYZ_Mapping_Column_Name.

  2. I display a SelectOption in My Settings tab by describing all the fields in the Account Custom Object.

  3. Admin of the Customer Salesforce App changes this setting appropriately to be in sync with my system.

  4. Once this setting is done I will fetch the mapping attribute as XYZ_Mapping_Column_Name.XYZ_Custom_Settings and will send it as a

    ...&params[:mapping_attribute_value]=result

to my web Application.

Assuming the Admin has set "Id" I will map my Account with SF_ACCOUNT_ID.

String query = 'SELECT '+XYZ_Mapping_Column_Name.XYZ_Custom_Settings+' FROM Account where id=\''+account.id+'\'';
Account acc = Database.query(query);
return (String)acc.get(XYZ_Mapping_Column_Name.XYZ_Custom_Settings);   

Problem:

  • Current problem I am facing is since I display all Fields of Account in mapping. When Admin wrongly sets an object which cannot be Stringified by String cast, then it throws an error as follows:

    Content cannot be displayed: Invalid conversion from runtime type Date to String

  • This happens for all the attributes which cannot be casted by String.

  • I can't put a limitation on which fields can be mapped, since it can be any Custom Field and my Managed Package should work across them.

  • Is there a generic way to convert any Attribute to String ? ( as java would respond with Object hash by default if it doesn't has its own toString() )

I tried this zConvert but still I need to know the Class of Mapping Attribute to call this zConvert.ToString(SpecificClass). By Default the attribute is an Object class.

2
  • 1
    why you need to type cast as a string ? Commented Aug 29, 2013 at 16:38
  • Thanks Mohith. As you mentioned, it worked without a typecast. I just added 'value='+CustomObject#instance+'...' without type casting and it worked fine. Commented Sep 3, 2013 at 5:36

1 Answer 1

3

Use the instanceof operator in Apex to find out the type and call the appropriate method on the zConvert class, casting the object value returned from Dynamic Apex, for example.

Object objectValue = acc.get(XYZ_Mapping_Column_Name.XYZ_Custom_Settings); 
String textValue = null;
if(value instanceof DateTime)
     textValue = zConvert.ToString((DateTime) fieldValue);
else if(value instanceof Long)
     textValue = zConvert.ToString((Long) fieldValue);
// ... and so on ...

You must log in to answer this question.

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