• Meghana Sharma
  • NEWBIE
  • 20 Points
  • Member since 2019

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 15
    Replies
Hello Developers

need small Assistance with the apex class

My requirement is that when i create a lead with country as 'india', there is a custom field named 'India_Auto_Number__c' where it should update as 1, and again when i create a lead with country name 'india' it should increment to 2...like this it should keep on incrementing till 5th lead, when i try to insert 6th lead, it should again update as 1
how do i achive this functionality
 
public class IncrementonLead {
    public void onInsert( list<Lead> newList){

    List<Lead> lstld = [SELECT Id,India_Auto_Number__c FROM Lead];
    Integer intCounter = lstld.size() != 0 ? lstld[0].India_Auto_Number__c : 0;
    for(Lead objLead: newList)
    {
        intCounter ++;
        objLead.India_Auto_Number__c = intCounter;
    }

    }
}

This is what i have  come up with so far
Hello 

i have written a test class for the trigger, when i try to run the test i get 100% code coverage,i have even disabled the validation rule which is present on the lead object,  but still in the Test results i get the following error

System.DmlException: Insert failed. First exception on row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, Error::::::Divide by 0: [CRM_Owner__c]

how do i resolve this issue
Below is my trigger and Test class
 
trigger ShareWithCRMOwner on Lead (after insert,after update) {
    List<LeadShare> csShareList = new List<LeadShare>();
    for( Lead cs : trigger.new ) {
        if( cs.CRM_Owner__c != NULL ) {
            // Create a new LeadShare object for each Lead where CRM_Owner__c field is not NULL.
            LeadShare csShare = new LeadShare();
            // Give Read write access to that user for this particular Lead record.
            csShare.LeadAccessLevel = 'edit';
            // Assign Lead Id of Lead record.
            csShare.LeadId = cs.id;
            // Assign user id to grant read write access to this particular Lead record.
            csShare.UserOrGroupId = cs.CRM_Owner__c;
            csShareList.add( csShare );
        }
    }
    if( csShareList != null && csShareList.size() != 0 ) {
        try {
            insert csShareList;
            update csShareList;
             if(Test.isRunningTest())
            {
                integer k=1/0;
            }
        }catch( Exception e ) {
            trigger.new[0].CRM_Owner__c.addError('Error::::::'+e.getMessage());
        }
    }
}

Test Class
 
@isTest
private class TestShareWithCRMOwner {
    
    // test that newly inserted records marked as pubic=true have corresponding shares created
    static testMethod void testAddShares() {
        
        Set<ID> ids = new Set<ID>();
        List<Lead> Leads = new List<Lead>();
        
        for (Integer i=0;i<50;i++)
            Leads.add(new Lead(FirstName='First ',LastName='Name '+i,RecordTypeId='012p0000000Nn05AAC',
                               Email='email'+i+'@email.com',Company='ABSYZ',CRM_Owner__c='00528000006OKhPAAW'));
        
        insert Leads;
        
        // get a set of all new created ids
        for (Lead c : Leads)
            ids.add(c.id);
        
        // assert that 50 shares were created
        List<LeadShare> shares = [select id from LeadShare where 
                                  LeadId IN :ids and RowCause = 'Manual'];
        System.assertEquals(shares.size(),50);
        
    }
    
    // insert records and switch them from public = true to public = false
    static testMethod void testUpdateContacts() {
        
        Set<ID> ids = new Set<ID>();
        List<Lead> Leads = new List<Lead>();
        
        for (Integer i=0;i<50;i++)
            Leads.add(new Lead(FirstName='First ',LastName='Name '+i,RecordTypeId='012p0000000Nn05AAC',
                               Email='email'+i+'@email.com',Company='ABSYZ',CRM_Owner__c='00528000006OKhPAAW'));
        
        insert Leads;
        
        for (Lead c : Leads)
            ids.add(c.id);
        
        update Leads;
        
        // assert that 0 shares exist
        List<LeadShare> shares = [select id from LeadShare where 
                                  LeadId IN :ids and RowCause = 'Manual'];
        System.assertEquals(shares.size(),0);
        
        for (Lead c : Leads)
            c.CRM_Owner__c='2F00528000006OKhP';
        
        update Leads;
        
        // assert that 50 shares were created
        shares = [select id from LeadShare where LeadId IN :ids and RowCause = 'Manual'];
        System.assertEquals(shares.size(),50);
        
        for (Lead c : Leads)
            c.CRM_Owner__c='2F00528000006OKhP';
        
        update Leads;
        
        // assert that 0 shares exist
        shares = [select id from LeadShare where LeadId IN :ids and RowCause = 'Manual'];
        System.assertEquals(shares.size(),0);
        
    }
    
}




 
Hello Developers

