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
Steve KucklincaSteve 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.

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 }
shiv@SFDCshiv@SFDC
Hi Steve,

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.
trigger createMerchOpps on Application__c (after insert, after update)
{
	LIst<Merchopps__c> merchoppsList = new List<Merchopps__c>();
	for(Application__c application : Trigger.new)
	{
		// check status (ApI name check in your system for envelope status)
    		if(application.Envelope_Status__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 = application.account__c, Name = String.valueOf(application.account__c)+System.Today()));
		}
	}
	if (merchoppsList.size() > 0)
	{
		insert merchoppsList ;
	}

}

If this is the solution of your problem. mark it as solution. Thanks.
Steve KucklincaSteve Kucklinca
Corrected for API values now get this error: Once this is corrected - won't I need a class for coverage?

  Error: Compile Error: Condition expression must be of type Boolean at line 7 column 13

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 ;
    }

}
shiv@SFDCshiv@SFDC
Line 7 corrected
trigger createMerchOpps on Application__c (after insert, after update)
02	{
03	    LIst<Merchopps__c> merchoppsList = new List<Merchopps__c>();
04	    for(Application__c application : Trigger.new)
05	    {
06	        // check status (ApI name check in your system for envelope status)
07	            if(application.Envelope_Status__c == 'Completed ')
08	        {
09	            // Please check api name of account on application and merchopps custom object. and also assign values in all required fields in below statment.
10	            merchoppsList.add(new MerchOpps__c(Account__c = application.account__c, Name = String.valueOf(application.account__c)+System.Today()));
11	        }
12	    }
13	    if (merchoppsList.size() > 0)
14	    {
15	        insert merchoppsList ;
16	    }
17	 
18	}

Steve KucklincaSteve Kucklinca
testing in sandbox; adjusted envelope status to Completed. No MerchOpps record was created. Is this because the Class is missing? Otherwise the trigger saved properly. thnk you
shiv@SFDCshiv@SFDC
1. Application record which is you are inserting or updating having status='completed' ?
2. Name field of MerchOpps__c objcet in text or auto number ?
shiv@SFDCshiv@SFDC
No class don't have any role in this. test class is used for unit testing once trigger works fine.
Steve KucklincaSteve Kucklinca
Text
shiv@SFDCshiv@SFDC
did you activate the trigger ?
Steve KucklincaSteve Kucklinca
yes, it's active
shiv@SFDCshiv@SFDC
trigger createMerchOpps on Application__c (after insert, after update)
02	02  {
03	03      LIst<Merchopps__c> merchoppsList = new List<Merchopps__c>();
04	04      for(Application__c application : Trigger.new)
05	05      {
06	06          // check status (ApI name check in your system for envelope status)
07	07              if(application.Envelope_Status__c == 'Completed ')
08	08          {
09	09              // Please check api name of account on application and merchopps custom object. and also assign values in all required fields in below statment.
10	10              merchoppsList.add(new MerchOpps__c(Account__c = application.account__c, Name = 'TestOpp'));
11	11          }
12	12      }
13	13      if (merchoppsList.size() > 0)
14	14      {
15	15          insert merchoppsList ;
16	16      }
17	17  
18	18  }
can you test it once..
shiv@SFDCshiv@SFDC

trigger createMerchOpps on Application__c (after insert, after update)
  {
      LIst<Merchopps__c> merchoppsList = new List<Merchopps__c>();
      for(Application__c application : Trigger.new)
      {
          // check status (ApI name check in your system for envelope status)
              if(application.Envelope_Status__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 = application.account__c, Name = 'TestOpp'));
          }
      }
      if (merchoppsList.size() > 0)
      {
          insert merchoppsList ;
      }
  
  }
shiv@SFDCshiv@SFDC
are you getting some error while executing or saving record ? Or do you have some other requited field or validation rule on object.
Steve KucklincaSteve Kucklinca
no validation rules are active that would affect the creation. No error while executing/saving record Type Completed in the Envelope Status field and no MerchOpps record is created
shiv@SFDCshiv@SFDC
Stevae i gave one extra space to check completed... I think below code must work fine.
trigger createMerchOpps on Application__c (after insert, after update)
  {
      LIst<Merchopps__c> merchoppsList = new List<Merchopps__c>();
      for(Application__c application : Trigger.new)
      {
          // check status (ApI name check in your system for envelope status)
              if(application.Envelope_Status__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 = application.account__c, Name = 'TestOpp'));
          }
      }
      if (merchoppsList.size() > 0)
      {
          insert merchoppsList ;
      }
  
  }

Steve KucklincaSteve Kucklinca
I have mirrored exactly what you have posted. no new child record is created when I save with Envelope Status (Completed__c) Completed. Does it matter that Account_Name__c in line 10 is pulled from a Master detail lookup type?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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 = 'TestOpp'));
        }
    }
    if (MerchOppsList.size() > 0)
    {
        insert MerchOppsList ;
    }

}
Steve KucklincaSteve Kucklinca
attempted debug got this error: required (...)+ loop did not match anything at input 'trigger'
shiv@SFDCshiv@SFDC
can you add the image of debug log ?
shiv@SFDCshiv@SFDC
screen shot of debug log
Steve KucklincaSteve Kucklinca
User-added image not sure what else to try
Jaseem PookandyJaseem Pookandy
Hey Steve,

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) ;
    }  
    


}
shiv@SFDCshiv@SFDC
Steve I can see in debug log, trigger is not firing. It means eithere trigger is deactivated OR you have a custom visualforce page and custom controller and you are not doing any DML operation(insert, update) in your controller. If this is the case can you share your VF Page & Controller code.

Thanks.
Steve KucklincaSteve Kucklinca
Jaseem with your code I get the following error: Error: Compile Error: Method does not exist or incorrect signature: [LIST<MerchOpps__c>].IsEmtpy() at line 32 column 6

As the custom object is MerchOpps, seeing MerOpps won't that cause errors moving forward once we fix the above?
Steve KucklincaSteve Kucklinca
Shiv, would the VF pages that are live in SFDC because of integrations to external apps and not available in sandbox be the culprit? how would I remove these actions? Especially as they are connected to Docusign and this does not map to Sandbox. Seemingly it will not let me uninstall from the sandbox. Suggested workaround?
shiv@SFDCshiv@SFDC
Steave can you give some brief idea for following query.

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
Steve KucklincaSteve Kucklinca
I have successfully uninstalled DocuSign from the sandbox, thinking the actions there may have been causing the hang up. Unfortunately after this the attempt to run the trigger above still does not cause the creation of the record that I need. Any thoughts Shiv?