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
satyamsatyam 

Please help me to create a custom clone button

Hi,

 

I want to create a clone button which will clone the record in edit mode and user want can change some value and save the record.

 

But i have status field in cloned record which should be new and user can submit for approval after the status will be approved then i need to change the status of the record from which we are cloning this record to change to completed automatically.

 

Means based on cloned record status, i need to change the status of the rcord from which we r cloning new record and it should be automatically.how i can achieve this.

 

Thanks in advance!!!!!!

 

Thanks

 

Best Answer chosen by Admin (Salesforce Developers) 
SurekaSureka

Hi,

 

Try the following code:

 

 trigger suman on Job_Application__c (after update) { 
List<Job_Application__c> jbApp = new List<Job_Application__c>();
 for(Job_Application__c stud:Trigger.new){
 if(stud.Status__c=='Extend an offer'){
  Job_Application__c st = [Select id,Status__c,Master_Id__c from Job_Application__c  where id=:stud.Master_Id__c];

if(st.Status__c!='Rejected')

{
  st.Status__c = 'Rejected';

jbApp.add(st);

}
if(jbApp.size()>0)

update jbApp;
  }
 }
 }

 

Thanks

All Answers

SurekaSureka

Hi,

 

Create a Custom "Clone" button and do a URL Override. Sample URL below:

 

/{!Contact.Id}/e?clone=1&retURL=%2F{!Contact.Id}&00N80000004fIVg=Sample

 

Add the Custom Clone button to the page layout.

 

Where 00N80000004fIVg is the Id of field that needs to be prepopulated. In your case, this would be Id of Status field.

              Sample is the value to be pre-populated in the field.

 

 

Let me know if this helps.

 

Thanks

Bhuvana

satyamsatyam

I dont want to change the status at the time of cloning the record i want to change the status of tha record(From which we r cloning)  based on cloned record status means if the cloned record status is approved then i want to automatically change the status of the record(from which we r cloning) to Completed.

SurekaSureka

Hi,

 

Then in that case, override the Clone Button with the VF page. The VF page should have nothing but the 'action' in the Page tab. In the 'action' method, check if the record to be clone has a particular value, accordingly pre-poulate and redirect.

 

Thanks

satyamsatyam

Hi,

 

Actually there are 2 cases

 

1.Record should be clone

 

2. After that clone record should go for approval and once the cloned record status is approved then at that time automatically the record from which we r cloning status should change to complete.

 

Thanks

 

SurekaSureka

Hi,

 

Follow the below steps:

 

1. Using the above steps, clone the record and redirect to the record clone page

2. Create an approval process as per your requirement.

3. On click of save button, write a trigger "afterInsert" and invoke the approval process from the Trigger/Apex class which is invoked from Trigger.

4. In your case,  as you need to change the Status of record from which you are cloning, you need to have a field to identify from which record it has been cloned. So have a hidden field that stores the ID of the record from which you are cloning.

5. Once the cloned record is approved, have a "AfterUpdate" trigger, that checks if the record status is "Approved".

If so, using the hidden field, it takes the ID of the master record(from which u r cloning) and updates the master record status accordingly.

 

Will this work?

 

Thanks

 

satyamsatyam

Hi,

 

But how we can capture the Id of a record from which we are cloning to the cloned record is there any way to catch the id without making any relationship .

 

And This trigger will automatically invoke the approval process while saving the record i want that approval process should be started by user after pressing subit for approval button.

 

Then the status and all should change.

 

Thanks!!!!

SurekaSureka

Hi,

 

Workaround to capture the cloned record Id, is to create a field and prepopulate the value for this field with the Master Record Id through the URL. So the record will automatically have the Master Record Id.

 

Thanks

 

satyamsatyam

Hi,

 

I am writing below trigger to update the status of master record but its not working what change i need to do.

 

  trigger suman on Job_Application__c (after update) {
 

 for(Job_Application__c stud:Trigger.new){
 if(stud.Status__c=='Extend an offer'){
  Job_Application__c st = [Select id,Status__c,Master_Id__c from Job_Application__c  where id=:stud.Master_Id__c];
  st.Status__c = 'Rejected';
  update st;
  }
 }
 
 }

 

Here Master_Id__c is the field in child record where i am saving the id of master record .Now how i need to use the trigger.

 

