You need to sign in to do that
Don't have an account?
Steve Kucklinca
trigger to create child record on custom object from a parent custom object - PLEASE HELP!
I am relatively new to Apex triggers and classes. I have 2 Custom Objects Merchant Applications__c and MerchOpps__c what I need is to create a new MerchOpps__c record when a field value on Merchant Applications__c is met. As the only other trigger I ever deployed was code I lifted from another admin and I don't have ability to take the Developer course, I am hoping some of you will pity me and help me with this trigger and class. Basically, if field "Envelope Status" = Completed on Merchant Applications__c, I need a MerchOpps__c record created using Account Name for the related Lookup field.
I figure it may be similar to this example I found here. But I still wouldn't be able to determine the Class I would have to write.
I figure it may be similar to this example I found here. But I still wouldn't be able to determine the Class I would have to write.
trigger CreateClass on Opportunity (after insert , before update ) |
02 |
{ |
03 |
if (!myStaticClass.flag) |
04 |
{ |
05 |
List < CS__c > listCS = new List < CS__c > (); |
06 |
|
07 |
for (Opportunity o : trigger. new ) |
08 |
{ |
09 |
if (Trigger.isUpdate) |
10 |
{ |
11 |
if (o.StageName = = 'Closed Won' && o.StageName != trigger.oldMap. get (o.Id).StageName) |
12 |
{ |
13 |
listCS.add( new CS__c (name = 'Addendum ' + o.Name + ' Closed' , Opportunity__c = o.id)); |
14 |
} |
15 |
} |
16 |
if (Trigger.isInsert) |
17 |
{ |
18 |
if (o.StageName = = 'Closed Won' ) |
19 |
{ |
20 |
listCS.add( new CS__c (name = 'Addendum ' + o.Name + ' Closed' , Opportunity__c = o.id)); |
21 |
} |
22 |
} |
23 |
} |
24 |
|
25 |
if (listCS.size() > 0 ) |
26 |
{ |
27 |
insert listCS; |
28 |
} |
29 |
myStaticClass.flag = true ; |
30 |
} |
31 |
} |
As you told that Merchant Applications__c and MerchOpps__c are custom object and thery are related to account object. Once application status changed you want to create a record on merchopps custom object. so we need to write a trigger on applications__c object.
I have wrie code here according to your requirement but you might have to change APIs name.
If this is the solution of your problem. mark it as solution. Thanks.
trigger createMerchOpps on Merchant_Application__c (after insert, after update)
{
LIst<MerchOpps__c> MerchOppsList = new List<MerchOpps__c>();
for(Merchant_Application__c Merchant_Application : Trigger.new)
{
// check status (ApI name check in your system for envelope status)
if(Merchant_Application.Completed__c = 'Completed ')
{
// Please check api name of account on application and merchopps custom object. and also assign values in all required fields in below statment.
MerchOppsList.add(new MerchOpps__c(Account__c = Merchant_Application.Account_Name__c, Name = String.valueOf(Merchant_Application.Account_Name__c)+System.Today()));
}
}
if (MerchOppsList.size() > 0)
{
insert MerchOppsList ;
}
}
2. Name field of MerchOpps__c objcet in text or auto number ?
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
{
LIst<MerchOpps__c> MerchOppsList = new List<MerchOpps__c>();
for(Merchant_Application__c Merchant_Application : Trigger.new)
{
// check status (ApI name check in your system for envelope status)
if(Merchant_Application.Completed__c == 'Completed ')
{
// Please check api name of account on application and merchopps custom object. and also assign values in all required fields in below statment.
MerchOppsList.add(new MerchOpps__c( Account__c = Merchant_Application.Account_Name__c, Name = 'TestOpp'));
}
}
if (MerchOppsList.size() > 0)
{
insert MerchOppsList ;
}
}
I guess here the problem is you are not querying for the values here, as this is an after trigger.
trigger createMerchOpps on Merchant_Application__c (after insert, after update)
{
set<id> setIds = new set<id>();
list<Merchant_Application__c> MerList = new list<Merchant_Application__c>();
LIst<MerchOpps__c> MerchOppsList = new List<MerchOpps__c>();
for(Merchant_Application__c Merchant_Application : Trigger.new){
setIds.add(Merchant_Application.id);
}
// query for the fields you would need later....
MerList = [select id,name,Envelope_Status__c,Account_Name__c from Merchant_Application__c where id in : setIds ];
// now loop through this list ....
for (Merchant_Application__c Mer : Merlist ){
if(Mer.Envelope_Status__c== 'Completed '){
MerchOpps__c MerOpp = new MerchOpps__c();
MerOpp.Account__c = Mer.Account_Name__c;
MerOpp.Name = 'test';
MerchOppsList.add(MerOpp);
}
}
if (!MerchOppsList.IsEmtpy())
{
Database.insert (MerchOppsList,false) ;
}
}
Thanks.
As the custom object is MerchOpps, seeing MerOpps won't that cause errors moving forward once we fix the above?
1. From where you are creating Merchant Application Record. Can you put screen shot of that page.
2. Is the page you are using to create Merchant Application record installed by that application. If yes can you please put screen shot of that page.
3. If that application demo is available freely, please tell me the name of that application.
I just want to give my last try by getting answers of above questions othervise real picture can be understood by login in to the system only.
Thanks,
Shiv