• Theodore Ray 18
  • NEWBIE
  • 0 Points
  • Member since 2018

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 3
    Questions
  • 3
    Replies
I have created a visual force section for the account page layout. The section is a contact list. In it I am trying to display the number of campaign members for each individual contact row shown.

Here's what it  looks like.

User-added image

The thing is I do not know how to out how to pass a value to apex from the VF page for every row/record  so that the controller can return data per record in the table.

Can anyone tell me the right method for doing this?
Dear All

Please help with this issue!

I have a hierarchy of Standard & custom objects joined by custom lookups. From the top:

1. Account
2. CRJ__c (with a lookup to Account)
3. Milestone__c (with a lookup to CRJ__c)

I have written code to create the CRJ record (and it's accompanying milestones) when a checkbox on the Account record is checked. 

The code is working and the records are being created. However, when I try and assign the ownership of the CRJ record in the code I get the error - Field is not writeable: CRJ__c.Owner.

I have checked and (1) this field is writable (I can change ownership in the front end). (2) As you can tell from point 1 - the field is a lookup field not a master detail field (though note it was once a master detail until I changed it to a lookup. Which I did before writing this code).

Here is the create method in the code. The problem line of code causing the error is underlined. I also tried assigning the owner after insert in case that made a difference but I got the same error - field is not writable...so that's not the solution.

Method:

    public static void insertCRJAndMileStoneCreate() {
        
        List<CRJ__c> existingCRJs = [SELECT Id, Account__c FROM CRJ__c WHERE Account__c IN : Trigger.newMap.keySet()];  //create a list and remove from the trigger...       
        List<Id> accountIdsWithExistingCRJs = new List <Id>();
        for (CRJ__c crj: existingCRJs){
            accountIdsWithExistingCRJs.add(crj.Account__c);
        }
        List<CRJ__c> newCRJsToInsert = new list <CRJ__c>();
        for (Account acc : (List<Account>) Trigger.new) {
            if (!accountIdsWithExistingCRJs.contains(acc.Id)&& acc.Create_CRJ__c){
                 //acc.Create_CRJ__c = FALSE; (need to be a before update)
                CRJ__c newCRJ = new CRJ__c();
                 newCRJ.Name = acc.Name+' CRJ';
                newCRJ.Account__c = acc.Id;
                newCRJ.Owner = (Id)'0050W000006VY4f';
               // newCRJ.Owner = acc.Owner; 
                newCRJsToInsert.add(newCRJ);
                //Milestone__c new
            }
            
        }
        insert newCRJsToInsert;
        
        
        //create all the milestones
        List<Milestone__c> allMilestones = new List <Milestone__c>(); 
        for (CRJ__c crj: newCRJsToInsert){
            //crj.Owner = (Id)'0050W000006VY4f';
            allMilestones.addAll(createFullMilestoneList(crj));
        }
        insert allMilestones;
    }

I guess either

A.  I am doing something wrong - if you can see what please let me know!!

or

B. Perhaps this is in artifact of the fact the CRJ and Account relationship was once a master detail relationship and so the CRJ__C.Owner field was indeed at that time not writable and for some reason this is persisting in the SF database perhaps just because weird stuff sometimes happens in sandboxes. If you have experience of something similar please share!! 

​​​​​​​Many thanks to All!!! :)
I;m new to coding and cannot figure out the issue with my test class. Can someone please help!

This is the trigger I am testing:

trigger CreateProProv on OpportunityLineItem (after update){
    //Instantiate the list here, before the loop
    List<Pro_Provisioning__c> myList = new List<Pro_Provisioning__c>();
    for (OpportunityLineItem OppLI : Trigger.new) {
        if (OppLI.Oppp_is_signed__c == 'Signed' && OppLI.Oppp_is_signed__c!= trigger.OldMap.get(OppLI.Id).Oppp_is_signed__c  
        && OppLI.Product_Family_for_Rollups__c == 'Pro'&& OppLI.Product_Subfamily_for_Rollups__c != 'Legacy' ) {
            Pro_Provisioning__c PP = new Pro_Provisioning__c();
            PP.Related_Opportunity__c = OppLI.OpportunityID;
            PP.OpLI_ID__c = OppLI.id;
            PP.Product1__c = OppLI.Product_Name_for_Formulas__c;
            PP.CurrencyIsoCode= OppLI.CurrencyIsoCode;
            PP.Pro_Experiments_Allowedt__c= OppLI.Pro_Experiments_Allowed__c;
            PP.Pro_Experiments_Periodt__c= OppLI.Pro_Experiments_Period__c;
            PP.Language__c = OppLI.Pro_Language__c;
            //Put the new record inside the list
            myList.add(PP);
        }
    }
    //Insert the whole list once the loop is finished (outside the loop)
    insert myList;
}

