0

I am working with SOAP integration Is it possible creation of field in salesforce Dynamically, Based upon SOAP Request

Example-->>

Product      Qunatity       Price

pen           2            10

pen           3            9  

CanAny one give me some sample, if it possible means.

May i know how can i update my code as per metadata Api

List<Quote_Item__c> qilist = new List<Quote_Item__c>();
   for(QuoteLineWrapper wrapper : req.qlines){      
  Quote_Item__c qi = new Quote_Item__c();
     qi.Name = wrapper.qlineName;
   qi.Quantity__c = wrapper.quantity;
    qi.Unit_Price__c = wrapper.unitprice;
    qi.Orginal_Price__c = wrapper.orgPrice;
    qilist.add(qi); 
   }

  //MAP QUOTEITEM TO QUOTE AND PRODUCT
   for(Integer rowIdx = 0; rowIdx < qlist.size(); rowIdx++)
    qilist[rowIdx].Quote__c =qlist[rowIdx].Id       

   for(Integer rowIdx = 0; rowIdx < lstProduct.size(); rowIdx++)
    qilist[rowIdx].Product__c =lstProduct[rowIdx].Id;

      insert qilist;
  • You can create fields dynamically using the Salesforce Metadata API, but i sense that might not be what your asking here? Can you give a bit more information and background, the example you give does not seem to relate to your question? What is your actual requirement? – Andrew Fawcett Dec 19 '13 at 12:09
  • @Andrew Actually from third party application i am getting same product but 2 diffenrt qunaties, 2 differnt prices but these all are belongs under same Quote.But in sfdc i have only 1 qunatity, 1 price fields, if 2 qunatity, 2 price will come how can i map that values in my custom webservice. – Sathya Dec 19 '13 at 12:13
  • Ok, but what does this have to do with 'Is it possible creation of field in salesforce Dynamically'? Sorry still a little lost with your question... – Andrew Fawcett Dec 19 '13 at 12:14
  • @Andrew I want to create a field dynamically in salesforce based upon the soap request.May i know is it possible or not...?? – Sathya Dec 19 '13 at 12:17
  • I'm really sorry, I'm a bit confused more than ever now, are you really sure you want to 'create a new custom field' here? I cannot see in your code any place where you are needing to do this? – Andrew Fawcett Dec 19 '13 at 13:22
3

Salesforce Metadata API. It is possible to create a field dynamically in Salefsorce using the Salesforce Metadata API, which is a SOAP Web Service API which can be called from a number of langauges and platforms. You can see an example and further reading on how to do this in Apex from this repository and this blog here. The following is an example...

MetadataService.MetadataPort service = new MetadataService.MetadataPort();
service.SessionHeader = new MetadataService.SessionHeader_element();
service.SessionHeader.sessionId = UserInfo.getSessionId();

MetadataService.CustomField customField = new MetadataService.CustomField();
customField.fullName = 'Test__c.TestField__c';
customField.label = 'Test Field';
customField.type_x = 'Text';
customField.length = 42;
MetadataService.AsyncResult[] results =
    service.create(new List<MetadataService.Metadata> { customField }); 

Applying this to your use case. In your case you want to dynamically create two new fields if additional information is provided in your soap request. The main problem I see with this and the Metadata API (which is the only way to programatically create fields). Is this API is asynchronous meaning (as per the linked blog) you need to poll (since you cannot pause execution either) for the results until you get to know if the field has been created or not. Thus you will not be able to create the field and populate it in the same SOAP request as I think your hoping to do.

Summary. I would have thought it might be better to simply create enough fields up front and conditionally populate them based on the data you receive. Since even if you managed to work around the above (with some kind of differed updating of your rows after field create). Surely you need to also update layouts to show the new fields? So in my view I think you should probably head towards creating a set of these fields up front via the standard way in Salesforce and then populate them as and when needed.

| improve this answer | |
  • 2
    getting good mileage from that repo! – bigassforce Dec 19 '13 at 12:30
  • @Andrew Can you suggest me where can i include your code in my custom webservice my code is – Sathya Dec 19 '13 at 13:11
  • List<Quote_Item__c> qilist = new List<Quote_Item__c>(); for(QuoteLineWrapper wrapper : req.qlines){ Quote_Item__c qi = new Quote_Item__c(); qi.Name = wrapper.qlineName; qi.Quantity__c = wrapper.quantity; qi.Unit_Price__c = wrapper.unitprice; qi.Orginal_Price__c = wrapper.orgPrice; qilist.add(qi); } – Sathya Dec 19 '13 at 13:14
  • //MAP QUOTEITEM TO QUOTE AND PRODUCT for(Integer rowIdx = 0; rowIdx < qlist.size(); rowIdx++) qilist[rowIdx].Quote__c =qlist[rowIdx].Id; for(Integer rowIdx = 0; rowIdx < lstProduct.size();rowIdx++) qilist[rowIdx].Product__c =lstProduct[rowIdx].Id; insert qilist – Sathya Dec 19 '13 at 13:15
  • 2
    What is the business value of creating extra fields just because the price and quantity is different? why not just add a separate line item Opportunity line item and/or quote line item? or calculate the correct price and quantity based on the number of items you get? – Sven Delporte Dec 19 '13 at 14:27
0

It looks like you need many quotes for a single item. Rather than add columns dynamically, you should look at creating related records dynamically.

For example, if you have a Quotable_Item__c object, it might have a couple properties like name, dimensions, etc. Then you can create an Item_Quote object as such

Quotable_Item__c : the stuff that varies depending on who is selling the service/item.

  • Name
  • Description__c
  • Type__c (service or product?)
  • Dimensions__c (optional)
  • Weight__c (optional)
  • Material__c (optional)
  • (do NOT add price or quantity here -- those belong in the next object)

Item_Quote__c: contains properties that vary depending on context (who is offering the item at a certain price quote, for example).

  • Quoted_Item__c - Lookup field points to Quotable_Item__c.
  • Price__c - currency
  • Quantity__c - number
  • Frequency_Description__c - (text) - for subscription/service type quotes. e.g. 'charged biweekly'.
  • Source__c - (string/text) - Name/description of source of the quote.

Then when you are building out a page, show a related list of Quotes. When writing apex code, you can query for all related items with a secondary SELECT statement.

Quotable_Item__c item = [select ... from Quotable_Item__c WHERE ...];
List<Item_Quote> quotes = [SELECT Price__c, Quantity__c, Source__c, Id FROM Price_Quote__c WHERE Quoted_Item__c = :item.Id];

If you have additional random fields whose meaning really can vary widely, you may want to create another field called 'Additional_Data__c' that contains JSON or key-value pairs as text, or another related object with a Key__c and Value__c properties. But I strongly recommend against either of these. In most cases you should only accept data you are prepared to handle in an organized way.

Don't create multiple fields that mean the same thing (Price1, Price2, Price3...) -- this indicates a need for a separate related object.

| improve this answer | |

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.