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
SapamrmSapamrm 

Automatically create new record upon save

I originally posted the message below on the general salesforce question board, but there they told me to come here. So here I am! :-)

 

 

 

I would like Salesforce to automatically create (and save) a new record (for object B) as soon as I manually save a newly created record for object A that meets a certain condition (e.g. field A is not empty). With that I would like to prepopulate some object B fields with object A data.
(Right now I have a created a custom button on my object A record which does all of this, but pushing this button manually each time is not the ideal solution, so I would like this process to automatically take place in the background).

Is this feasible? If yes, how? :-)

Probably the answer will be "Yes, with Apex!", because always when I get stuck the problem can be solved using Apex or Visualforce.. Tbh, this is really frustrating since I'm just a simple administrator and not a developer.

So any help (e.g. some sample code, test class) would be more than appreciated!

BritishBoyinDCBritishBoyinDC

Do the fields on Object B need to be populated with data from A? Or can they just be lookup formula fields?

 

How is B related to A? Master-Detail, or lookup?

SapamrmSapamrm

Objects A and B are not related to each other in any way.

 

Right now, the B-record is manually created by pushing a button on the A-record detail page.

Pre-populating the B-record with A-record ("Beursinput") data is done in the custom button code (see below)

 

http://na1.salesforce.com/a0M/e?CF00N30000005srKz={!Beursinput__c.Bedrijf__c}&CF00N30000005srKz_lkid={!Beursinput__c.BedrijfId__c}&retURL=%2F{!Beursinput__c.BedrijfId__c}&00N30000005srLd="Fair"&00N30000005srLi="Batibouw 2012"&00N30000005sxYE={!Beursinput__c.Datum__c}&00N30000005vOfs="Brussel"&00N30000005srLx="Subscribed"&00N30000005srLs={!Beursinput__c.Opmerkingen__c}

 

BritishBoyinDCBritishBoyinDC

Okay - without having access to your instance, I can't put the actual objects/field names in, but here's an example where any Opportunity (Object A) where type = 'Existing Customer - Upgrade', we'll create a new Contact (Object B) where Lastname = Opportunity Name, and Contact Title = Opportunity StageName.

 

So obviously not a real life example, but I think you can easily adapt it to do what you want...note that unlike with buttons, the fields names for both source and target have to be the API field names...

 

Best practice for triggers is to write a simple trigger, and then pass off handling to class...so first we have the class, and that class also contains a test script...

 

public with sharing class ManageOpportunities {

public static void afterInsert(Opportunity [] optys) {
//Put all new records in a list so we only execute one DML statement
List<Contact> newcons = new List<Contact> ();
 
//Loop through records passed in by trigger
   for (Opportunity o: optys) {
     //only create for correct type
         if (o.type.equals('Existing Customer - Upgrade')) {
//we can create a contact based on Opty data and add directly to a list
             newcons.add(new Contact (LastName = o.Name, Title = o.StageName, Languages__c = o.Account_Number__c));
         }
    
    }
    //once loop is finished, check if any new records were requried
    if (newcons.size() > 0) {
//if so create       
 try {
        insert newcons;
        }
    //error handling depends on your use case...but as a simple example, this won't stop Opportunities being created
    //and debug log will say why insert of contacts failed
       catch (Exception Ex) {
        system.debug (ex);
        }
    }
    
} //end after insert

static testmethod void  testafterinsert() {
//Create new Opportunity that WILL create a new contact
Opportunity testo = new Opportunity (Name = 'testconcreated', CloseDate = system.today(), StageName = 'Prospecting', Type = 'Existing Customer - Upgrade');
insert testo;
//Check new contact created and data updated correctly
Contact testcon = [Select Id, LastName, Title from Contact where LastName = :testo.name];
system.assertequals(testo.StageName, testcon.Title);

//Create Opportunity that WON'T
Opportunity testonocon = new Opportunity (Name = 'testconnotcreated', CloseDate = system.today(), StageName = 'Prospecting', Type = 'Existing Customer - New');
insert testonocon ;
//Check no new Contact created...
system.assertequals(0, [Select count() from Contact where LastName = :testonocon.name]);


}

} //end class

 

 

 

And then the trigger to call that class

 

trigger ManageOpportunities on Opportunity (before insert, after insert) { 

    if(Trigger.isInsert && Trigger.isAfter){
        ManageOpportunities.afterInsert(Trigger.New); 
    }  
} //end trigger

 

That should get you started...

SapamrmSapamrm

Hi Peter,

 

I really appreciate you trying to help me! The code below may be obvious to you and other developers, but it's like chinese to me, lol!

I have absolutely no experience with apex triggers, so I have no idea where to begin.

 

I always have to pass when the answer is "Apex" or 'Visualforce". I kinda expected this, nevertheless it's again frustrating not being able to implement a possible improvement to our Salesforce environment!

 

Anyway, thanks for your feedback!

 

Regards,

 

Martijn