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
Sunny GSunny G 

How to deploy a trigger in production? Pls Help....

Hi,

 

I have some very simple trigger in sandbox. Something like to update a particular field value to another value for custom objects.

 

Now, i want to deploy my custom object to production.. but it is giving me an error saying that "Test coverage of selected Apex Trigger is 0%, at least 1% test coverage is required"

 

I am not able to understand why is it coming up??Since my trigger is working fine in sandbox.

 

what to do in that case?? please help.

 

If i need to write an some apex class and all.. then i am not totally aware of how to go about and it and where and what has to be written in that class. Please see the following trigger:

 

trigger updaterequestowner on Pre_Sales_Request__c (before insert)
{
        Profile auth_profile = [select id from profile where name='HS_StandardUser_Presales'];         
        Id v_User = UserInfo.GetProfileId();   
    for(Pre_Sales_Request__c ob : Trigger.new)
        {   
            if(v_User !=  auth_profile.Id)      
                {   
                   ob.Request_Owner__c =  Userinfo.getUserId();  
                }  
        }                
}

can somebody please help in this....

homer671homer671

Yes you probably have to write at least one apex class that includes test code.  Since its a insert method at the minimum you have to do an insert.

 

It will get you past the 1% requirement. 

 

 

@isTest
private class TriggerTests {
			
    public static testmethod void testTrigger() {
       Pre_Sales_Request__c psrc=new Pre_Sales_Request__c( name = 'a' );
       insert psrc;
    }
}

 

You will need to deploy both the trigger and this test class at the same time. 

 

Sunny GSunny G

Hi,

 

Thanks for this.. it has really worked..

 

Please let me know how to write the test class for before update event: 

 

 

trigger previousstage on Pre_Sales_Request__c (before update) 
{
    for(Pre_Sales_Request__c a:Trigger.new)
        {
        if (system.trigger.OldMap.get(a.Id).Opportunity__c == system.trigger.NewMap.get(a.Id).Opportunity__c)

            {
            if(system.trigger.OldMap.get(a.Id).Previous_Stage__c != system.trigger.NewMap.get(a.Id).Previous_Stage__c)
             {
                 a.adderror('you are not allowed to change the previous stage');                           
                  
             }
             }
        }
}

 

 

I shall really be thankful...

homer671homer671

 

 

You can add 

 

 

psrc.name = 'b';

update psrc;

 

To that testTrigger method. 

 

That should do it. 

Sunny GSunny G

Hi,

 

Thanks for this. I used the following code:

 

@isTest
private class TriggerTests1 {
            
    public static testmethod void testTrigger() {
       Pre_Sales_Request__c psrc=new Pre_Sales_Request__c();
       psrc.name = 'b';
       update psrc;    
    }
}

 

 

but.. while running all test.. it is not getting display under "Trigger Code Coverage" section...

 

Am i missing anything??

 

Sunny GSunny G

Hi,

 

I am sorry i had to add that part to an existing class.. my mistake....

 

Now, it is working.. just wanted to know that when i Run All Test.. inside the 

Trigger Code Coverage section.. i have some trigger with less then 75% code coverage.

 

will it really effect the deployment process??

 

Thanks

homer671homer671

Not for triggers.    If you we're deploying  classes then you have to average a 75% for all your classes.  So some classes can be less than 75%. 

homer671homer671

If you we're deploying  classes then you have to average 75% for all your classes, so some classes can be less than 75%. 

Sunny GSunny G

It means..That if we have written any trigger then there is no need to write anything for it further [like Test classes & all] . Only if the trigger is working fine.. then we can deploy that trigger directly to production.

 

Am in correct??

homer671homer671

No you still have to have that 1% so you have to at least try to test it.   I know that 1% is all you need though.  I had a contractor for SFDC write a long trigger code for us and he didn't write a line of test code with it.   I did have one line of test that ran it and that's it.      

 

I guess if he thought it was acceptable.    

 

But technically we should be testing everything.     But having at least 1% for the trigger is the least you can have for triggers. 

Sunny GSunny G

Hi Sir,

 

After doing Run All Test i have the following code coverage for the following trigger.  And based on your valuable efforts i was able to create an apex test class the covers four trigger named: (1) updaterequestowner (2) change_opps (3) initialstage (4) previous stage with the following code coverage. Although all of them are more than 1%.

 

Trigger Name                           Coverage %

UpdateResourceReqDates  75

