Our company sells products in Thailand.

However , now what they are doing is for a product , they have different prices in different regions and it is based on UOM or the region in which product is sold.

I need to create a combination of 4 fields as the primary key to determine price of a product.

By that I mean, if any of the field of 4 fields has a different value , then the existing combination.

A new price for the product will be stored.

How can I create such a key?

  • I guess Perform trigger operation before upsert. generate your primary key in trigger,
    Nov 21 '17 at 5:08
  • Hi, I need to create a primary key with combination of 4 fields, i.e. Product + Sales Unit Of Measure + Currency + Province. If any of the 4 fields differ, it will result in a different price for the product. Currently the logic goes just for the region. But currently the country I have been working for is maintaining price on the basis of Sales unit of Measure. I have added fields on the standard price page. But how do i form a primary key with the combination of these fields? Nov 29 '17 at 7:08
  • Find below answer, will work for you.
    Nov 29 '17 at 9:12
  • You can use external IDs to prevent duplicate records from being created as a result of the import operation.

  • An external ID is a custom field that has the “External ID” attribute, meaning that it contains unique record identifiers from a system outside of Salesforce. When you select this option, the import wizard will detect existing records in Salesforce that have the same external ID. Note that this operation is not case-sensitive - for example, “ABC” will be matched with “abc”. However, there is an exception: if the custom field has the separate “Unique” attribute and the case-sensitive option for that attribute is selected, uppercase and lowercase letters will not be considered identical.

  • Salesforce allows you mark up to 3 fields as External IDs and these fields must be text, number or email field types.Values in these External ID field must also be unique and you can also determine whether or not value are case sensitive.

Example :

I have create field extId__c in Account object as External field "Text(50) (External ID) (Unique Case Insensitive)".

String Product='p1';
String Sales_Unit='s1';
String CurrencyVal='c1';
String Province='p1';
String extId=Product + '_' + Sales_Unit + '_' + CurrencyVal +'_'+ Province;
    Account acc=new account();
    insert acc; // succesfully execute only if no Same extId is present in org
    system.debug('acc' + acc); 
catch(exception ex){    
    // perform update operation only if sample dublicate record is present 
    if(ex.getDmltype(0) == StatusCode.DUPLICATE_VALUE)  { 
    Account a=[select id,name,phone from Account  where extId__c =:extId limit 1];    
    upsert a; 

When you run above code in anonymous window.

  • First time execute it will not throw as exception
  • Second time execute the same code , it will throw exception of duplicate external key Id, and update phone number of the account.

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.