• Rakesh Babu 3
  • NEWBIE
  • 0 Points
  • Member since 2016

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 0
    Replies
Hi,

I receiving "Field is not writeable: OpportunityShare.OpportunityId" error for the below code.Can someone please help me to sort out the error?


trigger shareRecord on Opportunity (after insert,after update) {
    List<OpportunityShare> share=new List<OpportunityShare>();
    User u=[select id from User where alias='kshar'];
    for(Opportunity op:Trigger.New){
        if(op.amount>50000){
            OpportunityShare p=new OpportunityShare();
            p.OpportunityId=op.id;
            p.UserOrGroupId=u.id;
            p.OpportunityAccessLevel='Read';
            p.RowCause='Manual';
            share.add(p);
        }
    }
    insert share;
}
When i execute the below test code:

@isTest
public class TestAfterInsert {   
    static testmethod void AItest(){   
     Account  a1 = new Account (Name = 'After Insert1', Phone = '1234567890');              
        Test.startTest();
        insert a1;
        Test.stopTest();
        Contact c = [Select LastName, Phone from contact where ID = :a1.id];       
        system.assertequals(c.lastname,a1.name);
        system.assertequals(c.phone,a1.phone);      
       }
     }

for below trigger :

trigger AfterInsert on Account (after insert) {
    List<Contact> cons = new List<Contact>();
    For(account a: trigger.new){       
        contact c = new contact();
        c.AccountId = a.Id;
        c.LastName = a.Name;
        c.Phone = a.Phone;       
        cons.add(c); 
    }
    insert cons;       
    }

I am receiving "System.QueryException: List has no rows for assignment to SObject"  error. Can someone please help me out to resolve the error?