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

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;
}
}
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;
}
}
@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
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;
}
}
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
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 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
@hemant rana
did it solved , if yes please marked closed.
its calling the class also the method of page redirect but not redirecting it...
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;
}
}
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.