0

i have created one button on record top beside the clone button, when clicked, execute code that would make a new version record with the same name and append today's date on the end of the name to distinguish it from the original.i created apex class and custom list button but the i clicked on button so java script does'not working on this.so below error occour during click on the button.

How should i do this ?

List has no rows for assignment to SObject

Apex Class

Public Class CloneVersionwithTextController{

Public List<Master_Service_Agreement_Version__c> msalist{get;set;}
public string msaid{get;set;}

Public CloneVersionwithTextController(){

    msaid = ApexPages.CurrentPage().getParameters().get('id');

    List<Master_Service_Agreement_Version__c> msalist = new List<Master_Service_Agreement_Version__c>();
    Master_Service_Agreement_Version__c msa = [SELECT ID, Name, Status__c FROM Master_Service_Agreement_Version__c WHERE Id = : msaid];
    Master_Service_Agreement_Version__c msaCopy = msa.clone(false,true);
    insert msaCopy;
    system.debug('===msaCopy===='+msaCopy);
    List<MSA_Text_Block__c> mtext = new List<MSA_Text_Block__c>();
    system.debug('===mtext===='+mtext);
    mtext = [SELECT Id, Name, Service__c, Order_within_Service__c,Master_Service_Agreement_Version__r.ID,LastModifiedBy.Name FROM MSA_Text_Block__c WHERE Master_Service_Agreement_Version__r.ID = : msa.Id];
    for(MSA_Text_Block__c c : mtext)
    {
        MSA_Text_Block__c mtextCopy = c.clone(false,true);
        mtextCopy.Master_Service_Agreement_Version__c = msaCopy.Id;
        mtext.add(mtextCopy);
    }
    system.debug('===msaList===='+msaList);
    if(msaList.size() > 0){
        msaList.get(0).Name = msaList.get(0).Name + Datetime.now();
    }
    update msaList;
}
}

Visualforce Page

 <apex:page controller="CloneVersionwithTextController" >
   <apex:detail subject="{!msaid}" relatedList="true" title="false"/>
 </apex:page>

List button code

{!URLFOR("/apex/CloneVersionwithText", "")}?msaid={!Master_Service_Agreement_Version__c.Id}
0
Master_Service_Agreement_Version__c msa = [SELECT ID, Name, Status__c FROM Master_Service_Agreement_Version__c WHERE Id = : msaid];

in this line if you don't get any record then you will get this error. convert msa into List<Master_Service_Agreement_Version__c> msaList and first check size if it is msaList.size()>0 the refer first element. It will solve your problem.Thanks.

  • Master_Service_Agreement_Version__c msaCopy = msalist.clone(false,true); this line error show that: Method does not exist or incorrect signature: [List<Master_Service_Agreement_Version__c>].clone(Boolean, Boolean) – Ravi Vadher Aug 17 '15 at 12:35
  • i used msalist but did'not solve the issue – Ravi Vadher Aug 17 '15 at 12:37
  • Master_Service_Agreement_Version__c msaCopy = msalist[0].clone(false,true); try this – Tushar Sharma Aug 17 '15 at 12:37
  • tushar i need your help show new error:-List index out of bounds: 0 – Ravi Vadher Aug 17 '15 at 12:51
  • Please let me know. – Tushar Sharma Aug 17 '15 at 12:52
0

{!URLFOR("/apex/CloneVersionwithText", "")}?msaid={!Master_Service_Agreement_Version__c.Id}

In your button you use msaid variable but in class you are getting id variable. I think if you change this line

msaid = ApexPages.CurrentPage().getParameters().get('id');

with this

msaid = ApexPages.CurrentPage().getParameters().get('msaid');

it will work just fine. You can test you are getting a correct id with putting a system debug line after msaid line.

system.debug('msaid = ' + msaid);

UPDATE: DML not allowed

Public List<Master_Service_Agreement_Version__c> msalist{get;set;}
public string msaid{get;set;}

Public CloneVersionwithTextController(){}
public void myFunction(){
    msaid = ApexPages.CurrentPage().getParameters().get('msaid');

        List<Master_Service_Agreement_Version__c> msalist = new List<Master_Service_Agreement_Version__c>();
        Master_Service_Agreement_Version__c msa = [SELECT ID, Name, Status__c FROM Master_Service_Agreement_Version__c WHERE Id = : msaid];
        Master_Service_Agreement_Version__c msaCopy = msa.clone(false,true);
        insert msaCopy;
        system.debug('===msaCopy===='+msaCopy);
        List<MSA_Text_Block__c> mtext = new List<MSA_Text_Block__c>();
        system.debug('===mtext===='+mtext);
        mtext = [SELECT Id, Name, Service__c, Order_within_Service__c,Master_Service_Agreement_Version__r.ID,LastModifiedBy.Name FROM MSA_Text_Block__c WHERE Master_Service_Agreement_Version__r.ID = : msa.Id];
        for(MSA_Text_Block__c c : mtext)
        {
            MSA_Text_Block__c mtextCopy = c.clone(false,true);
            mtextCopy.Master_Service_Agreement_Version__c = msaCopy.Id;
            mtext.add(mtextCopy);
        }
        system.debug('===msaList===='+msaList);
        if(msaList.size() > 0){
            msaList.get(0).Name = msaList.get(0).Name + Datetime.now();
        }
        update msaList;
    }
    }

Visualforce Page

<apex:page controller="CloneVersionwithTextController" action="{!myFunction}">
   <apex:detail subject="{!msaid}" relatedList="true" title="false"/>
 </apex:page>
  • show error "DML currently not allowed" – Ravi Vadher Aug 18 '15 at 5:38
  • DML operations are not allowed in constructor method. You need to move insert and update out of the constructor. If this code will work only once when you clicked the button you can write a new method and add this method in visualforce page action tag. I've updated my answer to make this change. – tugce Aug 18 '15 at 6:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.