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
hemant ranahemant rana 

creating record through trigger and then redirecting to that records edit page

I have a requirement in which when a record is created manually say on an object "customObject__c" a record is also cretaed on the "opportunity" object . I have done this by trigger. Now they want to create and also redirect the page to the newly created opportunity record edit page?? is it possible to add some code in trigger as i thought to use page reference but that dosenot seems to work. please help... 

This is my trigger---
trigger TriggerOnEngagementForCreatingNewOportunity on Engagement__c (after insert) {
    set<id> engIds=new set<id>();
    set<string> engIdscontact=new set<string>();
    string urls;
    map<id,id> contactAccount=new map<id,id>();
    map<id,id> engagementcontact=new map<id,id>();
    map<id,id> engagementAccount=new map<id,id>();
    List<Opportunity> comms = new List<Opportunity>();
    list<engagement__c> ab=[select Contact_Name__r.Account.Id,Contact_Name__r.Name from engagement__c where Id=:trigger.new];
    for(engagement__c abc:ab)
    {
      engIds.add(abc.Contact_Name__r.Account.Id) ;
        engIdscontact.add(abc.Contact_Name__r.Name);
    }
     
    for(Engagement__c a : trigger.new)
    {
      
       Opportunity comm = new Opportunity ();
        for(Id ii:engIds)
        {
        comm.AccountId=ii;
        }
        for(String ss: engIdscontact)
        {
        comm.Name=ss;
        }
        comm.Contact__c=a.Contact_Name__c;
        comm.CloseDate=system.today();
       
        comm.StageName='Q4 2015';
        comm.Engagement__c=a.Id;
       comms.add(comm);     
    }
    insert comms;
    for(Opportunity opppp: comms)
    urls='https://cs8.salesforce.com/'+opppp.id+'/e?retURL=%2F'+opppp.id;
    public PageReference redirec()
    {
    system.debug('inside page reference');
    PageReference pageAccount = new PageReference('https://cs8.salesforce.com/006L0000005KSS2/e?retURL=%2F006L0000005KSS2');
    pageAccount.setRedirect(true);
    return pageAccount;
    }
}
naveen rahulnaveen rahul

@hemant rana  

you could have done redirecting,when the triger  complete. i would do like this.

trigger createinsert on Bank_names__c (after insert) {

Bank_names__c [] stringname =Trigger.new;

  /*
     here your Opportunity codes
*/

 //call a static class method in that write up redirect page. 
  bankclasses.triggerAfterInsert(stringname);


}



thanks

hemant ranahemant rana
hi thanks for the rep...
 are u saying to do lyk this---
trigger TriggerOnEngagementForCreatingNewOportunity on Engagement__c (after insert) {
{
//code for opp;
}
insert record;
public static PageReference redirec()
    {
    system.debug('inside page reference');
    PageReference pageAccount = new PageReference('https://cs8.salesforce.com/006L0000005KSS2/e?retURL=%2F006L0000005KSS2');
    pageAccount.setRedirect(true);
    return pageAccount;
    }
}
KaranrajKaranraj
Hemant - You can't redirect to page from apex trigger. 

Here is the workaround for your problem
1. First you have to override your custom object 'New' button with the visualforce page.
2. In you custom Visualforce page, using extension overirde the save button and in your extension method create opportunity record and from there you can able to redirect to the opportunity page.

Thanks,
Karan 
hemant ranahemant rana
Hi naveen--- do you want so say lyk this?

