You need to sign in to do that
Don't have an account?
Priya Raj 22
How to pass a JSON string and a DML operation together in a list
Hi All,
i am new to apex programming. in apex code below i have to pass one dml statement(opplist) to pass account id along with json string (parentOppJson) can anybody let me know how to do that.
/**
* Controller used by the "scheduleOppsLwc" Lightning Web Component.
* @since 2021-10-05
* @version 2021-10-05
*/
public class ScheduleOppsCC {
/**
* Creates new child installment Opportunities related to the Opportunity
* used for the "Schedule Opportunities" quick action and updates the parent
* as needed to CPQ-related details.
* @param parentOpp ID of Opportunity from quick action context
* @param parentOppJson JSON string-representation of the parent opportunity with new values for update
* @param childOppsJson JSON string-representation of a list of child opportunities to create
*/
@AuraEnabled
public static void createChildrenUpdateParent(String parentOpp, String parentOppJson, String childOppsJson) {
system.debug('--> Schema.Opportunity.SObjectType ' + Schema.Opportunity.SObjectType);
Id parentOppId = Id.valueOf( String.escapeSingleQuotes(parentOpp) );
system.debug('--> parentOppId ' + parentOppId);
if ( Schema.Opportunity.SObjectType == parentOppId.getSObjectType() )
{
try {
List<Opportunity> parentOp = (List<Opportunity>)JSON.deserialize(parentOppJson, List<Opportunity>.class);
System.debug( '-> parentOp = ' + parentOp );
//List<Opportunity> oppList = [ SELECT Id, AccountId , RecordTypeId , StageName, Name,CloseDate FROM Opportunity WHERE Id = :parentOppId ];
if ( (parentOp != null) && !parentOp .isEmpty() ) {
List<Opportunity> childOpps = (List<Opportunity>)JSON.deserialize(childOppsJson, List<Opportunity>.class);
system.debug('--> childOpps after deserialize ' + childOpps);
List<Opportunity> insertingChildren = new List<Opportunity>();
for ( Integer i = 0; i < childOpps.size(); ++i ) {
Opportunity opp = new Opportunity(AccountId = parentOp [0].AccountId, RecordTypeId = parentOp [0].RecordTypeId );
opp.Is_Child__c = true;
opp.Parent_OpportunityID__c = parentOppId;
opp.StageName = parentOp[0].StageName;
opp.Name = parentOp[i].Name;
opp.CloseDate = childOpps[i].CloseDate;
opp.Primary_Contact__c = childOpps[i].Primary_Contact__c;
opp.First_Installment_Date__c = parentOp [0].First_Installment_Date__c;
opp.Installment_Period__c = parentOp [0].Installment_Period__c;
opp.Last_Installment_Date__c = parentOp [0].Last_Installment_Date__c;
opp.Number_of_Installments__c = parentOp [0].Number_of_Installments__c;
opp.MINT_Lines_Magenta_IoT__c = childOpps[i].MINT_Lines_Magenta_IoT__c;
opp.Probability = parentOp [0].Probability;
opp.Scheduled_as_Child__c = childOpps[i].Scheduled_as_Child__c;
opp.Scheduled_with_Children__c = childOpps[i].Scheduled_with_Children__c;
opp.Schedule_Type__c = parentOp [0].Schedule_Type__c;
opp.Voice_Lines__c = childOpps[i].Voice_Lines__c;
opp.Yellow_IoT_Lines__c = childOpps[i].Yellow_IoT_Lines__c;
// update opp with records from childOpps[i] //
insertingChildren.add(opp);
}
System.debug( '-> insertingChildren1 = ' + insertingChildren);
try{
insert insertingChildren;
System.debug( '-> insertingChildren2 = ' + insertingChildren);
} catch(DmlException e)
{ System.debug('The following exception has occurred: ' + e.getMessage()); }
}
List<OpportunityLineItem> oliList = [ SELECT Id
FROM OpportunityLineItem
WHERE OpportunityId = :parentOppId ];
System.debug( '-> oliList = ' + oliList );
delete oliList;
/* insert (List<Opportunity>)JSON.deserialize(childOppsJson, List<Opportunity>.class);
delete [ SELECT Id
FROM OpportunityLineItem
WHERE OpportunityId = :parentOppId ]; */
for( Opportunity op : parentOp ){
op.StageName = 'Discovery';
}
System.debug( '-> parent = ' + parentOp );
update parentOp ;
} catch (Exception ex) {
System.debug( 'EXCEPTION @ ' + ex.getLineNumber() + ' (' + ex.getLineNumber() + ': ' +
ex.getMessage() + '): ScheduleOppsCC.createChildrenUpdateParent()\n -> parentOpp = ' +
parentOpp + '\n -> parentOppJson = ' + parentOppJson +
'\n -> childOppsJson = ' + childOppsJson);
throw new AuraHandledException('An error occurred, please reach out to your administrator for assistance.');
}
}
throw new AuraHandledException('This action is only available for Opportunity records.');
}
}
i am new to apex programming. in apex code below i have to pass one dml statement(opplist) to pass account id along with json string (parentOppJson) can anybody let me know how to do that.
/**
* Controller used by the "scheduleOppsLwc" Lightning Web Component.
* @since 2021-10-05
* @version 2021-10-05
*/
public class ScheduleOppsCC {
/**
* Creates new child installment Opportunities related to the Opportunity
* used for the "Schedule Opportunities" quick action and updates the parent
* as needed to CPQ-related details.
* @param parentOpp ID of Opportunity from quick action context
* @param parentOppJson JSON string-representation of the parent opportunity with new values for update
* @param childOppsJson JSON string-representation of a list of child opportunities to create
*/
@AuraEnabled
public static void createChildrenUpdateParent(String parentOpp, String parentOppJson, String childOppsJson) {
system.debug('--> Schema.Opportunity.SObjectType ' + Schema.Opportunity.SObjectType);
Id parentOppId = Id.valueOf( String.escapeSingleQuotes(parentOpp) );
system.debug('--> parentOppId ' + parentOppId);
if ( Schema.Opportunity.SObjectType == parentOppId.getSObjectType() )
{
try {
List<Opportunity> parentOp = (List<Opportunity>)JSON.deserialize(parentOppJson, List<Opportunity>.class);
System.debug( '-> parentOp = ' + parentOp );
//List<Opportunity> oppList = [ SELECT Id, AccountId , RecordTypeId , StageName, Name,CloseDate FROM Opportunity WHERE Id = :parentOppId ];
if ( (parentOp != null) && !parentOp .isEmpty() ) {
List<Opportunity> childOpps = (List<Opportunity>)JSON.deserialize(childOppsJson, List<Opportunity>.class);
system.debug('--> childOpps after deserialize ' + childOpps);
List<Opportunity> insertingChildren = new List<Opportunity>();
for ( Integer i = 0; i < childOpps.size(); ++i ) {
Opportunity opp = new Opportunity(AccountId = parentOp [0].AccountId, RecordTypeId = parentOp [0].RecordTypeId );
opp.Is_Child__c = true;
opp.Parent_OpportunityID__c = parentOppId;
opp.StageName = parentOp[0].StageName;
opp.Name = parentOp[i].Name;
opp.CloseDate = childOpps[i].CloseDate;
opp.Primary_Contact__c = childOpps[i].Primary_Contact__c;
opp.First_Installment_Date__c = parentOp [0].First_Installment_Date__c;
opp.Installment_Period__c = parentOp [0].Installment_Period__c;
opp.Last_Installment_Date__c = parentOp [0].Last_Installment_Date__c;
opp.Number_of_Installments__c = parentOp [0].Number_of_Installments__c;
opp.MINT_Lines_Magenta_IoT__c = childOpps[i].MINT_Lines_Magenta_IoT__c;
opp.Probability = parentOp [0].Probability;
opp.Scheduled_as_Child__c = childOpps[i].Scheduled_as_Child__c;
opp.Scheduled_with_Children__c = childOpps[i].Scheduled_with_Children__c;
opp.Schedule_Type__c = parentOp [0].Schedule_Type__c;
opp.Voice_Lines__c = childOpps[i].Voice_Lines__c;
opp.Yellow_IoT_Lines__c = childOpps[i].Yellow_IoT_Lines__c;
// update opp with records from childOpps[i] //
insertingChildren.add(opp);
}
System.debug( '-> insertingChildren1 = ' + insertingChildren);
try{
insert insertingChildren;
System.debug( '-> insertingChildren2 = ' + insertingChildren);
} catch(DmlException e)
{ System.debug('The following exception has occurred: ' + e.getMessage()); }
}
List<OpportunityLineItem> oliList = [ SELECT Id
FROM OpportunityLineItem
WHERE OpportunityId = :parentOppId ];
System.debug( '-> oliList = ' + oliList );
delete oliList;
/* insert (List<Opportunity>)JSON.deserialize(childOppsJson, List<Opportunity>.class);
delete [ SELECT Id
FROM OpportunityLineItem
WHERE OpportunityId = :parentOppId ]; */
for( Opportunity op : parentOp ){
op.StageName = 'Discovery';
}
System.debug( '-> parent = ' + parentOp );
update parentOp ;
} catch (Exception ex) {
System.debug( 'EXCEPTION @ ' + ex.getLineNumber() + ' (' + ex.getLineNumber() + ': ' +
ex.getMessage() + '): ScheduleOppsCC.createChildrenUpdateParent()\n -> parentOpp = ' +
parentOpp + '\n -> parentOppJson = ' + parentOppJson +
'\n -> childOppsJson = ' + childOppsJson);
throw new AuraHandledException('An error occurred, please reach out to your administrator for assistance.');
}
}
throw new AuraHandledException('This action is only available for Opportunity records.');
}
}
create an instance of this wrapper class in your@AuraEnabled method, set the values
return the wrapper to the client side controller
add the logic to parse this data in your lightning component
--
Naveen K N