This is my test class:
@isTest public Class CreateProProvTest{
  @istest private static void testCreateProProvTrigger(){
Product2 prod = new Product2(Name = 'test', Family = 'Pro');
insert prod;
Id pricebookId = Test.getStandardPricebookId();
PricebookEntry standardPrice = new PricebookEntry(Pricebook2Id = pricebookId, Product2Id = prod.Id, UnitPrice = 500, IsActive = true);
insert standardPrice;
Pricebook2 customPB = new Pricebook2(Name='Custom Pricebook', isActive=true);
insert customPB;
PricebookEntry customPrice = new PricebookEntry(Pricebook2Id = customPB.Id, Product2Id = prod.Id, UnitPrice = 500, IsActive = true);
insert customPrice;
Account acc = new Account(Name='TestAccount');
insert acc;
Opportunity opp = new Opportunity(Name='TestOpportunity', AccountId=acc.Id, CloseDate=Date.Today(), 
                                  StageName='Qualification', Pricebook2Id=customPB.Id);
insert opp;
OpportunityLineItem oli = new OpportunityLineItem(OpportunityId = opp.Id, Quantity = 1, TotalPrice = 500, 
                                                  Oppp_is_signed__c = 'Not Signed', Pro_Experiments_Allowed__c = 3, 
                                                  Pro_Experiments_Period__c = 'Monthly', PricebookEntryId=customPrice.Id);
insert oli;
Test.startTest();
      oli.Oppp_is_signed__c = 'Signed';
          Update oli;
test.stopTest();
}
}

On the trigger record in SF Salesforce tells me that I have 100% test coverage for the trigger, but when I try to validate in my QA sandbox using run specified tests (this test only) the system throws an error and says "

Code Coverage Warning
Your code coverage is 35%. You need at least 75% coverage to complete this deployment.
CreateProProv"

Can someone please help! What am I missing?

Cheers!!

Theo

 
Dear All

Please help with this issue!

I have a hierarchy of Standard & custom objects joined by custom lookups. From the top:

1. Account
2. CRJ__c (with a lookup to Account)
3. Milestone__c (with a lookup to CRJ__c)

I have written code to create the CRJ record (and it's accompanying milestones) when a checkbox on the Account record is checked. 

The code is working and the records are being created. However, when I try and assign the ownership of the CRJ record in the code I get the error - Field is not writeable: CRJ__c.Owner.

I have checked and (1) this field is writable (I can change ownership in the front end). (2) As you can tell from point 1 - the field is a lookup field not a master detail field (though note it was once a master detail until I changed it to a lookup. Which I did before writing this code).

Here is the create method in the code. The problem line of code causing the error is underlined. I also tried assigning the owner after insert in case that made a difference but I got the same error - field is not writable...so that's not the solution.

Method:

    public static void insertCRJAndMileStoneCreate() {
        
        List<CRJ__c> existingCRJs = [SELECT Id, Account__c FROM CRJ__c WHERE Account__c IN : Trigger.newMap.keySet()];  //create a list and remove from the trigger...       
        List<Id> accountIdsWithExistingCRJs = new List <Id>();
        for (CRJ__c crj: existingCRJs){
            accountIdsWithExistingCRJs.add(crj.Account__c);
        }
        List<CRJ__c> newCRJsToInsert = new list <CRJ__c>();
        for (Account acc : (List<Account>) Trigger.new) {
            if (!accountIdsWithExistingCRJs.contains(acc.Id)&& acc.Create_CRJ__c){
                 //acc.Create_CRJ__c = FALSE; (need to be a before update)
                CRJ__c newCRJ = new CRJ__c();
                 newCRJ.Name = acc.Name+' CRJ';
                newCRJ.Account__c = acc.Id;
                newCRJ.Owner = (Id)'0050W000006VY4f';
               // newCRJ.Owner = acc.Owner; 
                newCRJsToInsert.add(newCRJ);
                //Milestone__c new
            }
            
        }
        insert newCRJsToInsert;
        
        
        //create all the milestones
        List<Milestone__c> allMilestones = new List <Milestone__c>(); 
        for (CRJ__c crj: newCRJsToInsert){
            //crj.Owner = (Id)'0050W000006VY4f';
            allMilestones.addAll(createFullMilestoneList(crj));
        }
        insert allMilestones;
    }

I guess either

A.  I am doing something wrong - if you can see what please let me know!!

or

B. Perhaps this is in artifact of the fact the CRJ and Account relationship was once a master detail relationship and so the CRJ__C.Owner field was indeed at that time not writable and for some reason this is persisting in the SF database perhaps just because weird stuff sometimes happens in sandboxes. If you have experience of something similar please share!! 

​​​​​​​Many thanks to All!!! :)
I;m new to coding and cannot figure out the issue with my test class. Can someone please help!

This is the trigger I am testing:

trigger CreateProProv on OpportunityLineItem (after update){
    //Instantiate the list here, before the loop
    List<Pro_Provisioning__c> myList = new List<Pro_Provisioning__c>();
    for (OpportunityLineItem OppLI : Trigger.new) {
        if (OppLI.Oppp_is_signed__c == 'Signed' && OppLI.Oppp_is_signed__c!= trigger.OldMap.get(OppLI.Id).Oppp_is_signed__c  
        && OppLI.Product_Family_for_Rollups__c == 'Pro'&& OppLI.Product_Subfamily_for_Rollups__c != 'Legacy' ) {
            Pro_Provisioning__c PP = new Pro_Provisioning__c();
            PP.Related_Opportunity__c = OppLI.OpportunityID;
            PP.OpLI_ID__c = OppLI.id;
            PP.Product1__c = OppLI.Product_Name_for_Formulas__c;
            PP.CurrencyIsoCode= OppLI.CurrencyIsoCode;
            PP.Pro_Experiments_Allowedt__c= OppLI.Pro_Experiments_Allowed__c;
            PP.Pro_Experiments_Periodt__c= OppLI.Pro_Experiments_Period__c;
            PP.Language__c = OppLI.Pro_Language__c;
            //Put the new record inside the list
            myList.add(PP);
        }
    }
    //Insert the whole list once the loop is finished (outside the loop)
    insert myList;
}

This is my test class:
@isTest public Class CreateProProvTest{
  @istest private static void testCreateProProvTrigger(){
Product2 prod = new Product2(Name = 'test', Family = 'Pro');
insert prod;
Id pricebookId = Test.getStandardPricebookId();
PricebookEntry standardPrice = new PricebookEntry(Pricebook2Id = pricebookId, Product2Id = prod.Id, UnitPrice = 500, IsActive = true);
insert standardPrice;
Pricebook2 customPB = new Pricebook2(Name='Custom Pricebook', isActive=true);
insert customPB;
PricebookEntry customPrice = new PricebookEntry(Pricebook2Id = customPB.Id, Product2Id = prod.Id, UnitPrice = 500, IsActive = true);
insert customPrice;
Account acc = new Account(Name='TestAccount');
insert acc;
Opportunity opp = new Opportunity(Name='TestOpportunity', AccountId=acc.Id, CloseDate=Date.Today(), 
                                  StageName='Qualification', Pricebook2Id=customPB.Id);
insert opp;
OpportunityLineItem oli = new OpportunityLineItem(OpportunityId = opp.Id, Quantity = 1, TotalPrice = 500, 
                                                  Oppp_is_signed__c = 'Not Signed', Pro_Experiments_Allowed__c = 3, 
                                                  Pro_Experiments_Period__c = 'Monthly', PricebookEntryId=customPrice.Id);
insert oli;
Test.startTest();
      oli.Oppp_is_signed__c = 'Signed';
          Update oli;
test.stopTest();
}
}

On the trigger record in SF Salesforce tells me that I have 100% test coverage for the trigger, but when I try to validate in my QA sandbox using run specified tests (this test only) the system throws an error and says "

Code Coverage Warning
Your code coverage is 35%. You need at least 75% coverage to complete this deployment.
CreateProProv"

Can someone please help! What am I missing?

Cheers!!

Theo