trigger TriggerOnEngagementForCreatingNewOportunity on Engagement__c (after insert) {
{
RedirectPageOpp handler = new RedirectPageOpp(Trigger.isExecuting, Trigger.size);
//code for opp;
}
insert record;
handler.triggerAfterInsert(Trigger.new);

CLASS_-----

public with sharing class RedirectPageOpp{

   public static PageReference redirec()
    {
    system.debug('inside page reference');
    PageReference pageAccount = new PageReference('https://cs8.salesforce.com/006L0000005KSS2/e?retURL=%2F006L0000005KSS2');
    pageAccount.setRedirect(true);
    return pageAccount;
    }

}
hemant ranahemant rana
Hi, karan thanks for the help but i don't want to create vf page need to redirect thorugh trigger.
KaranrajKaranraj
@Hemant  -  No you can't redirect to page from trigger code. 
naveen rahulnaveen rahul

@hemant rana

 

Trigger
------------

trigger createinsert on Bank_names__c (after insert) {

Bank_names__c [] stringname =Trigger.new;
   
    bankclasses.triggerAfterInsert(stringname);

}


Class
---------

public with sharing class bankclasses {


  public  static pagereference  triggerAfterInsert(List<Bank_names__c> bankNames){
       Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
       String[] toAddresses = new String[] {'test@test.com'};
       String[] CcAddresses = new String[] {'test@test.com'};
       mail.setToAddresses(toAddresses);
       mail.setCcAddresses(CcAddresses);
       mail.setReplyTo('navinragul@yahoo.com');
       mail.setSenderDisplayName('Salesforce Support');
       mail.setSubject('Trigger plus email functionality ');
       mail.setBccSender(false);
       mail.setUseSignature(false);
       mail.setPlainTextBody('this email is received through trigger,while creating a custom object');
       mail.setHtmlBody('  this email is received through trigger,while creating a custom object <br/>'+
                        '<h1>If you receive this email,please send me back<h1>');
       // Send the email you have created.
       Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
       
        system.debug('mail function here');
         Pagereference pg;
         pg = new Pagereference('/apex/listbank_names');
         pg.setredirect(true);
         return pg;
             
    }

}


work for me pretty well and test

naveen rahulnaveen rahul

@hemant rana 

did it solved , if yes please marked closed.

 

hemant ranahemant rana
hi Naveen its not showing any error but it also not redirecting the page don't know why....
its calling the class also the method of page redirect but not redirecting it...
naveen rahulnaveen rahul
if you like to get solved skype me naveen_rahul.
hemant ranahemant rana
Hi, skype is not accessible in my office can you please see the code here----

TRIGGER___________________--------------

trigger TriggerOnEngagementForCreatingNewOportunity on Engagement__c (after insert) {
    //Engagement__c[] stringname=trigger.new ;
    string stringname;
    system.debug('stringname'+stringname);
    set<id> engIds=new set<id>();
    set<string> engIdscontact=new set<string>();
    string urls;
    map<id,id> contactAccount=new map<id,id>();
    map<id,id> engagementcontact=new map<id,id>();
    map<id,id> engagementAccount=new map<id,id>();
    List<Opportunity> comms = new List<Opportunity>();
    list<engagement__c> ab=[select Contact_Name__r.Account.Id,Contact_Name__r.Name from engagement__c where Id=:trigger.new];
    for(engagement__c abc:ab)
    {
  engIds.add(abc.Contact_Name__r.Account.Id) ;
        engIdscontact.add(abc.Contact_Name__r.Name);
    }
    for(Engagement__c a : trigger.new)
    {
  Opportunity comm = new Opportunity ();
        for(Id ii:engIds)
        {
   comm.AccountId=ii;
        }
        for(String ss: engIdscontact)
        {
   comm.Name=ss;
        }
        comm.Contact__c=a.Contact_Name__c;
        comm.CloseDate=system.today();
        comm.StageName='Q4 2015';
        comm.Engagement__c=a.Id;
  comms.add(comm);     
    }
    insert comms;
    for(Opportunity opppp: comms)
    {
  stringname='https://cs8.salesforce.com/'+opppp.id+'/e?retURL=%2F'+opppp.id;
    }
  system.debug('stringname'+stringname);
  bankclasses.triggerAfterInsert(stringname);
}

CLASS_----------------------

public with sharing class bankclasses
{
public  static pagereference  triggerAfterInsert(string bankNames)
{
  system.debug('hiiii==>'+bankNames);
  PageReference pageAccount = new PageReference(bankNames);
  system.debug('pageAccount====>'+pageAccount);
  pageAccount.setRedirect(true);
  system.debug('===>'+pageAccount);
  return pageAccount;
}
}
KaranrajKaranraj
Guys – Trust me you can’t redirect to page from apex trigger code. Don’t waste your time on investing which is not at all possible in salesforce. Try with the approach which I shared before. Best of Luck! Thanks, Karan
hemant ranahemant rana
I think karan is right thanks Karan.... and also Naveen for the help....
naveen rahulnaveen rahul

did you solved it hemant,i got em working, how ?? the codes above i posted really worked for me.how its possible then??.

Need closer look really.