2

So here's my issue. I wrote a Trigger in order to handle CaseShares, and the problem that I am having is at.

    for(Case cs : trigger.new){ 
      if(PartnerUsermap.containsKey(cs.CreatedById)){
        for(GroupMember grpmem : GroupMemberList){
  //Here! if(grpmem.UserOrGroupId == cs.CreatedById){  --Group member is a Portal user, and this checks if he created the Case
          CaseShare  csshare = new CaseShare (CaseId = cs.id,UserOrGroupId = grpmem.GroupId,CaseAccessLevel = 'Edit'); //This line doesn't get coverage, I'm assuming because I'm not getting a match
            CsShareList.add(csshare);   

The problem is that I can't get a good comparission when developing my test class, and not I'm sure how to feed in my data from my test class in order to properly get the match. Below is my test class. I think I'm close to getting it with my System.runas, but I'm stuck.. Any guidance would be great

 static testMethod void myUnitTest1() {
    List < Case > leadList = new List < Case > ();
    List < CaseShare > leadShareList = new list < CaseShare > ();

    String dateString = String.valueof(Datetime.now()).replace(' ', '').replace(':', '').replace('-', '');
    String orgId = UserInfo.getOrganizationId();
    Integer randomInt = Integer.valueOf(math.rint(math.random() * 1000000));
    String uniqueName = orgId + dateString + randomInt;

    Account accInd = new account(name = 'individual');
    insert accInd;
    Contact contact = new Contact(LastName = 'hello', FirstName = 'World', Email = '[email protected]', AccountId = accInd.Id);
    insert contact;

    Profile p = [SELECT Id FROM Profile WHERE UserType = 'PowerPartner' limit 1 ];
    User u = new User(Alias = 'standt',
        Email = '[email protected]',
        EmailEncodingKey = 'UTF-8',
        LastName = 'Testing',
        LanguageLocaleKey = 'en_US',
        LocaleSidKey = 'en_US',
        ProfileId = p.Id,
        TimeZoneSidKey = 'America/Los_Angeles',
        Username = uniqueName + '@test' + orgId + '.org',
        ContactId = contact.id);

    Product2 p2 = new Product2();
    p2.Name = 'TestProduct';
    p2.IsActive = True;
    p2.AE_Text__c = 'TestText';
    p2.Item_number__c = 'TestNumber';
    insert p2;

    Case c = new Case();
    c.Product_old__c = p2.id;
    c.Status = 'New';
    c.Origin = 'Email';
    c.Case_Level__c = 'Level 1 - Product Concept/Selection';
    c.Territory__c = 'Asia';
    c.Region__c = 'CHINA';
    system.debug('Creating a New case!   ' + c + 'Created by!   ' + c.CreatedById);

    System.runAs(u) {
        System.debug('Current User: ' + UserInfo.getUserName());
        System.debug('Current Profile: ' + UserInfo.getProfileId());
        insert c;

        GroupMember groupMemberStore = [select GroupId, UserOrGroupId from GroupMember limit 1];
        system.debug('GroupMemberStore ' + groupMemberStore);
        CaseShare cs = new CaseShare();
        cs.CaseId = c.id;

        cs.UserOrGroupId = groupMemberStore.UserOrGroupId;
        cs.CaseAccessLevel = 'Edit';
        system.debug('ls ++!@+!  ' + cs);
        insert cs;

    }
    Case newCase = [SELECT Id, CaseNumber, CreatedById FROM Case WHERE CreatedById = : u.id]; //Can't figure this out        
    System.debug('NEW CASE ' + newCase);
    System.assertEquals(u.Id, newCase.CreatedById);
}

2 Answers 2

0

Is this a before insert or after insert trigger?

I have noticed some odd things lately where the createdbyID is null during test methods in before insert triggers. (May have always been that way but the test methods worked previously.....)

Debug the createdbyid and the maps to see where the code is not finding a match for the key and value looking for and work backwards from there.

2
  • It does come through null, which is why I have that Case newCase at the bottom of the test script, and its after insert, after update
    – EricSSH
    May 7, 2014 at 18:36
  • Ok. Then follow through on the second part and debug your maps. Given the small snippet of code there is no real way for anyone to help except to say it is the key not being found in the partner, group or the createdbyid does not equal the userorgroupid
    – Eric
    May 7, 2014 at 21:12
0

@Eyescream He Helped me out a lot and actually solved this for me

    static testMethod void myUnitTest1() {
        String dateString = String.valueof(Datetime.now()).replace(' ', '').replace(':', '').replace('-', '');
        String orgId = UserInfo.getOrganizationId();
        Integer randomInt = Integer.valueOf(math.rint(math.random() * 1000000));
        String uniqueName = orgId + dateString + randomInt;

        Account accInd = new account(name = 'individual');
        insert accInd;
        Contact contact = new Contact(LastName = 'hello', FirstName = 'World', Email = '[email protected]', AccountId = accInd.Id);
        insert contact;

User u = new User(Alias = 'standt', 
Email = '[email protected]', 
EmailEncodingKey = 'UTF-8', 
LastName = 'Testing', 
LanguageLocaleKey = 'en_US', 
LocaleSidKey = 'en_US', 
ProfileId = [SELECT Id FROM Profile WHERE UserType = 'PowerPartner' limit 1 ].Id,   
TimeZoneSidKey = 'America/Los_Angeles', 
Username = uniqueName + '@test' + orgId + '.org', 
ContactId = contact.id); 
insert u; 

Product2 p2 = new Product2( 
Name = 'TestProduct', 
IsActive = True, 
AE_Text__c = 'TestText', 
Item_number__c = 'TestNumber' 
); 
insert p2; 

Group g = new Group(
    Name = 'unit test group',
    DeveloperName = 'unit_test_group',
    Type = 'Regular'
);
insert g;
System.debug('Group Id: ' + g.Id);

GroupMember gp = new GroupMember(
    GroupId = g.Id,
    UserOrGroupId = u.Id
);
insert gp;
System.debug('GroupMem Id: ' +gp.Id + ' UserGPID ' +gp.UserOrGroupId);

Case c = new Case( 
Status = 'New', 
Origin = 'Email', 
Case_Level__c = 'Level 1 - Product Concept/Selection', 
Territory__c = 'Asia', 
Region__c = 'CHINA' 
); 

System.runAs(u) { 
insert c; 
}
system.debug('This is C' +c+ 'CID' +c.id );


//List<CaseShare> shares = [SELECT Id FROM CaseShare WHERE CaseId = :c.Id AND CaseAccessLevel = 'Edit' AND UserOrGroupId = :gp.Id];
List<CaseShare> shares = [SELECT CaseId, UserOrGroupId, CaseAccessLevel FROM CaseShare WHERE CaseId = :c.Id limit 1];
System.debug('Shares!' +shares);

// Check what does the debug log say... If all went OK you would be able to write this select like that:

/*  SELECT Id
    FROM CaseShare
    WHERE CaseId = :c.Id 
        AND CaseAccessLevel = 'Edit'
        AND UserOrGroupId = :g.Id
    */

    System.assertEquals(1, shares.size());

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .