• steven.fouracre
  • NEWBIE
  • 25 Points
  • Member since 2012

  • Chatter
    Feed
  • 1
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 7
    Questions
  • 6
    Replies
Using the following code I am able to deactivate a flow via code in any org, but if I use the same code in an org where a package has been installed. although you can still manually deactivate the flow, the code no longer works

Http h = new Http();
        HttpRequest req = new HttpRequest();
        req.setHeader('Authorization', 'OAuth ' + '{!$Credential.OAuthToken}');
req.setHeader('Content-Type', 'application/json');
req.setEndpoint('callout:Connect_to_this_org/services/data/v43.0/tooling/sobjects/FlowDefinition/<<flow id>>');
req.setMethod('PATCH');
String reqMsg = '{"Metadata": {"activeVersionNumber": 0}}';
req.setBody(reqMsg);
HttpResponse res = h.send(req);
        h.send(req);
system.debug('## res1 ' + res.getbody());
I have been trying to read the metadata of Flows from apex using api calls but it doesnt seem to work. 
A few things that may be important to know, my org has a namespace.

I have followed Andy Fawcetts blog at https://andyinthecloud.com/2015/11/28/automating-the-creation-of-flow-screens-with-apex-metadata-api/ but there are some issues

So Andy suggests to use UserInfo.getSessionId(); to set the session Id. that used to be possible but Salesforce now prevents setting the session id with this method.
The solution to provide connection is below :

    Http h = new Http();
    HttpRequest req = new HttpRequest();
    req.setEndpoint('callout:<<Named Credential>>/services/data/v40.0/limits');
    req.setMethod('GET');
    HttpResponse res = h.send(req);
    
    ses.MetadataService.MetadataPort service = new ses.MetadataService.MetadataPort();
    service.endpoint_x = 'callout:<<Named Credential>>/services/Soap/m/40.0';
    service.SessionHeader = new MetadataService.SessionHeader_element();
    service.SessionHeader.sessionId = '{!$Credential.OAuthToken}';

This will successfully give the connection. Code after this is basically the same as Andy's:

MetadataService.Flow flow =
    (MetadataService.Flow) service.readMetadata('Flow',
        new String[] { 'NewFlow-1' }).getRecords()[0];

However the output that is returned just has nulls. There is no error messages, just nothing returned:

Flow:[Metadata.fullName=null, actionCalls=null, actionCalls_type_info=(actionCalls, http://soap.sforce.com/2006/04/metadata, null, 0, -1, false), apexPluginCalls=null, apexPluginCalls_type_info=(apexPluginCalls, http://soap.sforce.com/2006/04/metadata, null, 0, -1, false), apex_schema_type_info=(http://soap.sforce.com/2006/04/metadata, true, false), assignments=(FlowAssignment:[FlowBaseElement.processMetadataValues=null, FlowElement.description=null,........


Ive tried entering the namespace for the Flow, removing "-1" and other different possible options and none seem to work
 
Ive attempted to query on ApexLog object and also tried using ToolingAPI to hopefully return a debug log, but nothing appears to work

Can anyone help?
I've created an Invocable Method but Im getting a failure

Insert failed. First exception on row 0; first error: CANNOT_EXECUTE_FLOW_TRIGGER, The record couldn’t be saved because it failed to trigger a flow. <br>A flow trigger failed to execute the flow with version ID 301d0000000LHot

Here is the method
    @InvocableMethod(label='Count_Characters_In_Function' description='')
    public static void processType(KeyValueInv[] kvLst){
//some basic code
//even changed to just putting system.debug
}

I've got an inner class which is basic

public class KeyValueInv {
    @InvocableVariable(label='key1' required=true)
    public String key1;
}

I can select everything as normal in the PB but it fails

Please help?
Ive created a function just as a test for now 

    @InvocableMethod(label='Setup_Salesforce_To_Salesforce_Email' description='Sends a Email To Explain Setup Of Salesforce To Salesforce')
    global static void sendSFToSFSetupEmail(String[] emls){
        try{
            Document doc = [Select Body From Document where id='015E00000068ZLg'];
            system.debug('## doc ' +doc);
            String PDFOutputBOL =  (doc.Body).toString();
            system.debug('## PDFOutputBOL ' +PDFOutputBOL);
        }
        catch(Exception ex){}
        
    }

Here's the PB Ive setup

User-added image


But when I create a lead I get this error

The record couldn’t be saved because it failed to trigger a flow. 
A flow trigger failed to execute the flow with version ID 301E0000000TjxN. 
Contact your administrator for help.


Any ideas?

 

So I've created a Public List type Custom setting and I'm accessing it in a normal way

 

XXX__c cs= XXX__c.getInstance('login');
system.debug('###login ' +cs.YYY__c + ' '+cs.ZZZ__c);

 

The 2nd line complains that it cannot find the field. Ive checked and everything exists even the record 'login'.

 

Note: All of the code completely works in the org that I created the package.

 

Not sure if there is a Salesforce bug related to Custom settings which cannot be used in packages

Im trying to make a Rest call to enterprise api. I connect successfully using the partner wsdl and a session id returns, but when I post the call below I get a CalloutException 

 

I used the same details on https://wiki.developerforce.com/page/Enterprise_Describe_Global

 

Can anyone help?

 

<?xml version="1.0" encoding="utf-8"?>' +
'<soapenv:Envelope xmlns:soapenv="<a href="http://schemas.xmlsoap.org/soap/envelope/" class="external free" title="http://schemas.xmlsoap.org/soap/envelope/" rel="nofollow">http://schemas.xmlsoap.org/soap/envelope/</a>' +
' xmlns:urn="urn:enterprise.soap.sforce.com">

<soapenv:Header>

   <urn:SessionHeader>

      <urn:sessionId><b>' +session_ID+ '</b></urn:sessionId>

   </urn:SessionHeader>

</soapenv:Header>

<soapenv:Body>

      <urn:describeGlobal />

</soapenv:Body>

</soapenv:Envelope>

Using the following code I am able to deactivate a flow via code in any org, but if I use the same code in an org where a package has been installed. although you can still manually deactivate the flow, the code no longer works

Http h = new Http();
        HttpRequest req = new HttpRequest();
        req.setHeader('Authorization', 'OAuth ' + '{!$Credential.OAuthToken}');
req.setHeader('Content-Type', 'application/json');
req.setEndpoint('callout:Connect_to_this_org/services/data/v43.0/tooling/sobjects/FlowDefinition/<<flow id>>');
req.setMethod('PATCH');
String reqMsg = '{"Metadata": {"activeVersionNumber": 0}}';
req.setBody(reqMsg);
HttpResponse res = h.send(req);
        h.send(req);
system.debug('## res1 ' + res.getbody());
I have been trying to read the metadata of Flows from apex using api calls but it doesnt seem to work. 
A few things that may be important to know, my org has a namespace.

I have followed Andy Fawcetts blog at https://andyinthecloud.com/2015/11/28/automating-the-creation-of-flow-screens-with-apex-metadata-api/ but there are some issues

So Andy suggests to use UserInfo.getSessionId(); to set the session Id. that used to be possible but Salesforce now prevents setting the session id with this method.
The solution to provide connection is below :

    Http h = new Http();
    HttpRequest req = new HttpRequest();
    req.setEndpoint('callout:<<Named Credential>>/services/data/v40.0/limits');
    req.setMethod('GET');
    HttpResponse res = h.send(req);
    
    ses.MetadataService.MetadataPort service = new ses.MetadataService.MetadataPort();
    service.endpoint_x = 'callout:<<Named Credential>>/services/Soap/m/40.0';
    service.SessionHeader = new MetadataService.SessionHeader_element();
    service.SessionHeader.sessionId = '{!$Credential.OAuthToken}';

This will successfully give the connection. Code after this is basically the same as Andy's:

MetadataService.Flow flow =
    (MetadataService.Flow) service.readMetadata('Flow',
        new String[] { 'NewFlow-1' }).getRecords()[0];

However the output that is returned just has nulls. There is no error messages, just nothing returned:

Flow:[Metadata.fullName=null, actionCalls=null, actionCalls_type_info=(actionCalls, http://soap.sforce.com/2006/04/metadata, null, 0, -1, false), apexPluginCalls=null, apexPluginCalls_type_info=(apexPluginCalls, http://soap.sforce.com/2006/04/metadata, null, 0, -1, false), apex_schema_type_info=(http://soap.sforce.com/2006/04/metadata, true, false), assignments=(FlowAssignment:[FlowBaseElement.processMetadataValues=null, FlowElement.description=null,........


Ive tried entering the namespace for the Flow, removing "-1" and other different possible options and none seem to work
 
Ive created a function just as a test for now 

    @InvocableMethod(label='Setup_Salesforce_To_Salesforce_Email' description='Sends a Email To Explain Setup Of Salesforce To Salesforce')
    global static void sendSFToSFSetupEmail(String[] emls){
        try{
            Document doc = [Select Body From Document where id='015E00000068ZLg'];
            system.debug('## doc ' +doc);
            String PDFOutputBOL =  (doc.Body).toString();
            system.debug('## PDFOutputBOL ' +PDFOutputBOL);
        }
        catch(Exception ex){}
        
    }

Here's the PB Ive setup

User-added image


But when I create a lead I get this error

The record couldn’t be saved because it failed to trigger a flow. 
A flow trigger failed to execute the flow with version ID 301E0000000TjxN. 
Contact your administrator for help.


Any ideas?

 

So I've created a Public List type Custom setting and I'm accessing it in a normal way

 

XXX__c cs= XXX__c.getInstance('login');
system.debug('###login ' +cs.YYY__c + ' '+cs.ZZZ__c);

 

The 2nd line complains that it cannot find the field. Ive checked and everything exists even the record 'login'.

 

Note: All of the code completely works in the org that I created the package.

 

Not sure if there is a Salesforce bug related to Custom settings which cannot be used in packages