You need to sign in to do that
Don't have an account?

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);
}
}
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);
}
}
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
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
Hi Will
For this check your sharing setting of Campaign object ,i think the permission for Campaign in Read only
Thanks