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
Craig GroveCraig Grove 

link id of new object record to the record which triggered the creation though they are not parent/child

Hello,

     I've created the below trigger so that our Telemarketing (Inside Sales) initiatives will open an Opportunity if the Inside Sales record's outcome__c field = "Meeting Scheduled". I have a lookup field for each object linking them. I have code to pass the originating record's id to the lookup field of the created record, but I need to get the new record's id sent back to the lookup field on the originating record. The lookup field on the originating record is Opportunity__c. Any assistance is appreciated.

trigger OpportunityCreate on Inside_Sales__c (after Update) {

//The purpose of this trigger is to automatically create an open opportunity when a telemarketer
//has successfully scheduled a meeting or presentation in reference to an inside sales initiative
//This trigger kicks off when an inside sales initiative has an outcome of meeting scheduled or present. scheduled

    //Here we are creating a list of Opportunity records
    //We create a lists or batches so that we can avoid exceeding governor limits
    List<Opportunity> Opps = new List<Opportunity>();
   
    //Here we are specifying to run the trigger only if the if statemenet is met
    //We have multiple if statements
    for (Inside_Sales__c updatedInsideSales : Trigger.New){ 
    
         //Here we are stating to run the trigger if outcome equals meeting scheduled
        if(updatedInsideSales.CreateOpp__c == True){
        
            //If the above if statement was met the below parameters will be passed
            Opps.add(new Opportunity(
                AccountId = updatedInsideSales.Account_Name__c,
                OwnerID = updatedInsideSales.Sales_Manager__c,
                InsideSales__c = updatedInsideSales.Id,
                Telemarketing__c = 'Yes',
                Name = updatedInsideSales.Account_Name_Text__c,
                Owner_s_Region__c = updatedInsideSales.PDX_Region__c,
                StageName = 'Presentation',       
                CloseDate = date.today()+60));
            }
        }

    //Here we are inserting the opportunity records
    insert Opps;
   
}
Purnima JothikrishnanPurnima Jothikrishnan
after inserting the opp's , loop the list and create a map of insidesales id and opp id. Use this map to populate the opp id's in the inside sales object, something like this ,


Map<Id,Id> insideSalesANDOppMap = new Map<Id,Id>();
for(Opportunity o : Opps){
    insideSalesANDOppMap.put(o.InsideSales__c,o.Id);
}

List<InsideSales__c> toUpdate = new List<InsideSales__c>();
for(Id i :insideSalesANDOppMap.keySet()){
    toUpdate.add(new InsideSales__c(Id=i,Opportunity__c=insideSalesANDOppMap.get(i)));
}
update toUpdate;


This update would cause recursion of the trigger so please prevent that from happening. You can refer the below link for that ,
https://help.salesforce.com/apex/HTViewSolution?id=000133752&language=en_US
 
Craig GroveCraig Grove
Thank you Prunima. I have attempted to implement your suggested solution, but I'm not having much luck. I'm pretty new to apex. Here's how I've updated the trigger, but none of the trigger executes when adding the checkrecursive method. Any suggestions are appreciated.

trigger OpportunityCreate on Inside_Sales__c (after Update) {
    if(checkRecursive.runOnce()){

//The purpose of this trigger is to automatically create an open opportunity when a telemarketer
//has successfully scheduled a meeting or presentation in reference to an inside sales initiative
//This trigger kicks off when an inside sales initiative has an outcome of meeting scheduled

        //Here we are creating a list of Opportunity records
        //We create a lists or batches so that we can avoid exceeding governor limits
        List<Opportunity> Opps = new List<Opportunity>();
   
        //Here we are specifying to run the trigger only if the if statemenet is met
        //We have multiple if statements
        for (Inside_Sales__c updatedInsideSales : Trigger.New){ 
    
             //Here we are stating to run the trigger if outcome equals meeting scheduled
            if(updatedInsideSales.CreateOpp__c == True){
        
                //If the above if statement was met the below parameters will be passed
                Opps.add(new Opportunity(
                    AccountId = updatedInsideSales.Account_Name__c,
                    OwnerID = updatedInsideSales.Sales_Manager__c,
                    InsideSales__c = updatedInsideSales.Id,
                    Telemarketing__c = 'Yes',
                    Name = updatedInsideSales.Account_Name_Text__c,
                    Owner_s_Region__c = updatedInsideSales.PDX_Region__c,
                    StageName = 'Presentation',       
                    CloseDate = date.today()+60));
                }
            }

        //Here we are inserting the opportunity records
        insert Opps;
        
        Map<Id,Id> insideSalesANDOppMap = new Map<Id,Id>();
        
        for(Opportunity o : Opps){
            insideSalesANDOppMap.put(o.InsideSales__c,o.Id);
        }

        List<Inside_Sales__c> toUpdate = new List<Inside_Sales__c>();
        
        for(Id i :insideSalesANDOppMap.keySet()){
            toUpdate.add(new Inside_Sales__c(Id=i,Opportunity__c=insideSalesANDOppMap.get(i)));
        }

        update toUpdate;

    }
       
}