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
ManoharSFManoharSF 

Salesforce to Salesforce & Trigger

Hi there,

 

Background  - I have S2S setup to transfer external org records into ours. I am trying to pull detail object record in Master Detail relationship, since it doesnt accept the Id of the master I am getting that in a text field. but before inserting I have 'before insert' trigger that I am trying to use to grab the Master object record Id, and stamp it on the master Id field. but looks like my trigger is not firing , its complaining on the master field saying "Error: You must enter a value". I can understand that Master field is required on Detail object for Master Detail Relationship. wouldnt expect it would fire before the trigger.

 

Am I missing anything or any other way I can achive it?

 

trigger createS2SLeadSolarArray on String_Array__c (before insert) {
  
  for (String_Array__c sa: Trigger.new) {
// I tried commenting out the If statement as well if (sa.Source__c == 'S2S'){
//query for master record Solar_System__c ssg = [SELECT Lead__c, Id FROM Solar_System__c WHERE Source_Solar__c = :sa.Solar_System_Text__c limit 1]; sa.Solar_System__c = ssg.Id;
// tried hardcoding the id value // sa.Solar_System__c = 'a1oK0000000bNi9IAE'; } } }

 

Thanks

Manohar

 

 

 

souvik9086souvik9086

This way it can't happen. If you use before insert then that text field containing the master field id is yet blank, it cannot be fetched. And if you use after insert then it will never insert as well give error "Required Field Missing".

 

What you can do is assign a default valid(present in the target org) master id in that field in before insert trigger.

 

Then after insertion you have to execute a batch apex class where there will be mass update based on that text field id of the master, assigning the correct look ups.

 

If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.

Thanks

ManoharSFManoharSF
This is a master detail field, doesnt gives you option to default.
Alex.AcostaAlex.Acosta

What I suggest is to export both your table as a CSV file from your old org.... insert your parent only into the new org and export those so you have the new Id.

 

Once this is complete open up excel and use formla fields to determine which is the new IDs for the child... keep in mind when you upload your parent records into the new org, keep your old salesforce id field in the file though you do not use it as a reference point when making your formula fields in excel...

 

Master Detail relationships will always require a value for the master lookup field from the child no matter what.

souvik9086souvik9086

Yes, I mean you give a valid master id value in all records. It needs a value, thats all. Then after that you can assign the correct lookups through batch class update.

 

Thanks

souvik9086souvik9086

Another thing if you do not do this in S2S. You can do this in Dataloader, then in the csv file you can use Vlookup to assign this.

 

If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.

Thanks