DefaultTitle                               50

updaterequestowner             100

AccountDuplicateTrigger       80

ContactDuplicateTrigger      100

change_opps                          66

initialstage                              100

previousstage                         75

 

 

 

 

But when i try deploying them an error appears for each of four trigger i have mentioned above is that :

 

previousstage   Test coverage of selected Apex Trigger is 0%, at least 1% test coverage is required
updaterequestowner   Test coverage of selected Apex Trigger is 0%, at least 1% test coverage is required
initialstage   Test coverage of selected Apex Trigger is 0%, at least 1% test coverage is required

 

am i missing something else?? please help...

homer671homer671

Ah you have to deploy your test code to Production too.   So as well as the trigger you have to push your test classes.   Then it will run the test code there with the coverage you need.  

Sunny GSunny G

means do i have to push my test class using the trigger???

 

I certainly do not know how do do that. Please help me with one trigger and class.. like i have the following trigger:

 

trigger updaterequestowner on Pre_Sales_Request__c (before insert)
{

       Profile auth_profile = [select id from profile where name='HS_StandardUser_Presales'];          
       Id v_User = UserInfo.GetProfileId();
      for(Pre_Sales_Request__c ob : Trigger.new)
        {          
            if(v_User !=  auth_profile.Id)       
                {
                   ob.Request_Owner__c =  Userinfo.getUserId(); 
                }
        }                           
}

 

and i have the following test class :

 

@isTest

private class TriggerTests {
            
    public static testmethod void testTrigger() {
    /*   Pre_Sales_Request__c psrc=new Pre_Sales_Request__c( name = 'a' );
       insert psrc;
       psrc.name = 'b';
       update psrc;  */

    }
}

 

 

How to go to achive the code coverage while deployment, i.e. how to push test class from my trigger??  I know its taking too much of your efforts but its really an unforgettable help from your side....  

Sunny GSunny G

means do i have to push my test class using the trigger???

 

I certainly do not know how do do that. Please help me with one trigger and class.. like i have the following trigger:

 

trigger updaterequestowner on Pre_Sales_Request__c​ (before insert)
{

       Profile auth_profile = [select id from prof​ile where name='HS_StandardUser_Presales'];       ​   
       Id v_User = UserInfo.GetProfileId();
      for(Pre_Sales_Request__c ob : Trigger.new)
        {          
            if(v_User !=  auth_profile.Id)       
                {
                   ob.Request_Owner__c =  Userinfo​.getUserId(); 
                }
        }                           
}

 

and i have the following test class :

 

@isTest

private class TriggerTests {
            
    public static testmethod void testTrigger() {
       Pre_Sales_Request__c psrc=new Pre_Sales_R​equest__c( name = 'a' );
       insert psrc;
       psrc.name = 'b';
       update psrc;  

    }
}

 

 

How to go to achive the code coverage while deployment, i.e. how to push test class from my trigger??  I know its taking too much of your efforts but its really an unforgettable help from your side....  

homer671homer671

I'm not sure how you are deploying this.  But you can deploy the test class first, then you can deploy the trigger.

 

If you tell me how your doing the deployment maybe I can get more detailed.  Eclipse or through the Setup->Deploy.

Sunny GSunny G

Hi...

 

I tried deploying the test class first using both [i.e. Setup ->deploy ->change set]  &  eclipse.

But it gives me an error somthing like "Pre_Sales_Request__c" custom object does not exists in production.... i am afraid how to do this then...

 

 

Please help me with either way to deploy this custom object with all triggers and test class. I shall follow the same.

 

Which one is better from your point of view.. i'll follow that to achieve this .... its getting tough for me..... pls help....

 

camforcecamforce

It sounds like you are deploying to a Production environment that does not have the same custom objects as the development environment. The objects and fields being used in the trigger will have to be set up in the Production account as well. You can't reference an object that doesn't exist.

 

Let us know if this is the case.

Sunny GSunny G

Hi,

 

Yes.. you are right. I have a new custom object in sandbox . And what want that object to deploy to production.

 

Do i have to create that object itself in production?? if yes.. then will i have to create all the custom fields and validation as well???

 

Pls guide me what is the best way to deploy that custopm object????

camforcecamforce

Basically, the trigger cannot reference an object or field that does not exist in the environment. So the objects or fields that you are using in Development will also have to be created in Production.

