Problem
Over the years as Force.com developer, I found that migration and trigger are two area where I encountered issues most frequently, and here is another interesting issue with migration in Salesforce. I had a custom sObject that were in Master-Detail relationship with Account. When I tried to migrate this object using Force.com Migration Tool, I was getting following error:
Work-around
With the cause of the problem tracked down, I still found the prospect of manually enabling history tracking on the object and each field in the destination org quite troubling. With Salesforce.com migration, some manual steps are unavoidable (e.g. Community setup, Quote enablement, etc.), but you do want to minimize the manual steps as much as possible, and I thought there might be a way to automate this step using Force.com Migration Tool.
First solution I considered was creating the object first with a lookup relationship with Account, then have another deployment of the same object with relationship change to Master-Detail relationship. This did eliminate the "duplicate value..." error, but the problem was that when you change a relationship from Lookup to Master-detail (or vice-versa), it triggers sharing rule recalculation, which causes problem if you had any sharing rule being migrated at the later stage of the deployment step, which we did have for this project.
The eventual solution I came up with was a three-staged migration of the object. First, I migrated a shell Metadata of the child object with Master-Detail Relationship on, but with history tracking turned off. Second, I migrated the shell Metadata file again, but with history tracking turned on. Finally, I migrated the full retrieval of the child object Metadata. One issue you have is that you have to manually maintain copies of shell Metadata files for these children objects, but schema for the shell is quite simple and you only have to create these shell files only once, whereas you have to perform manual step, which is prone to error, on every environment you migrate to.
I have checked in the code for the simplified version of this process in my github repository. You can find it at https://github.com/jpaek/MasterDetailWithHistory.
Takeaway
The error message for this problem was rather misguiding for me, but this shouldn't come as a surprise to us Force.com developers, especially when working with migration. Googling, as was case here, can help figure out what the problem is, but sometime you do have to manually figure out the problem by playing around with it, which can be quite cumbersome tasks.
Also, I want to point out that, with bit of thinking and experimentation, you can eliminate many of the manual steps involved with migration steps through Force.com Migration Tool.
Over the years as Force.com developer, I found that migration and trigger are two area where I encountered issues most frequently, and here is another interesting issue with migration in Salesforce. I had a custom sObject that were in Master-Detail relationship with Account. When I tried to migrate this object using Force.com Migration Tool, I was getting following error:
duplicate value found <unknown> duplicates value on record with id: <unknown>After much googling, I found this post on StackExchange, indicating that History Tracking might be a problem. When I disabled the history tracking on the object, I was able to migrate the object.
Work-around
With the cause of the problem tracked down, I still found the prospect of manually enabling history tracking on the object and each field in the destination org quite troubling. With Salesforce.com migration, some manual steps are unavoidable (e.g. Community setup, Quote enablement, etc.), but you do want to minimize the manual steps as much as possible, and I thought there might be a way to automate this step using Force.com Migration Tool.
First solution I considered was creating the object first with a lookup relationship with Account, then have another deployment of the same object with relationship change to Master-Detail relationship. This did eliminate the "duplicate value..." error, but the problem was that when you change a relationship from Lookup to Master-detail (or vice-versa), it triggers sharing rule recalculation, which causes problem if you had any sharing rule being migrated at the later stage of the deployment step, which we did have for this project.
The eventual solution I came up with was a three-staged migration of the object. First, I migrated a shell Metadata of the child object with Master-Detail Relationship on, but with history tracking turned off. Second, I migrated the shell Metadata file again, but with history tracking turned on. Finally, I migrated the full retrieval of the child object Metadata. One issue you have is that you have to manually maintain copies of shell Metadata files for these children objects, but schema for the shell is quite simple and you only have to create these shell files only once, whereas you have to perform manual step, which is prone to error, on every environment you migrate to.
I have checked in the code for the simplified version of this process in my github repository. You can find it at https://github.com/jpaek/MasterDetailWithHistory.
Takeaway
The error message for this problem was rather misguiding for me, but this shouldn't come as a surprise to us Force.com developers, especially when working with migration. Googling, as was case here, can help figure out what the problem is, but sometime you do have to manually figure out the problem by playing around with it, which can be quite cumbersome tasks.
Also, I want to point out that, with bit of thinking and experimentation, you can eliminate many of the manual steps involved with migration steps through Force.com Migration Tool.