• Abhishek Sharma 194
  • NEWBIE
  • 10 Points
  • Member since 2017

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 1
    Replies
Hi guys,
 am new in lightning in m trying to clone opportunity with line item which i already achieved  But now i can't open and new cloned opportunity in edit view.   
I can either travel to new clonned opportunity with event or i can show an edit component which show overlay on old opportunity after save that overlay edit mode we stay on old opportunity which i have to travel to new opportunity.

java script:
({
    doInit : function(component, event, helper) {
        var recordId = component.get("v.recordId"); 
        var action = component.get("c.getCloneOpp");
        action.setParams({"oldId": recordId});
        action.setCallback(this, function(response){
            var state = response.getState();
            if (component.isValid() && state === "SUCCESS") {
                var retrunValueId =response.getReturnValue(); 
                component.set("v.recordIdNew", retrunValueId);
               
               /* var editRecordEvent = $A.get("e.force:editRecord");
			    editRecordEvent.setParams({
		        "recordId": retrunValueId
   				}); 
                editRecordEvent.fire();*/
                
                var closeActionEvent = $A.get("e.force:closeQuickAction");
                closeActionEvent.fire();
            }
        });
        $A.enqueueAction(action); 
        
    } // end doInit function
    
    
})

Component :
<aura:component implements="force:lightningQuickActionWithoutHeader,force:hasRecordId" controller="CloneOpportunityProducts">
    <aura:attribute name="recordId" type="String"/>
    <aura:attribute name="recordIdNew" type="String"/>
    <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
    <h1>Cloning Opportunity...</h1>
     
</aura:component>

Controller:
public class CloneOpportunityProducts {
    
    @AuraEnabled
    public static String getCloneOpp( String oldId ) {
        //system.debug( 'oldId ' + oldId );
        
        // Initialize setup variables
        String objectName = 'Opportunity';  // modify as needed
        String query = 'SELECT';
        Map<String, Schema.SObjectField> objectFields = Schema.getGlobalDescribe().get( objectName ).getDescribe().fields.getMap();
        
        // Grab the fields from the describe method and append them to the queryString one by one.
        for( String s : objectFields.keySet() ) {
            query += ' ' + s + ', ';
        }
        
        // Manually add related object's fields that are needed.
        query += 'Account.Name '; // modify as needed    'Account.Name';
        
        // Strip off the last comma if it exists.
        if ( query.subString( query.Length()-1, query.Length() ) == ',' ){
            query = query.subString( 0, query.Length()-1 );
        }
        
        // Add FROM statement
        query += ' FROM ' + objectName;
        
        // Add on a WHERE/ORDER/LIMIT statement as needed
        query += ' WHERE Id =  \''+ oldId +'\'  LIMIT 1'; // modify as needed   WHERE Id = \'test\' AND HasOpportunityLineItem = true LIMIT 1'; 
        
        Opportunity opp = database.query( query );
        system.debug( opp );
        Opportunity newOpp = opp.clone( false, false, false, false );
        newOpp.Name = opp.Name + ' clone';
        newOpp.Program_ID__c = '';
        insert newOpp;   
        if ( opp.HasOpportunityLineItem == true ) {
            List<OpportunityLineItem> products = [ SELECT Id, Name, Quantity, UnitPrice, PricebookEntryId
                                                  /*, Cost__c, Customer_Target_Price__c,
                                                   D_D_Cost__c, D_D_Margin__c, D_D_Price__c, D_D_Recovery__c, ServiceDate, Discount, Description,
                                                   ListPrice, Margin__c, Margin_Percentage__c, Product2Id, Product_Annual_Sales__c, ProductCode,
                                                   Selling_Price__c, Subtotal, Target_Actual_GM__c, Tooling_Cost__c, Tooling_Margin__c, Tooling_Margin_New__c,
                                                   Tooling_Price__c, Tooling_Recovery__c*/
                                                   FROM OpportunityLineITem 
                                                   WHERE OpportunityId = :opp.Id
                                                 ];
            
            
             
            List<OpportunityLineItem> newProdList = new List<OpportunityLineItem>();
            for ( OpportunityLineItem prod : products ) {
                OpportunityLineItem newProd = prod.clone( false, false, false, false );
                newProd.OpportunityId = newOpp.id;
                newProdList.add( newProd );
            }
            insert( newProdList );          
        } // end if
        
        return newOpp.id;  
    }
    
}

Challenge Not yet complete... here's what's wrong: 
Could not find the contact's name in the debug log. Be sure to run a query for your record, and to write your contact's name to the debug log using the System.debug() method.

I have this error in this unit any help would be great.

Thanks in advance.
Challenge Not yet complete... here's what's wrong: 
Could not find the contact's name in the debug log. Be sure to run a query for your record, and to write your contact's name to the debug log using the System.debug() method.

I have this error in this unit any help would be great.

Thanks in advance.