Shrikanth LaxminarayanShrikanth Laxminarayan
A: Trigger
//Ray : On every Insert Opportunity, create a Opportunity Line Item Automatically
trigger CreateOLIonInsertOpp on Opportunity(After insert) {
  List < Opportunity_Line_Item__c > list_opportunity_line_item = new list < Opportunity_Line_Item__c > ();
  //for every new Opportunity inserted
  if (Trigger.IsInsert) {
    for (Opportunity vopportunity: [SELECT Id,Product_Name__c,Product_Pitched__c,Trial_Created__c,Trial_Created_date__c
        FROM Opportunity WHERE Id IN: trigger.New]) {
      //create a Product Map
      Map < String, Id > productMap = new map < String, Id > ();
      for (Product_Master__c vproduct: [select id, Name From Product_Master__c]) {
        productMap.put(vproduct.Name, vproduct.Id);
      }
      //check for a valid Product_Pitched__c 
      if (vopportunity.Product_Pitched__c != null) {
       system.debug('------------>>>>>>>For every product in OLI creation' + vopportunity.Product_Pitched__c);
       for (String singleProduct: vopportunity.Product_Pitched__c.split(';')) {
          system.debug('------------>>>>>> product' + singleProduct);
          if (productMap.containskey(singleProduct)) {
            system.debug('------------>>>>>>exists,');
            //create a line item in case of a Product_Pitched__c is found in Product_Master__c
            Opportunity_Line_Item__c vopportunity_line_item = new Opportunity_Line_Item__c();
            vopportunity_line_item.Opportunity__c = vopportunity.Id;
            vopportunity_line_item.Product_Name__c = productMap.get(singleProduct);
            if(vopportunity.Trial_Created__c == 'Yes') {
              vopportunity_line_item.Trial__c = true;
              vopportunity_line_item.Trial_Start_Date__c = vopportunity.Trial_Created_date__c !=null ? vopportunity.Trial_Created_date__c : DateTime.now();
            }
            system.debug('------------>>>>>> create a new opportunitylineitem' + vopportunity_line_item);
            list_opportunity_line_item.add(vopportunity_line_item);
          }
        }
      }
      // insert the newly created OLIs 
      if (list_opportunity_line_item.size() > 0) {
        insert list_opportunity_line_item;
      }
    }
  }
}
// Creates a product line item(custom child object on opportunity) with details of product. 
B. Test Class
@isTest
public class createOLIonOPPInsert_Test {
  public static testMethod void testolicreate() {
    Profile p = [SELECT Id FROM Profile WHERE Name='System Administrator']; 
    user intuser = new User(Alias = 'standt', Email='standarduser@testorg.com', 
      EmailEncodingKey='UTF-8', LastName='Integration User', LanguageLocaleKey='en_US', 
      LocaleSidKey='en_US', ProfileId = p.Id, 
      TimeZoneSidKey='America/Los_Angeles', UserName='intuser@practo.com');
    insert intuser;

    system.runAs(intuser) {
        //List<Territory_Mapping__c> list_Territory_Name = [Select Id From Territory_Mapping__c Where Territory_Name__c = 'Rest of India' Limit 1];
        String strRecordTypeId = [Select Id From RecordType Where SobjectType = 'Opportunity' and Name = 'Ray ROI'].Id;


        Account newaccount = new Account(
          Name = 'test opp Jul 171435',
          Practice_Category__c =  'Clinic',
          Ray_Practice_ID__c = '171435',
          Is_Computer_Available__c = 'Don\'t Know',
          Is_Internet_Availabe__c = 'Don\'t Know',
          Is_Receiptionist_Available__c = 'Don\'t Know',
          Lead_Type__c = 'Unpaid',
          Primary_Contact__c = '8989787898',
          Country_Code__c = 'India Only'
        );
        insert newaccount;

        Opportunity newopp = new Opportunity(
          RecordTypeId = strRecordTypeId,
            //Prod '012280000011DFA', Test : 0120k0000004NvXAAU
          //opportunity_id, //RAY ROI
          Name =  'OPP-'+ System.today().format(),
          Accountid = newaccount.Id,
          Product_Name__c  = 'Ray',
          Product_Pitched__c = 'Ray CM Atom',
          Closedate = System.today() + 7 ,
          StageName = 'Trial'
        );
        insert newopp;
    }
  }
}
//Creates and acccount and opportunity with mandatory fields filled in and expectation is that it will activate trigger on insert.
Pls Help!!