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
Will Crowley 6Will Crowley 6 

Return ID from record created after insert

I'm having trouble getting my head around returning the ID of a newly created record & inserting it into a field on the original object.  I'm trying to automatically create a campaign from a custom object (Listing__c) (which is working) but I've tried various ways of inserting the campaign ID into the Campaign__c field on the Listing and it's just not working.  

Can someone help me out?

Current Trigger:
trigger CreateCampaign on Listing__c (after insert)
 {

// Create Associated Campaign when Listing is Created
 
    List<Campaign> CampaignInsert=new List<Campaign>();
    for(Listing__c L: trigger.new)
 


 CampaignInsert.add(new Campaign(
                                  Name=L.Deal_Name__c,
                                  Type='Email - Property Listing',
                                  IsActive=True,
                                  Status='New'                                                                                                     
                                                    ));
 }   
    try{
    if(CampaignInsert != Null){
          insert CampaignInsert;          
    }
    }Catch(Exception e){
        System.debug('Exception ****'+e.getMessage());
        System.debug(Campaign.id);
     }
}
anuj huriaanuj huria

Hi Will Crowley 6,
u can get the record id fafter insert like this

  ID listing_id; //declare variable to store id of your custom object.
for(Listing__c L: trigger.new)
 

listing_id=L.id; //the id of your newly created record get stored in listing_id.
 CampaignInsert.add(new Campaign(
                                  Name=L.Deal_Name__c,
                                  Type='Email - Property Listing',
                                  IsActive=True,
                                  Status='New'                                                                                                     
                                                    ));
 }   

thanks 

if you need any help..please let me know

if this helps you mark this as solver 

Will Crowley 6Will Crowley 6
Appreciate the response but I still need to associate the newly created campaign & the listing together.  After the Campaign is created I need to insert the Campaign ID into the Campaign__c field on the Listing object.

I tried changing this line to insert the Listing ID into the correct field but it's throwing an exception.

Before:
listing_id=L.id; //the id of your newly created record get stored in listing_id.
After:
L.Campaign__c=Listing_id;

Error:
Apex trigger CreateCampaign caused an unexpected exception, contact your administrator: CreateCampaign: execution of AfterInsert caused by: System.FinalException: Record is read-only: Trigger.CreateCampaign: line 12, column 1
anuj huriaanuj huria

Hi Will
For this check your sharing setting of Campaign object ,i think the permission for Campaign in Read only

 

Thanks