need small Assistance with the apex class

My requirement is that when i create a lead with country as 'india', there is a custom field named 'India_Auto_Number__c' where it should update as 1, and again when i create a lead with country name 'india' it should increment to 2...like this it should keep on incrementing till 5th lead, when i try to insert 6th lead, it should again update as 1
how do i achive this functionality
 
public class IncrementonLead {
    public void onInsert( list<Lead> newList){

    List<Lead> lstld = [SELECT Id,India_Auto_Number__c FROM Lead];
    Integer intCounter = lstld.size() != 0 ? lstld[0].India_Auto_Number__c : 0;
    for(Lead objLead: newList)
    {
        intCounter ++;
        objLead.India_Auto_Number__c = intCounter;
    }

    }
}

This is what i have  come up with so far
Hello 

i have written a test class for the trigger, when i try to run the test i get 100% code coverage,i have even disabled the validation rule which is present on the lead object,  but still in the Test results i get the following error

System.DmlException: Insert failed. First exception on row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, Error::::::Divide by 0: [CRM_Owner__c]

how do i resolve this issue
Below is my trigger and Test class
 
trigger ShareWithCRMOwner on Lead (after insert,after update) {
    List<LeadShare> csShareList = new List<LeadShare>();
    for( Lead cs : trigger.new ) {
        if( cs.CRM_Owner__c != NULL ) {
            // Create a new LeadShare object for each Lead where CRM_Owner__c field is not NULL.
            LeadShare csShare = new LeadShare();
            // Give Read write access to that user for this particular Lead record.
            csShare.LeadAccessLevel = 'edit';
            // Assign Lead Id of Lead record.
            csShare.LeadId = cs.id;
            // Assign user id to grant read write access to this particular Lead record.
            csShare.UserOrGroupId = cs.CRM_Owner__c;
            csShareList.add( csShare );
        }
    }
    if( csShareList != null && csShareList.size() != 0 ) {
        try {
            insert csShareList;
            update csShareList;
             if(Test.isRunningTest())
            {
                integer k=1/0;
            }
        }catch( Exception e ) {
            trigger.new[0].CRM_Owner__c.addError('Error::::::'+e.getMessage());
        }
    }
}

Test Class
 
@isTest
private class TestShareWithCRMOwner {
    
    // test that newly inserted records marked as pubic=true have corresponding shares created
    static testMethod void testAddShares() {
        
        Set<ID> ids = new Set<ID>();
        List<Lead> Leads = new List<Lead>();
        
        for (Integer i=0;i<50;i++)
            Leads.add(new Lead(FirstName='First ',LastName='Name '+i,RecordTypeId='012p0000000Nn05AAC',
                               Email='email'+i+'@email.com',Company='ABSYZ',CRM_Owner__c='00528000006OKhPAAW'));
        
        insert Leads;
        
        // get a set of all new created ids
        for (Lead c : Leads)
            ids.add(c.id);
        
        // assert that 50 shares were created
        List<LeadShare> shares = [select id from LeadShare where 
                                  LeadId IN :ids and RowCause = 'Manual'];
        System.assertEquals(shares.size(),50);
        
    }
    
    // insert records and switch them from public = true to public = false
    static testMethod void testUpdateContacts() {
        
        Set<ID> ids = new Set<ID>();
        List<Lead> Leads = new List<Lead>();
        
        for (Integer i=0;i<50;i++)
            Leads.add(new Lead(FirstName='First ',LastName='Name '+i,RecordTypeId='012p0000000Nn05AAC',
                               Email='email'+i+'@email.com',Company='ABSYZ',CRM_Owner__c='00528000006OKhPAAW'));
        
        insert Leads;
        
        for (Lead c : Leads)
            ids.add(c.id);
        
        update Leads;
        
        // assert that 0 shares exist
        List<LeadShare> shares = [select id from LeadShare where 
                                  LeadId IN :ids and RowCause = 'Manual'];
        System.assertEquals(shares.size(),0);
        
        for (Lead c : Leads)
            c.CRM_Owner__c='2F00528000006OKhP';
        
        update Leads;
        
        // assert that 50 shares were created
        shares = [select id from LeadShare where LeadId IN :ids and RowCause = 'Manual'];
        System.assertEquals(shares.size(),50);
        
        for (Lead c : Leads)
            c.CRM_Owner__c='2F00528000006OKhP';
        
        update Leads;
        
        // assert that 0 shares exist
        shares = [select id from LeadShare where LeadId IN :ids and RowCause = 'Manual'];
        System.assertEquals(shares.size(),0);
        
    }
    
}