Please tell me what change i need to do in this trigger.

 

Thanks and appreciate for your Help!!!!!

SurekaSureka

Hi,

 

Try the following code:

 

 trigger suman on Job_Application__c (after update) { 
List<Job_Application__c> jbApp = new List<Job_Application__c>();
 for(Job_Application__c stud:Trigger.new){
 if(stud.Status__c=='Extend an offer'){
  Job_Application__c st = [Select id,Status__c,Master_Id__c from Job_Application__c  where id=:stud.Master_Id__c];

if(st.Status__c!='Rejected')

{
  st.Status__c = 'Rejected';

jbApp.add(st);

}
if(jbApp.size()>0)

update jbApp;
  }
 }
 }

 

Thanks

This was selected as the best answer
satyamsatyam

Hi,

 

Its showing following exception

 

Apex trigger suman caused an unexpected exception, contact your administrator: suman: execution of AfterUpdate caused by: System.QueryException: List has no rows for assignment to SObject: Trigger.suman: line 5, column 1

 

what is the problem?

 

Thanks!!!!

SurekaSureka

Hi,

 

Do the following check before the query:

 

if(stud.Status__c=='Extend an offer' && stud.Master_Id__c!=null)

{
  Job_Application__c st = [Select id,Status__c,Master_Id__c from Job_Application__c  where id=:stud.Master_Id__c];....

.............................

 

}

satyamsatyam

Hi,

 

Thanks

 

Now its working!!!!

 

Many many thanks for your support.

 

Now i want to right test case for this so what thing i need to remember and how can right can you give me some help.

 

Thanks

Chandrakant

SurekaSureka

Hi,

 

Refer to APEX pdf for the test method format.

 

In your case, follow the below logic:

 

1. Insert a Job Application Record - Eg. JR1

2. Insert another Job Application Record - Eg. JR2 , populating Master_Id__c field with JR1 and Status as "Extend an offer"

 

Test method is done.

 

Thanks

satyamsatyam

Hi,

 

Actually please help me to right the test class because i am new to right test class for trigger so please help me where i am doing wrong

 

@isTest
private class suman{
    
    public static testmethod void TestcheckJAstatus(){
    test.startTest();
         Job_Application__c skill=new Job_Application__c(Master_Id__c='00NU0000002iJqM',Status__c='00NU0000000g0AS');
          insert skill;
         Job_Application__c skill2=new Job_Application__c(Master_Id__c='00NU0000002iJqM',Status__c='00NU0000000g0AS');
          
          if(skill.Master_Id__c==skill2.Master_Id__c && skill2.Master_Id__c=='Rejected'){
              System.assert(true);
           
          }else{
          
          insert skill2;
          }
          
              test.stopTest();        
    }
}

 

Please correct my test class to resolve the problem

 

Thanks

:)))

 

satyamsatyam

Hi,

 

I have written the following test class but not getting where is the problem

 

My Apex trigger is

 

trigger suman on Job_Application__c (after update) {
List<Job_Application__c> jbApp = new List<Job_Application__c>();
 for(Job_Application__c stud:Trigger.new){
 if(stud.Status__c=='Rejected'&& stud.Master_Id__c!=null){
  Job_Application__c st = [Select id,Status__c,Master_Id__c from Job_Application__c  where id=:stud.Master_Id__c];
if(st.Status__c!='Extend an Offer')
{
  st.Status__c = 'Extend an Offer';
jbApp.add(st);
}
if(jbApp.size()>0)
update jbApp;
  }
 }
 }

 

And my test class which i written but not working

 

@isTest
private class Suman
{
    static testMethod void testCase1()
    {    
         Test.startTest();
        Job_Application__c adobj1 = Job_Application__c();
        adobj1.Name = 'JA-00025';
        adobj1.Job_Name__c='Engine';
        adobj1.Master_Id__c='a02U0000004GnZP';
        adobj1.Status__c='Rejected';
        insert adobj1;
         Test.startTest();
        Job_Application__c adobj2 = Job_Application__c();
        adobj2.Name = 'JA-00025';
        adobj2.Job_Name__c='Engine';
        adobj2.Master_Id__c='a02U0000004GnZP';
        adobj2.Status__c='Extend an Offer';
        insert adobj2;
        }
   
    
}

 

Please help me to resolve the problem where i am doing mistake.

 

Thanks:))