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
newbie2010newbie2010 

Create object instance from webform

I am not sure if that is the correct terminology, but here it goes...I created two custom objects in SF.  One is populated from a webform and the other is created from the one populated by the webform.  I am at a point where i am not sure what is the best process to accomplish this procedure.  I need to take into consideration the possibility that multiple webforms will be filled out at one time so the Trigger needs to be able to handle multiple items on an ongoing basis.  I am having a tough time deciding how to set this up...all the examples/suggestions/solutions are based instances of objects that are already created and are just updating fields within a certain instance based on a set of criteria.  I need to know how to create a new instance of one object based on the instance of another object once it is inserted from the webform.

 

Any help is greatly appreciated.

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox

You've got some random quotes in there that shouldn't be there. I'd recommend that you read up on the Map object. What's actually happening here is you're creating a Map that looks like this:

 

"James Brown" => "003xxxxxxx1",

"Jane Doe" => "003xxxxxxx2",

 

And then trying to recall those values, you're trying to get "TWF.Agent_Name__c", which, unless you had a contact with no first name and a last name of "TWF.Agent_Name__c", would never return a value (it will be null). Thus, you're triggering the "required fields missing" error.

 

Review my example of how to set up the Maps correctly, and then to recall the values, use the variable name, not surrounded by quotes:

 

 

AgentMap.get(TWF.Agent_Name__c);

 

 

All Answers

sfdcfoxsfdcfox

It's roughly the same as updating, but with a short twist. It looks like this:

 

 

trigger createSecondaryObject on PrimaryObject (after insert) {
  List<SecondaryObject> recordsToInsert = new List<SecondaryObject>();
  for(PrimaryObject po:Trigger.new)
    recordsToInsert.add(new SecondaryObject(Name=po.Name,PrimaryObject__c=po.id /* and more fields here */));
  insert recordsToInsert;
}

PrimaryObject is the parent object (the one directly populated from the web form), SecondaryObject is the child object (the object to be created as a result of the creation of the primary object). PrimaryObject__c is a Lookup or Master-Detail from SecondaryObject to PrimaryObject.

 

newbie2010newbie2010

Thanks for the reply.  What I am doing is a little more complicated as there are two fields that are master-detail to the object that is being created by the webform object.  I have used picklists to control what data is entered for these controlling fields.  One is Contacts of a certain record type and there is a controlled set of contacts with this type.  The other is a custom object that is also controlled and limited.  My guess is that I would need to somehow map the names selected in the picklists to the appropriate id in these objects.  The following code is what I have so far, but it is throwing an exception as it doesn't like the id I am passing from the picklist since it is just a name and not an actual id.

 

trigger TransWebFormInsert on Transmittal_WebForm__c (after insert) {
  if(trigger.isInsert){
     List<Transmittal_NEW__c> tnews = new List<Transmittal_NEW__c>{};
         for(Transmittal_Webform__c TWF : Trigger.new){
            Transmittal_NEW__c tnew = new Transmittal_NEW__c();
            tnew.Agent__c = TWF.Agent_Name__c;
            tnew.Agent_Email__c = TWF.Agent_Confirmation_Email__c;
            tnew.Lead_Source__c = TWF.Lead_Source__c;
            tnew.County_Office__c = TWF.County_Office__c;
            tnew.Contract_Number__c = TWF.Contract_Number__c;
            tnew.Member_Number__c = TWF.Member_Number__c;
            tnew.First_Name__c = TWF.First_Name__c;
            tnew.Street_Address__c = TWF.Street_Address__c;
            tnew.Middle_Initial__c = TWF.Middle_Initial__c;
            tnew.City__c = TWF.City__c;
            tnew.Last_Name__c = TWF.Last_Name__c;
            tnew.Zip_Postal_Code__c = TWF.Zip_Postal_Code__c;
            tnew.Email_Address__c = TWF.Email_Address__c;
            tnew.Phone_Number__c = TWF.Phone_Number__c;
            tnew.Birthdate__c = TWF.Birthdate__c;
            tnew.Type_of_Transaction__c = TWF.Type_of_Transaction__c;
            tnew.Additional_Remarks__c = TWF.Additional_Remarks__c;
            tnew.Requested_Effective_Date__c = TWF.Requested_Effective_Date__c;
            tnew.Deductible__c = TWF.Deductible__c;
            tnew.Product_Type__c = TWF.Product_Type__c;
            tnew.Product_Subtype__c = TWF.Product_Subtype__c;
            insert tnew;
         }
  }
}

 The two fields in the code that are underlined and in bold are the ones that relate to the master-detail records in the object that is created from the webform object.  I would think the best place to handle the map portion of the code is outside of the for loop, would that be correct?

sfdcfoxsfdcfox

 

Map<String,Id> CountyOfficeMap = new Map<String,Id>();
for(Transmittal_Webform__c TWF:Trigger.new) {
  CountyOfficeMap.put(TWF.County_Office__c,null);
}
for(County_Office__c co:[SELECT id,name
                         FROM County_Office__c
                         WHERE Name IN :CountyOfficeMap.keySet()])
  CountyOfficeMap.put(co.Name,co.Id);

Definitely on the right track. Here's roughly what I believe your map would look like. Of course, you need to do this for the second object as well, but hopefully this gets you where you want to be.

 

newbie2010newbie2010

Thanks for the help, sfdcfox!!!

 

I wanted to walk through each line of code explaining what I think it means to make sure I am understanding what's going on, but I am a little confused on some of the functionality.  I am also not sure how to incorporate this into my code and how to pull the id out of the map and "assign" it to the appropriate field on the new record.  Here is my code with the addition of your snippet for both objects.

 

trigger TransWebFormInsert on Transmittal_WebForm__c (after insert) {
  if(trigger.isInsert){
    List<Transmittal_NEW__c> tnews = new List<Transmittal_NEW__c>{};
    Map<String,Id> AgentMap = new Map<String,Id>();
    Map<String,Id> CountyOfficeMap = new Map<String,Id>();
         for(Transmittal_Webform__c TWF : Trigger.new){
            Transmittal_NEW__c tnew = new Transmittal_NEW__c();

            AgentMap.put(TWF.Agent_Name__c,null);
               for(Contact agent:[SELECT id,name FROM Contact
                WHERE Name IN :AgentMap.keySet()])
                 AgentMap.put(agent.Name,agent.Id);

            tnew.Agent__c = 'agent.Id';
            tnew.Agent_Email__c = TWF.Agent_Confirmation_Email__c;
            tnew.Lead_Source__c = TWF.Lead_Source__c;

            CountyOfficeMap.put(TWF.County_Office__c,null);
               for(County_Office__c co:[SELECT id,name FROM County_Office__c
                WHERE Name IN :CountyOfficeMap.keySet()])
                 CountyOfficeMap.put(co.Name,co.Id);

            tnew.County_Office__c = 'co.Id';
            tnew.Contract_Number_SSN__c = TWF.Contract_Number_SSN__c;
            tnew.Member_Number__c = TWF.Member_Number__c;
            tnew.First_Name__c = TWF.First_Name__c;
            tnew.Street_Address__c = TWF.Street_Address__c;
            tnew.Middle_Initial__c = TWF.Middle_Initial__c;
            tnew.City__c = TWF.City__c;
            tnew.Last_Name__c = TWF.Last_Name__c;
            tnew.Zip_Postal_Code__c = TWF.Zip_Postal_Code__c;
            tnew.Email_Address__c = TWF.Email_Address__c;
            tnew.Phone_Number__c = TWF.Phone_Number__c;
            tnew.Birthdate__c = TWF.Birthdate__c;
            tnew.Type_of_Transaction__c = TWF.Type_of_Transaction__c;
            tnew.Additional_Remarks__c = TWF.Additional_Remarks__c;
            tnew.Requested_Effective_Date__c = TWF.Requested_Effective_Date__c;
            tnew.Deductible__c = TWF.Deductible__c;
            tnew.Product_Type__c = TWF.Product_Type__c;
            tnew.Product_Subtype__c = TWF.Product_Subtype__c;
            insert tnew;
         }
  }
}

 

 

 

Again, I am not sure I incorporated it correctly as I get the following error when attempting the process...

 

Apex script unhandled trigger exception

TransWebFormInsert: execution of AfterInsert

caused by: System.StringException: Invalid id: agent.Id

newbie2010newbie2010

By the way, I did get this to work by hard coding the id's into those two fields so I at least know the functionality does work.  I just need to be able to get the id based on the selection in those picklists and then assign it to those fields.

newbie2010newbie2010

Ok, I have changed it to the following, but stil get an execption...

 

REQUIRED_FIELD_MISSING, Required fields are missing: [County Office, Agent]: [County Office, Agent]

 

Here is the code...

            Transmittal_NEW__c tnew = new Transmittal_NEW__c();

            AgentMap.put('TWF.AgentName__c',null);
               for(Contact agent:[SELECT id,name FROM Contact WHERE Name IN :AgentMap.keySet()])
                 AgentMap.put(agent.Name,agent.id);

            tnew.Agent__c = AgentMap.get('TWF.Agent_Name__c');
            tnew.Agent_Email__c = TWF.Agent_Confirmation_Email__c;
            tnew.Lead_Source__c = TWF.Lead_Source__c;

            CountyOfficeMap.put('TWF.County_Office__c',null);
               for(County_Office__c co:[SELECT id,name FROM County_Office__c WHERE Name IN :CountyOfficeMap.keySet()])
                 CountyOfficeMap.put(co.Name,co.id);

            tnew.County_Office__c = CountyOfficeMap.get('TWF.County_Office__c');
            tnew.Contract_Number_SSN__c = TWF.Contract_Number_SSN__c;

 

sfdcfoxsfdcfox

You've got some random quotes in there that shouldn't be there. I'd recommend that you read up on the Map object. What's actually happening here is you're creating a Map that looks like this:

 

"James Brown" => "003xxxxxxx1",

"Jane Doe" => "003xxxxxxx2",

 

And then trying to recall those values, you're trying to get "TWF.Agent_Name__c", which, unless you had a contact with no first name and a last name of "TWF.Agent_Name__c", would never return a value (it will be null). Thus, you're triggering the "required fields missing" error.

 

Review my example of how to set up the Maps correctly, and then to recall the values, use the variable name, not surrounded by quotes:

 

 

AgentMap.get(TWF.Agent_Name__c);

 

 

This was selected as the best answer
newbie2010newbie2010

Funny, that was where I got the quotes from was the map documentation.  Guess I was reading the example wrong.  Anyway, that fixed it and it works great so far.

 

Thanks so much for your help!!