Friday, February 20, 2009

Code to clone a opportunity of Contact

Clone a opportunity of cpntact using the Clone Function
Object required is Contact,Opportunity, OpportunityContactRole.

public Void CloneOpportunity() {
try{
//ArrayList to get the Checked opportunityId
String []idsArray= allIds.split(';');
if(idsArray.size()<0){
for(Integer i=0;i
Opportunity opp=[select Id,Name,StageName,CloseDate from opportunity where id =:idsArray[i]];
Opportunity newOpp = opp.clone(false, true);
newOpp.Name=opp.Name+'_Clone';
insert newOpp;
//making a entry in OpportunityContactRole object
OpportunityContactRole opportunityRole = new OpportunityContactRole();
opportunityRole.OpportunityId=newOpp.Id;
opportunityRole.ContactId=System.currentPageReference().getParameters().get('id');
insert opportunityRole;
}
}throw new MyException();
}catch(MyException e){
string Msg = 'plz select a Opportunity';
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.INFO,Msg ));
return null;
}
return null;
}

No comments: